From 9091850ec58ea4635270e7dc67fcadd39a18b450 Mon Sep 17 00:00:00 2001 From: Lynne Date: Mon, 22 Feb 2021 00:15:09 +1000 Subject: [PATCH] thanks rustfmt --- src/extensionset.rs | 23 +++++++++++++++-------- src/formats.rs | 4 ++-- src/inspectors.rs | 2 +- src/main.rs | 43 ++++++++++++++++++++----------------------- src/mimedb.rs | 18 ++++-------------- src/parameters.rs | 10 ++++++++-- 6 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/extensionset.rs b/src/extensionset.rs index 644b69e..17c7cef 100644 --- a/src/extensionset.rs +++ b/src/extensionset.rs @@ -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!(), } } } diff --git a/src/formats.rs b/src/formats.rs index 258542e..983f8bf 100644 --- a/src/formats.rs +++ b/src/formats.rs @@ -23,7 +23,7 @@ fn write_pathbuf(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::>())?; + // f.write_all(&*path.as_os_str().encode_wide().collect::>())?; write!(f, "'") } } @@ -91,7 +91,7 @@ impl Format for Script { fn no_known_extension(&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(&self, f: &mut W, path: &PathBuf) -> io::Result<()> { diff --git a/src/inspectors.rs b/src/inspectors.rs index 7cdbbaa..bd59a59 100644 --- a/src/inspectors.rs +++ b/src/inspectors.rs @@ -35,7 +35,7 @@ pub fn mime_type(db: &T, path: &Path) -> io::Result> { // 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); diff --git a/src/main.rs b/src/main.rs index 89e66b1..993b284 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 = 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); } } diff --git a/src/mimedb.rs b/src/mimedb.rs index 2ee86ed..545b7ce 100644 --- a/src/mimedb.rs +++ b/src/mimedb.rs @@ -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 diff --git a/src/parameters.rs b/src/parameters.rs index 5b9bc97..7f29d26 100644 --- a/src/parameters.rs +++ b/src/parameters.rs @@ -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>>, /// write good docs 0uo