diff --git a/src/args.rs b/src/args.rs index 641cfb5..ffed051 100644 --- a/src/args.rs +++ b/src/args.rs @@ -6,8 +6,8 @@ use word_tally::{Case, Sort}; #[command(about, version)] pub struct Args { /// File path to use as input rather than stdin ("-"). - #[arg(value_name = "PATH", default_value = "-", value_parser = clap::value_parser!(PathBuf))] - pub input: PathBuf, + #[arg(default_value = "-", value_name = "PATH")] + pub input: String, /// Sort order. #[arg(short, long, default_value_t, value_enum, value_name = "ORDER")] diff --git a/src/input.rs b/src/input.rs index 280b55d..fada3b7 100644 --- a/src/input.rs +++ b/src/input.rs @@ -11,11 +11,11 @@ pub enum Input { impl Input { /// Construct an `Input` from a file path or stdin. - pub fn from_args(path: PathBuf) -> Result { - if path.to_str() == Some("-") { + pub fn from_args(path: String) -> Result { + if path == "-" { Ok(Self::Stdin) } else { - Ok(Self::File(path)) + Ok(Self::File(PathBuf::from(path))) } } @@ -31,11 +31,16 @@ impl Input { } } - /// Returns the file name of the input if available. - pub fn file_name(&self) -> Option<&str> { + /// Returns the file name of the input or `"-"` for STDIN. + pub fn source(&self) -> String { match self { - Self::File(path) => path.file_name()?.to_str(), - Self::Stdin => None, + Self::File(path) => path + .file_name() + .expect("File name inaccessible.") + .to_str() + .expect("File name invalid UTF-8."), + Self::Stdin => "-", } + .to_string() } } diff --git a/src/main.rs b/src/main.rs index f47e5ae..279cd13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,10 +19,10 @@ fn main() -> Result<()> { let delimiter = unescape(&args.delimiter)?; let input = Input::from_args(args.input)?; - let input_file_name = input.file_name().unwrap_or("-").to_string(); + let source = input.source(); let reader = input .get_reader() - .context(format!("Failed to read from {}.", input_file_name))?; + .context(format!("Failed to read from {}.", source))?; let options = Options { case: args.case, @@ -41,12 +41,7 @@ fn main() -> Result<()> { let verbose = Verbose {}; let mut stderr_output = Output::stderr(); - verbose.log( - &mut stderr_output, - &word_tally, - &delimiter, - &input_file_name, - )?; + verbose.log(&mut stderr_output, &word_tally, &delimiter, &source)?; }; let mut output = Output::from_args(args.output)?;