on master help sign in
Rename field name, new constructors
90e7d2fsharkdp
examples/cat.rs+1−1
@@ -26,7 +26,7 @@ fn main() {
26 ]),
26 ]),
27 files: files
27 files: files
28 .iter()
28 .iter()
29− .map(|file| InputFile::Ordinary(OrdinaryFile::new(file, None)))
29+ .map(|file| InputFile::Ordinary(OrdinaryFile::from_path(file)))
30 .collect(),
30 .collect(),
31 ..Default::default()
31 ..Default::default()
32 };
32 };
examples/simple.rs+1−2
@@ -9,9 +9,8 @@ fn main() {
9 let path_to_this_file = OsStr::new(file!());
9 let path_to_this_file = OsStr::new(file!());
10
10
11 let config = Config {
11 let config = Config {
12− files: vec![InputFile::Ordinary(OrdinaryFile::new(
12+ files: vec![InputFile::Ordinary(OrdinaryFile::from_path(
13 path_to_this_file,
13 path_to_this_file,
14− None,
15 ))],
14 ))],
16 colored_output: true,
15 colored_output: true,
17 true_color: true,
16 true_color: true,
src/assets.rs+1−1
@@ -267,7 +267,7 @@ mod tests {
267 writeln!(temp_file, "{}", first_line).unwrap();
267 writeln!(temp_file, "{}", first_line).unwrap();
268 }
268 }
269
269
270− let input_file = InputFile::Ordinary(OrdinaryFile::new(OsStr::new(&file_path), None));
270+ let input_file = InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(&file_path)));
271 let syntax = self.assets.get_syntax(
271 let syntax = self.assets.get_syntax(
272 None,
272 None,
273 input_file,
273 input_file,
src/bin/bat/app.rs+3−1
@@ -265,7 +265,9 @@ impl App {
265 if input.to_str().unwrap() == "-" {
265 if input.to_str().unwrap() == "-" {
266 file_input.push(InputFile::StdIn(name));
266 file_input.push(InputFile::StdIn(name));
267 } else {
267 } else {
268− file_input.push(InputFile::Ordinary(OrdinaryFile::new(input, name)))
268+ file_input.push(InputFile::Ordinary(OrdinaryFile::from_path_with_name(
269+ input, name,
270+ )))
269 }
271 }
270 }
272 }
271 None => {}
273 None => {}
src/bin/bat/main.rs+3−4
@@ -167,10 +167,9 @@ fn run() -> Result<bool> {
167 Ok(true)
167 Ok(true)
168 } else {
168 } else {
169 let mut config = app.config()?;
169 let mut config = app.config()?;
170− config.files = vec![InputFile::Ordinary(OrdinaryFile::new(
170+ config.files = vec![InputFile::Ordinary(OrdinaryFile::from_path(OsStr::new(
171− OsStr::new("cache"),
171+ "cache",
172− None,
173− ))];
172+ )))];
174
173
175 run_controller(&config)
174 run_controller(&config)
176 }
175 }
src/inputfile.rs+18−8
@@ -54,20 +54,30 @@ impl<'a> InputFileReader<'a> {
54
54
55#[derive(Debug, Clone, Copy, PartialEq)]
55#[derive(Debug, Clone, Copy, PartialEq)]
56pub struct OrdinaryFile<'a> {
56pub struct OrdinaryFile<'a> {
57− filename: &'a OsStr,
57+ path: &'a OsStr,
58 user_provided_name: Option<&'a OsStr>,
58 user_provided_name: Option<&'a OsStr>,
59}
59}
60
60
61impl<'a> OrdinaryFile<'a> {
61impl<'a> OrdinaryFile<'a> {
62− pub fn new(filename: &'a OsStr, user_provided_name: Option<&'a OsStr>) -> OrdinaryFile<'a> {
62+ pub fn from_path(path: &'a OsStr) -> OrdinaryFile<'a> {
63 OrdinaryFile {
63 OrdinaryFile {
64− filename: filename,
64+ path,
65+ user_provided_name: None,
66+ }
67+ }
68+
69+ pub fn from_path_with_name(
70+ path: &'a OsStr,
71+ user_provided_name: Option<&'a OsStr>,
72+ ) -> OrdinaryFile<'a> {
73+ OrdinaryFile {
74+ path,
65− user_provided_name: user_provided_name,
75+ user_provided_name,
66 }
76 }
67 }
77 }
68
78
69 pub fn filename(&self) -> &'a OsStr {
79 pub fn filename(&self) -> &'a OsStr {
70− self.user_provided_name.unwrap_or_else(|| self.filename)
80+ self.user_provided_name.unwrap_or_else(|| self.path)
71 }
81 }
72}
82}
73
83
@@ -83,12 +93,12 @@ impl<'a> InputFile<'a> {
83 match self {
93 match self {
84 InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())),
94 InputFile::StdIn(_) => Ok(InputFileReader::new(stdin.lock())),
85 InputFile::Ordinary(ofile) => {
95 InputFile::Ordinary(ofile) => {
86− let file = File::open(ofile.filename)
96+ let file = File::open(ofile.path)
87− .map_err(|e| format!("'{}': {}", ofile.filename.to_string_lossy(), e))?;
97+ .map_err(|e| format!("'{}': {}", ofile.path.to_string_lossy(), e))?;
88
98
89 if file.metadata()?.is_dir() {
99 if file.metadata()?.is_dir() {
90 return Err(
100 return Err(
91− format!("'{}' is a directory.", ofile.filename.to_string_lossy()).into(),
101+ format!("'{}' is a directory.", ofile.path.to_string_lossy()).into(),
92 );
102 );
93 }
103 }
94
104