Rename field name, new constructors

This commit is contained in:
sharkdp 2020-04-11 19:17:36 +02:00 committed by David Peter
parent 04fa84aea7
commit 90e7d2fe33
6 changed files with 27 additions and 17 deletions

View File

@ -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()
};

View File

@ -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,

View File

@ -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,

View File

@ -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 => {}

View File

@ -167,10 +167,9 @@ fn run() -> Result<bool> {
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)
}

View File

@ -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(),
);
}