Skip to content

Commit

Permalink
Adding back access to filename for FileOrStdin
Browse files Browse the repository at this point in the history
In a previous change I removed pub visibility for `Source` in case the internal
implementation changes, but this removed access to the original filename
(or "-" in the case of stdin).

This diff adds `FileOrStdin::filename` to provide access to the original
filename.
  • Loading branch information
thepacketgeek committed Jul 12, 2024
1 parent 1db76ba commit ba4fb3e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/file_or_stdin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ impl<T> FileOrStdin<T> {
!self.is_stdin()
}

/// The value passed to this arg (Either "-" for stdin or a filepath)
pub fn filename(&self) -> &str {
match &self.source {
Source::Stdin => "-",
Source::Arg(path) => path,
}
}

/// Read the entire contents from the input source, returning T::from_str
pub fn contents(self) -> Result<T, StdinError>
where
Expand Down Expand Up @@ -168,3 +176,16 @@ impl<T> FromStr for FileOrStdin<T> {
})
}
}

#[test]
fn test_source_methods() {
let val: FileOrStdin<String> = "-".parse().unwrap();
assert!(val.is_stdin());
assert!(!val.is_file());
assert_eq!(val.filename(), "-");

let val: FileOrStdin<String> = "/path/to/something".parse().unwrap();
assert!(val.is_file());
assert!(!val.is_stdin());
assert_eq!(val.filename(), "/path/to/something");
}

0 comments on commit ba4fb3e

Please sign in to comment.