From b424be466079ddccfee1950e9f225d1e85bbe6e8 Mon Sep 17 00:00:00 2001 From: Loi Chyan Date: Wed, 2 Oct 2024 20:19:35 +0800 Subject: [PATCH] refactor: use `Utf8PathBuf` instead of `PathBuf` --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/cli.rs | 6 +++--- src/main.rs | 2 +- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bb13d70..98fd433 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -181,6 +181,12 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e368af43e418a04d52505cf3dbc23dda4e3407ae2fa99fd0e4f308ce546acc" +[[package]] +name = "camino" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" + [[package]] name = "cc" version = "1.1.0" @@ -574,6 +580,7 @@ version = "0.4.1" dependencies = [ "assert_cmd", "bytesize", + "camino", "clap", "codespan-reporting", "content_inspector", diff --git a/Cargo.toml b/Cargo.toml index 3b05bc0..36d14af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ rust-version = "1.70" [dependencies] bytesize = "1.3" +camino = "1.1" clap = { version = ">=4.0, <4.5", features = ["derive"] } codespan-reporting = "0.11.1" content_inspector = "0.2.4" diff --git a/src/cli.rs b/src/cli.rs index ab5dc01..151bb6a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,11 +1,11 @@ //! Command line arguments parser. use std::io::BufReader; -use std::path::PathBuf; use std::str::FromStr; use std::{fmt, fs, io}; use bytesize::ByteSize; +use camino::Utf8PathBuf; use clap::{Parser, Subcommand, ValueEnum}; use shadow_rs::formatcp; use thisctx::IntoError; @@ -170,7 +170,7 @@ impl FromStr for UserInput { #[derive(Clone, Debug)] pub enum IoPath { Stdio, - Path(PathBuf), + Path(Utf8PathBuf), } impl FromStr for IoPath { @@ -189,7 +189,7 @@ impl fmt::Display for IoPath { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Stdio => f.write_str("STDIO"), - Self::Path(path) => path.display().fmt(f), + Self::Path(path) => path.fmt(f), } } } diff --git a/src/main.rs b/src/main.rs index e991913..a4ac93e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,7 +58,7 @@ fn walk<'a>( }) .transpose() }) - .map(|e| e.map(|path| Source(IoPath::Path(path), None))), + .map(|e| e.map(|path| Source(IoPath::Path(path.try_into().unwrap()), None))), ) } }