thanks rustfmt

This commit is contained in:
Lynne Megido 2021-02-22 00:15:09 +10:00
parent 7a2f009622
commit 9091850ec5
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
6 changed files with 50 additions and 50 deletions

View File

@ -7,20 +7,27 @@ pub enum ExtensionSet {
Videos,
Media,
Documents,
Archives
Archives,
}
impl ExtensionSet {
pub fn extensions(&self) -> Vec<&str> {
match self {
Self::Images => vec!["png", "jpg", "jpeg", "webp", "raw", "gif", "apng", "tga", "bmp", "tif", "tiff", "heif",
"avif", "jp2", "mng", "svg"],
Self::Videos => vec!["webm", "mp4", "mkv", "mov", "avi", "m4v", "wmv", "bik", "ogv", "qt", "3gp", "3g2", "divx"],
Self::Audio => vec!["ogg", "oga", "opus", "mp3", "m4a", "aac", "flac", "ape", "midi", "mid", "alac", "wav",
"aiff", "aa3", "at3"],
Self::Documents => vec!["doc", "docx", "ppt", "pptx", "xls", "xlsx", "csv", "tsv", "pdf", "odt", "ods", "odp"],
Self::Images => vec![
"png", "jpg", "jpeg", "webp", "raw", "gif", "apng", "tga", "bmp", "tif", "tiff", "heif", "avif", "jp2", "mng",
"svg",
],
Self::Videos => vec![
"webm", "mp4", "mkv", "mov", "avi", "m4v", "wmv", "bik", "ogv", "qt", "3gp", "3g2", "divx",
],
Self::Audio => vec![
"ogg", "oga", "opus", "mp3", "m4a", "aac", "flac", "ape", "midi", "mid", "alac", "wav", "aiff", "aa3", "at3",
],
Self::Documents => vec![
"doc", "docx", "ppt", "pptx", "xls", "xlsx", "csv", "tsv", "pdf", "odt", "ods", "odp",
],
Self::Archives => vec!["zip", "tar", "gz", "zst", "xz", "rar", "7z", "bz", "bz2"],
_ => todo!()
_ => todo!(),
}
}
}

View File

@ -23,7 +23,7 @@ fn write_pathbuf<W: Write>(f: &mut W, path: &PathBuf) -> io::Result<()> {
f.write_all(&*path.as_os_str().as_bytes())?;
#[cfg(windows)]
write!(f, "{}", path.as_os_str().to_string_lossy())?; // TODO: implement bonked strings for windows
// f.write_all(&*path.as_os_str().encode_wide().collect::<Vec<u16>>())?;
// f.write_all(&*path.as_os_str().encode_wide().collect::<Vec<u16>>())?;
write!(f, "'")
}
}
@ -91,7 +91,7 @@ impl Format for Script {
fn no_known_extension<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
write!(f, "echo No known extension for ")?;
write_pathbuf(f, path)?;
writeln!(f, )
writeln!(f,)
}
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {

View File

@ -35,7 +35,7 @@ pub fn mime_type<T: MimeDb>(db: &T, path: &Path) -> io::Result<Option<Mime>> {
// this lint can be ignored: it's okay if the file isn't long enough to fill the buffer, as we only care about the
// first few bytes for the purpose of mime sniffing
#[allow(clippy::unused_io_amount)]
file.read(&mut buffer)?;
file.read(&mut buffer)?;
let r = db.get_type(&buffer);

View File

@ -18,7 +18,7 @@ use std::io::{stdout, BufWriter};
use std::path::{Path, PathBuf};
use clap::Clap;
use log::{debug, info, trace, warn, error};
use log::{debug, error, info, trace, warn};
use once_cell::sync::OnceCell;
#[cfg(feature = "multi-threaded")]
use rayon::prelude::*;
@ -32,13 +32,13 @@ use crate::parameters::OutputFormat;
use crate::scanerror::ScanError;
use std::process::exit;
mod extensionset;
mod findings;
mod formats;
mod inspectors;
mod mimedb;
mod parameters;
mod scanerror;
mod extensionset;
#[cfg(feature = "infer-backend")]
static MIMEDB: OnceCell<mimedb::InferDb> = OnceCell::new();
@ -179,10 +179,7 @@ fn main() {
debug!("Iterating directory: {:?}", args.dirs);
let extensions: Vec<&str> = if let Some(exts) = &args.exts {
exts
.iter()
.map(|s| s.as_str())
.collect()
exts.iter().map(|s| s.as_str()).collect()
} else if let Some(exts) = &args.ext_set {
exts.extensions().to_vec()
} else {
@ -192,16 +189,14 @@ fn main() {
debug!("Checking files with extensions: {:?}", extensions);
let stepper = WalkDir::new(&args.dirs).into_iter();
let mut probably_fatal_error= false;
let mut probably_fatal_error = false;
let entries: Vec<DirEntry> = stepper
.filter_entry(|e| wanted_file(&args, &extensions, e)) // filter out unwanted files
.filter_map(|e| {
if let Err(err) = &e {
debug!("uh oh spaghettio!! {:#?}", e);
// log errors to stdout, and remove them from the iterator
let path = err
.path()
.map_or("General error".into(), Path::to_string_lossy);
let path = err.path().map_or("General error".into(), Path::to_string_lossy);
if err.depth() == 0 {
// if something goes wrong while trying to read the root directory, we're probably not going to get much done
@ -211,8 +206,12 @@ fn main() {
// TODO: is there a way to just say `map_or(x, |y| y).thing()` instead of `map_or(x.thing(), |y| y.thing())`?
// i don't care whether i'm returning a walkdir error or an io error, i just care about whether or not it
// implements ToString (which they both do). map_or doesn't work on trait objects though :(
error!("{}: {}", path, err.io_error().map_or(err.to_string(), |e|e.to_string()));
return None
error!(
"{}: {}",
path,
err.io_error().map_or(err.to_string(), |e| e.to_string())
);
return None;
}
e.ok()
})
@ -233,13 +232,12 @@ fn main() {
trace!("Found {} items to check", entries.len());
let results: Vec<_> = scan_from_walkdir(entries)
let results: Vec<_> = scan_from_walkdir(entries)
.into_iter()
.filter(|result|
result.is_err()
|| !result.as_ref().unwrap().valid
// TODO: find a way to trace! the valid files without doing ↓
// || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true }
.filter(
|result| result.is_err() || !result.as_ref().unwrap().valid,
// TODO: find a way to trace! the valid files without doing ↓
// || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true }
)
.collect();
@ -256,15 +254,14 @@ fn main() {
}
}
if results.is_empty() { info!("All files have valid extensions!") }
if results.is_empty() {
info!("All files have valid extensions!")
}
match args.output_format {
OutputFormat::Script => {
let s = Script::new();
if s.write_all(
&results,
&mut BufWriter::new(stdout().lock())
).is_err() {
if s.write_all(&results, &mut BufWriter::new(stdout().lock())).is_err() {
exit(exitcode::IOERR);
}
}

View File

@ -20,8 +20,7 @@ impl MimeDb for InferDb {
// jpeg2000 support because why the stinch not
info.add("image/jpeg2000", ".jp2", |buf| {
buf.len() > 23
&& buf[..23] == b"\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A\x6A\x70\x32\x20"[..]
buf.len() > 23 && buf[..23] == b"\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A\x6A\x70\x32\x20"[..]
});
info.add("image/svg+xml", "svg", |buf| {
@ -35,19 +34,14 @@ impl MimeDb for InferDb {
// whitespace (according to https://www.w3.org/TR/xml/#NT-S)
b'\t' | b'\r' | b'\n' | b'\x20' => continue,
b'<' => break,
_ => return false
_ => return false,
}
}
// finally, to check whether or not the file is an SVG:
// - split the buffer up into chunks separated by the less than sign
// - check to see if this chunk starts with any of these identifiers:
let identifiers: Vec<&[u8]> = vec![
b"svg",
b"SVG",
b"!DOCTYPE svg",
b"!DOCTYPE SVG"
];
let identifiers: Vec<&[u8]> = vec![b"svg", b"SVG", b"!DOCTYPE svg", b"!DOCTYPE SVG"];
// - if it does, the nested `any` will short circuit and immediately return true, causing the parent `any` to do
// the same
// - and finally, if none of the chunks match, we'll return false
@ -55,11 +49,7 @@ impl MimeDb for InferDb {
// TODO: this is kind of messy, i'd like to clean it up somehow :(
buf
.split(|c| *c == b'<')
.any(|buf| {
identifiers
.iter()
.any(|id| buf.starts_with(id))
})
.any(|buf| identifiers.iter().any(|id| buf.starts_with(id)))
});
// unmut

View File

@ -1,8 +1,8 @@
use std::path::PathBuf;
use crate::extensionset::ExtensionSet;
use clap::Clap;
use smartstring::{LazyCompact, SmartString};
use crate::extensionset::ExtensionSet;
#[derive(Clap, PartialEq, Debug)]
pub enum OutputFormat {
@ -13,7 +13,13 @@ pub enum OutputFormat {
#[derive(Clap, Debug)]
pub struct Parameters {
/// Only examine files with these extensions (Comma-separated list)
#[clap(short, long, use_delimiter = true, require_delimiter = true, required_unless_present = "ext-set")]
#[clap(
short,
long,
use_delimiter = true,
require_delimiter = true,
required_unless_present = "ext-set"
)]
pub exts: Option<Vec<SmartString<LazyCompact>>>,
/// write good docs 0uo