replaced some pathbufs with paths, made it actually compile and work 0uo
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Lynne Megido 2021-03-12 03:26:35 +10:00
parent 6d55b3c309
commit e3af10cf5b
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
6 changed files with 39 additions and 24 deletions

View file

@ -10,6 +10,13 @@
</Attribute>
</value>
</entry>
<entry key="/src/formats.rs">
<value>
<Attribute>
<option name="separator" value="&#9;" />
</Attribute>
</value>
</entry>
<entry key="/src/inspectors.rs">
<value>
<Attribute>
@ -38,6 +45,13 @@
</Attribute>
</value>
</entry>
<entry key="/src/tests/mod.rs">
<value>
<Attribute>
<option name="separator" value="&#9;" />
</Attribute>
</value>
</entry>
</map>
</option>
</component>

View file

@ -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<String> {
mime_extension_lookup(self.mime.clone()).map(|extensions| extensions[0].to_owned())
}
}

View file

@ -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<Findings, ScanError>];
type Entries<'a> = [Result<Findings, ScanError<'a>>];
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<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
pub trait Format {
fn new() -> Self;
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()>;
fn no_known_extension<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
fn unknown_type<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()>;
fn no_known_extension<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()>;
fn unreadable<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()>;
fn unknown_type<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()>;
fn header<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
fn footer<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
@ -109,7 +109,7 @@ impl Format for Script {
Self {}
}
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()> {
fn rename<W: Write>(&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<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
fn no_known_extension<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
smart_write(
f,
&["echo No known extension for ".into(), path.into(), Writable::Newline],
)
}
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
fn unreadable<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
smart_write(f, &["# Failed to read ".into(), path.into(), Writable::Newline])
}
fn unknown_type<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
fn unknown_type<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()> {
smart_write(
f,
&[

View file

@ -211,14 +211,14 @@ fn scan_file(entry: &DirEntry) -> Result<Findings, ScanError> {
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();

View file

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

View file

@ -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!(