Skip to content

Commit ba4fb3e

Browse files
committed
Adding back access to filename for FileOrStdin
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.
1 parent 1db76ba commit ba4fb3e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/file_or_stdin.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ impl<T> FileOrStdin<T> {
5252
!self.is_stdin()
5353
}
5454

55+
/// The value passed to this arg (Either "-" for stdin or a filepath)
56+
pub fn filename(&self) -> &str {
57+
match &self.source {
58+
Source::Stdin => "-",
59+
Source::Arg(path) => path,
60+
}
61+
}
62+
5563
/// Read the entire contents from the input source, returning T::from_str
5664
pub fn contents(self) -> Result<T, StdinError>
5765
where
@@ -168,3 +176,16 @@ impl<T> FromStr for FileOrStdin<T> {
168176
})
169177
}
170178
}
179+
180+
#[test]
181+
fn test_source_methods() {
182+
let val: FileOrStdin<String> = "-".parse().unwrap();
183+
assert!(val.is_stdin());
184+
assert!(!val.is_file());
185+
assert_eq!(val.filename(), "-");
186+
187+
let val: FileOrStdin<String> = "/path/to/something".parse().unwrap();
188+
assert!(val.is_file());
189+
assert!(!val.is_stdin());
190+
assert_eq!(val.filename(), "/path/to/something");
191+
}

0 commit comments

Comments
 (0)