on master help sign in
Improve readability

Using `Path`s for paths expresses intent more clearly.

commit35347c23109400e9694f073bf8fe901b3ca1d59f authorAleksey Kladov <aleksey.kladov@gmail.com> dateTue 2 Mar 2021, 11:15 (6 years ago) parentb489fc7
8 files changed+42−33
CHANGELOG.md+2 src/assets.rs+4−4 src/bin/bat/app.rs+10−7 src/bin/bat/input.rs+4−4 src/bin/bat/main.rs+1−2 src/diff.rs+1−2 src/input.rs+13−7 src/pretty_printer.rs+7−7
CHANGELOG.md+2
@@ -6,6 +6,8 @@
66
77## Other
88
9+- `Input::ordinary_file` and `Input::with_name` now accept `Path` rather than `OsStr` see #1571 (@matklad)
10+
911## Syntaxes
1012
1113## New themes
src/assets.rs+4−4
@@ -327,7 +327,7 @@ mod tests {
327327 writeln!(temp_file, "{}", first_line).unwrap();
328328 }
329329
330− let input = Input::ordinary_file(file_path.as_os_str());
330+ let input = Input::ordinary_file(&file_path);
331331 let dummy_stdin: &[u8] = &[];
332332 let mut opened_input = input.open(dummy_stdin, None).unwrap();
333333
@@ -341,7 +341,7 @@ mod tests {
341341 fn syntax_for_file_with_content_os(&self, file_name: &OsStr, first_line: &str) -> String {
342342 let file_path = self.temp_dir.path().join(file_name);
343343 let input = Input::from_reader(Box::new(BufReader::new(first_line.as_bytes())))
344− .with_name(Some(file_path.as_os_str()));
344+ .with_name(Some(&file_path));
345345 let dummy_stdin: &[u8] = &[];
346346 let mut opened_input = input.open(dummy_stdin, None).unwrap();
347347
@@ -366,7 +366,7 @@ mod tests {
366366 }
367367
368368 fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
369− let input = Input::stdin().with_name(Some(OsStr::new(file_name)));
369+ let input = Input::stdin().with_name(Some(file_name));
370370 let mut opened_input = input.open(content, None).unwrap();
371371
372372 self.assets
@@ -521,7 +521,7 @@ mod tests {
521521 .expect("creation of directory succeeds");
522522 symlink(&file_path, &file_path_symlink).expect("creation of symbolic link succeeds");
523523
524− let input = Input::ordinary_file(file_path_symlink.as_os_str());
524+ let input = Input::ordinary_file(&file_path_symlink);
525525 let dummy_stdin: &[u8] = &[];
526526 let mut opened_input = input.open(dummy_stdin, None).unwrap();
527527
src/bin/bat/app.rs+10−7
@@ -1,6 +1,6 @@
11use std::collections::HashSet;
22use std::env;
3−use std::ffi::OsStr;
3+use std::path::Path;
44use std::str::FromStr;
55
66use atty::{self, Stream};
@@ -248,16 +248,19 @@ impl App {
248248 }
249249 _ => {}
250250 }
251− let filenames: Option<Vec<&str>> = self
251+ let filenames: Option<Vec<&Path>> = self
252252 .matches
253− .values_of("file-name")
254− .map(|values| values.collect());
253+ .values_of_os("file-name")
254+ .map(|values| values.map(Path::new).collect());
255255
256− let mut filenames_or_none: Box<dyn Iterator<Item = _>> = match filenames {
257− Some(ref filenames) => Box::new(filenames.iter().map(|name| Some(OsStr::new(*name)))),
256+ let mut filenames_or_none: Box<dyn Iterator<Item = Option<&Path>>> = match filenames {
257+ Some(filenames) => Box::new(filenames.into_iter().map(Some)),
258258 None => Box::new(std::iter::repeat(None)),
259259 };
260− let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
260+ let files: Option<Vec<&Path>> = self
261+ .matches
262+ .values_of_os("FILE")
263+ .map(|vs| vs.map(Path::new).collect());
261264
262265 if files.is_none() {
263266 return Ok(vec![new_stdin_input(
src/bin/bat/input.rs+4−4
@@ -1,15 +1,15 @@
11use bat::input::Input;
2−use std::ffi::OsStr;
2+use std::path::Path;
33
4−pub fn new_file_input<'a>(file: &'a OsStr, name: Option<&'a OsStr>) -> Input<'a> {
4+pub fn new_file_input<'a>(file: &'a Path, name: Option<&'a Path>) -> Input<'a> {
55 named(Input::ordinary_file(file), name.or(Some(file)))
66}
77
8−pub fn new_stdin_input(name: Option<&OsStr>) -> Input {
8+pub fn new_stdin_input(name: Option<&Path>) -> Input {
99 named(Input::stdin(), name)
1010}
1111
12−fn named<'a>(input: Input<'a>, name: Option<&OsStr>) -> Input<'a> {
12+fn named<'a>(input: Input<'a>, name: Option<&Path>) -> Input<'a> {
1313 if let Some(provided_name) = name {
1414 let mut input = input.with_name(Some(provided_name));
1515 input.description_mut().set_kind(Some("File".to_owned()));
src/bin/bat/main.rs+1−2
@@ -9,7 +9,6 @@ mod directories;
99mod input;
1010
1111use std::collections::{HashMap, HashSet};
12−use std::ffi::OsStr;
1312use std::io;
1413use std::io::{BufReader, Write};
1514use std::path::Path;
@@ -269,7 +268,7 @@ fn run() -> Result<bool> {
269268 run_cache_subcommand(cache_matches)?;
270269 Ok(true)
271270 } else {
272− let inputs = vec![Input::ordinary_file(OsStr::new("cache"))];
271+ let inputs = vec![Input::ordinary_file("cache")];
273272 let config = app.config(&inputs)?;
274273
275274 run_controller(inputs, &config)
src/diff.rs+1−2
@@ -1,7 +1,6 @@
11#![cfg(feature = "git")]
22
33use std::collections::HashMap;
4−use std::ffi::OsStr;
54use std::fs;
65use std::path::Path;
76
@@ -17,7 +16,7 @@ pub enum LineChange {
1716
1817pub type LineChanges = HashMap<u32, LineChange>;
1918
20−pub fn get_git_diff(filename: &OsStr) -> Option<LineChanges> {
19+pub fn get_git_diff(filename: &Path) -> Option<LineChanges> {
2120 let repo = Repository::discover(&filename).ok()?;
2221
2322 let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?;
src/input.rs+13−7
@@ -1,7 +1,7 @@
11use std::convert::TryFrom;
2−use std::ffi::{OsStr, OsString};
32use std::fs::File;
43use std::io::{self, BufRead, BufReader, Read};
4+use std::path::{Path, PathBuf};
55
66use clircle::{Clircle, Identifier};
77use content_inspector::{self, ContentType};
@@ -69,7 +69,7 @@ impl InputDescription {
6969}
7070
7171pub(crate) enum InputKind<'a> {
72− OrdinaryFile(OsString),
72+ OrdinaryFile(PathBuf),
7373 StdIn,
7474 CustomReader(Box<dyn Read + 'a>),
7575}
@@ -86,7 +86,7 @@ impl<'a> InputKind<'a> {
8686
8787#[derive(Clone, Default)]
8888pub(crate) struct InputMetadata {
89− pub(crate) user_provided_name: Option<OsString>,
89+ pub(crate) user_provided_name: Option<PathBuf>,
9090}
9191
9292pub struct Input<'a> {
@@ -96,7 +96,7 @@ pub struct Input<'a> {
9696}
9797
9898pub(crate) enum OpenedInputKind {
99− OrdinaryFile(OsString),
99+ OrdinaryFile(PathBuf),
100100 StdIn,
101101 CustomReader,
102102}
@@ -109,8 +109,11 @@ pub(crate) struct OpenedInput<'a> {
109109}
110110
111111impl<'a> Input<'a> {
112− pub fn ordinary_file(path: &OsStr) -> Self {
113− let kind = InputKind::OrdinaryFile(path.to_os_string());
112+ pub fn ordinary_file(path: impl AsRef<Path>) -> Self {
113+ Self::_ordinary_file(path.as_ref())
114+ }
115+ fn _ordinary_file(path: &Path) -> Self {
116+ let kind = InputKind::OrdinaryFile(path.to_path_buf());
114117 Input {
115118 description: kind.description(),
116119 metadata: InputMetadata::default(),
@@ -140,7 +143,10 @@ impl<'a> Input<'a> {
140143 matches!(self.kind, InputKind::StdIn)
141144 }
142145
143− pub fn with_name(mut self, provided_name: Option<&OsStr>) -> Self {
146+ pub fn with_name(mut self, provided_name: Option<impl AsRef<Path>>) -> Self {
147+ self._with_name(provided_name.as_ref().map(|it| it.as_ref()))
148+ }
149+ pub fn _with_name(mut self, provided_name: Option<&Path>) -> Self {
144150 if let Some(name) = provided_name {
145151 self.description.name = name.to_string_lossy().to_string()
146152 }
src/pretty_printer.rs+7−7
@@ -1,5 +1,5 @@
1−use std::ffi::OsStr;
21use std::io::Read;
2+use std::path::Path;
33
44use console::Term;
55use syntect::parsing::SyntaxReference;
@@ -71,7 +71,7 @@ impl<'a> PrettyPrinter<'a> {
7171 }
7272
7373 /// Add a file which should be pretty-printed
74− pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
74+ pub fn input_file(&mut self, path: impl AsRef<Path>) -> &mut Self {
7575 self.input(Input::from_file(path).kind("File"))
7676 }
7777
@@ -79,7 +79,7 @@ impl<'a> PrettyPrinter<'a> {
7979 pub fn input_files<I, P>(&mut self, paths: I) -> &mut Self
8080 where
8181 I: IntoIterator<Item = P>,
82− P: AsRef<OsStr>,
82+ P: AsRef<Path>,
8383 {
8484 self.inputs(paths.into_iter().map(Input::from_file))
8585 }
@@ -297,8 +297,8 @@ impl<'a> Input<'a> {
297297 }
298298
299299 /// A new input from a file.
300− pub fn from_file(path: impl AsRef<OsStr>) -> Self {
301− input::Input::ordinary_file(path.as_ref()).into()
300+ pub fn from_file(path: impl AsRef<Path>) -> Self {
301+ input::Input::ordinary_file(path).into()
302302 }
303303
304304 /// A new input from bytes.
@@ -313,8 +313,8 @@ impl<'a> Input<'a> {
313313
314314 /// The filename of the input.
315315 /// This affects syntax detection and changes the default header title.
316− pub fn name(mut self, name: impl AsRef<OsStr>) -> Self {
317− self.input = self.input.with_name(Some(name.as_ref()));
316+ pub fn name(mut self, name: impl AsRef<Path>) -> Self {
317+ self.input = self.input.with_name(Some(name));
318318 self
319319 }
320320