From 90e7d2fe335052e7bd83a48a8925934978dcc37b Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sat, 11 Apr 2020 19:17:36 +0200 Subject: [PATCH] Rename field name, new constructors --- examples/cat.rs | 2 +- examples/simple.rs | 3 +-- src/assets.rs | 2 +- src/bin/bat/app.rs | 4 +++- src/bin/bat/main.rs | 7 +++---- src/inputfile.rs | 26 ++++++++++++++++++-------- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/examples/cat.rs b/examples/cat.rs index cdec1caa..c98270bb 100644 --- a/examples/cat.rs +++ b/examples/cat.rs @@ -26,7 +26,7 @@ fn main() { ]), files: files .iter() - .map(|file| InputFile::Ordinary(OrdinaryFile::new(file, None))) + .map(|file| InputFile::Ordinary(OrdinaryFile::from_path(file))) .collect(), ..Default::default() }; diff --git a/examples/simple.rs b/examples/simple.rs index be2f70c8..a0d7afa1 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -9,9 +9,8 @@ fn main() { let path_to_this_file = OsStr::new(file!()); let config = Config { - files: vec![InputFile::Ordinary(OrdinaryFile::new( + files: vec![InputFile::Ordinary(OrdinaryFile::from_path( path_to_this_file, - None, ))], colored_output: true, true_color: true, diff --git a/src/assets.rs b/src/assets.rs index 4328fd15..2dfcebfb 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -267,7 +267,7 @@ mod tests { writeln!(temp_file, "{}", first_line).unwrap(); } - let input_file = InputFile::Ordinary(OrdinaryFile::new(OsStr::new(&file_path), None)); + let input_file = InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(&file_path))); let syntax = self.assets.get_syntax( None, input_file, diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index e828d38d..2da8a0d1 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -265,7 +265,9 @@ impl App { if input.to_str().unwrap() == "-" { file_input.push(InputFile::StdIn(name)); } else { - file_input.push(InputFile::Ordinary(OrdinaryFile::new(input, name))) + file_input.push(InputFile::Ordinary(OrdinaryFile::from_path_with_name( + input, name, + ))) } } None => {} diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index 15277f62..178c5c8c 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -167,10 +167,9 @@ fn run() -> Result { Ok(true) } else { let mut config = app.config()?; - config.files = vec![InputFile::Ordinary(OrdinaryFile::new( - OsStr::new("cache"), - None, - ))]; + config.files = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new( + "cache", + )))]; run_controller(&config) } diff --git a/src/inputfile.rs b/src/inputfile.rs index 7556ab97..0e4b8e28 100644 --- a/src/inputfile.rs +++ b/src/inputfile.rs @@ -54,20 +54,30 @@ impl<'a> InputFileReader<'a> { #[derive(Debug, Clone, Copy, PartialEq)] pub struct OrdinaryFile<'a> { - filename: &'a OsStr, + path: &'a OsStr, user_provided_name: Option<&'a OsStr>, } impl<'a> OrdinaryFile<'a> { - pub fn new(filename: &'a OsStr, user_provided_name: Option<&'a OsStr>) -> OrdinaryFile<'a> { + pub fn from_path(path: &'a OsStr) -> OrdinaryFile<'a> { OrdinaryFile { - filename: filename, - user_provided_name: user_provided_name, + path, + user_provided_name: None, + } + } + + pub fn from_path_with_name( + path: &'a OsStr, + user_provided_name: Option<&'a OsStr>, + ) -> OrdinaryFile<'a> { + OrdinaryFile { + path, + user_provided_name, } } pub fn filename(&self) -> &'a OsStr { - self.user_provided_name.unwrap_or_else(|| self.filename) + self.user_provided_name.unwrap_or_else(|| self.path) } } @@ -83,12 +93,12 @@ impl<'a> InputFile<'a> { match self { InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())), InputFile::Ordinary(ofile) => { - let file = File::open(ofile.filename) - .map_err(|e| format!("'{}': {}", ofile.filename.to_string_lossy(), e))?; + let file = File::open(ofile.path) + .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?; if file.metadata()?.is_dir() { return Err( - format!("'{}' is a directory.", ofile.filename.to_string_lossy()).into(), + format!("'{}' is a directory.", ofile.path.to_string_lossy()).into(), ); }