diff --git a/.idea/csv-plugin.xml b/.idea/csv-plugin.xml index 7f1c68a..b2eb9b1 100644 --- a/.idea/csv-plugin.xml +++ b/.idea/csv-plugin.xml @@ -10,6 +10,13 @@ + + + + + + @@ -38,6 +45,13 @@ + + + + + + diff --git a/src/findings.rs b/src/findings.rs index 0190569..05df884 100644 --- a/src/findings.rs +++ b/src/findings.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use mime_guess::Mime; +use smartstring::alias::*; use crate::inspectors::mime_extension_lookup; @@ -15,7 +16,7 @@ pub struct Findings { } impl Findings { - pub fn recommended_extension(&self) -> Option<&str> { - mime_extension_lookup(self.mime.clone()).map(|extensions| &*extensions[0]) + pub fn recommended_extension(&self) -> Option { + mime_extension_lookup(self.mime.clone()).map(|extensions| extensions[0].to_owned()) } } diff --git a/src/formats.rs b/src/formats.rs index 4f02a0b..c669852 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -3,7 +3,7 @@ use std::io::{self, Write}; #[cfg(unix)] use std::os::unix::ffi::OsStrExt; -use std::path::PathBuf; +use std::path::{Path}; use snailquote::escape; @@ -14,11 +14,11 @@ use crate::{Findings, BACKEND}; const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); #[doc(hidden)] -type Entries = [Result]; +type Entries<'a> = [Result>]; enum Writable<'a> { String(&'a str), - Path(&'a PathBuf), + Path(&'a Path), Space, Newline, } @@ -30,8 +30,8 @@ impl<'a> From<&'a str> for Writable<'a> { } } -impl<'a> From<&'a PathBuf> for Writable<'a> { - fn from(p: &'a PathBuf) -> Writable<'a> { +impl<'a> From<&'a Path> for Writable<'a> { + fn from(p: &'a Path) -> Writable<'a> { Writable::Path(p) } } @@ -63,10 +63,10 @@ fn smart_write(f: &mut W, writeables: &[Writable]) -> io::Result<()> { pub trait Format { fn new() -> Self; - fn rename(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()>; - fn no_known_extension(&self, f: &mut W, path: &PathBuf) -> io::Result<()>; - fn unreadable(&self, f: &mut W, path: &PathBuf) -> io::Result<()>; - fn unknown_type(&self, f: &mut W, path: &PathBuf) -> io::Result<()>; + fn rename(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()>; + fn no_known_extension(&self, f: &mut W, path: &Path) -> io::Result<()>; + fn unreadable(&self, f: &mut W, path: &Path) -> io::Result<()>; + fn unknown_type(&self, f: &mut W, path: &Path) -> io::Result<()>; fn header(&self, entries: &Entries, f: &mut W) -> io::Result<()>; fn footer(&self, entries: &Entries, f: &mut W) -> io::Result<()>; @@ -109,7 +109,7 @@ impl Format for Script { Self {} } - fn rename(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()> { + fn rename(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> { smart_write( f, &[ @@ -122,18 +122,18 @@ impl Format for Script { ) } - fn no_known_extension(&self, f: &mut W, path: &PathBuf) -> io::Result<()> { + fn no_known_extension(&self, f: &mut W, path: &Path) -> io::Result<()> { smart_write( f, &["echo No known extension for ".into(), path.into(), Writable::Newline], ) } - fn unreadable(&self, f: &mut W, path: &PathBuf) -> io::Result<()> { + fn unreadable(&self, f: &mut W, path: &Path) -> io::Result<()> { smart_write(f, &["# Failed to read ".into(), path.into(), Writable::Newline]) } - fn unknown_type(&self, f: &mut W, path: &PathBuf) -> io::Result<()> { + fn unknown_type(&self, f: &mut W, path: &Path) -> io::Result<()> { smart_write( f, &[ diff --git a/src/main.rs b/src/main.rs index c458a59..2b63ae6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -211,14 +211,14 @@ fn scan_file(entry: &DirEntry) -> Result { if result.is_err() { // an error occurred while trying to read the file // error!("{}: {}", entry.path().to_string_lossy(), error); - return Err(ScanError::File(entry.path().to_path_buf())); + return Err(ScanError::File(entry.path())); } let result = result.unwrap(); if result.is_none() { // the file was read successfully, but we were unable to determine its mimetype // warn!("Couldn't determine mimetype for {}", entry.path().to_string_lossy()); - return Err(ScanError::Mime(entry.path().to_path_buf())); + return Err(ScanError::Mime(entry.path())); } let result = result.unwrap(); diff --git a/src/scan_error.rs b/src/scan_error.rs index de26b29..c4fc785 100644 --- a/src/scan_error.rs +++ b/src/scan_error.rs @@ -1,15 +1,15 @@ use std::fmt::{Display, Formatter, Result}; -use std::path::PathBuf; +use std::path::Path; #[derive(Debug)] -pub enum ScanError { +pub enum ScanError<'a> { /// Something went wrong while trying to read the given file. - File(PathBuf), + File(&'a Path), /// Failed to determine the mimetype of the given file. - Mime(PathBuf), + Mime(&'a Path), } -impl Display for ScanError { +impl<'a> Display for ScanError<'a> { fn fmt(&self, f: &mut Formatter<'_>) -> Result { write!( f, diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 60e63d8..7290164 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -125,14 +125,14 @@ fn simple_directory() { // 3. ensure recommended extension is in the list of known extensions for PNG files assert!(mime_extension_lookup(IMAGE_PNG) .unwrap() - .contains(&result.recommended_extension().unwrap())); + .contains(&result.recommended_extension().unwrap().into())); continue; } // check if the recommended extension for this file is in the list of known extensions for its mimetype assert!(mime_extension_lookup(result.mime.clone()) .unwrap() - .contains(&result.recommended_extension().unwrap())); + .contains(&result.recommended_extension().unwrap().into())); // make sure the guessed mimetype is correct based on the extension of the scanned file assert_eq!(