output is now sorted 0u0

sort order: files that couldn't be read, then files with no known mimetype, then files with no known extensions, then files with the wrong extension
This commit is contained in:
Lynne Megido 2021-04-20 18:52:49 +10:00
parent b877d7d65e
commit 255665cae0
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
7 changed files with 55 additions and 23 deletions

View File

@ -9,6 +9,8 @@ Dates are given in YYYY-MM-DD format.
- Added .rpa (Ren'Py archive) support to infer backend - Added .rpa (Ren'Py archive) support to infer backend
- Added system extension set - Added system extension set
- [`xdg-mime`] no longer uses git version - [`xdg-mime`] no longer uses git version
- Output is sorted: Files that couldn't be read, then files with no known mimetype, then files with no known extensions,
then files with the wrong extension
### v0.2.12 (2021-04-14) ### v0.2.12 (2021-04-14)
#### Features #### Features

10
Cargo.lock generated
View File

@ -202,6 +202,7 @@ dependencies = [
"exitcode", "exitcode",
"fastrand", "fastrand",
"infer", "infer",
"itertools",
"log", "log",
"mime_guess", "mime_guess",
"once_cell", "once_cell",
@ -282,6 +283,15 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "itertools"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319"
dependencies = [
"either",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"

View File

@ -33,6 +33,7 @@ infer = "0.4.0"
rayon = { version = "1.5.0", optional = true } rayon = { version = "1.5.0", optional = true }
exitcode = "1.1.2" exitcode = "1.1.2"
cfg-if = "1.0.0" cfg-if = "1.0.0"
itertools = "0.10.0"
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
xdg-mime = "0.3.3" xdg-mime = "0.3.3"
@ -68,6 +69,9 @@ lto = "thin"
[profile.test] [profile.test]
opt-level = 0 opt-level = 0
# optimise dependencies, even when producing debug builds # optimise dependencies, even when producing debug and test builds
[profile.dev.package."*"] [profile.dev.package."*"]
opt-level = 3 opt-level = 3
[profile.test.package."*"]
opt-level = 3

View File

@ -6,6 +6,7 @@ use crate::inspectors::mime_extension_lookup;
use crate::string_type::String; use crate::string_type::String;
/// Information about a scanned file. /// Information about a scanned file.
#[derive(Ord, PartialOrd, Eq, PartialEq)]
pub struct Findings<'a> { pub struct Findings<'a> {
/// The location of the scanned file. /// The location of the scanned file.
pub file: &'a Path, pub file: &'a Path,

View File

@ -10,6 +10,7 @@ use snailquote::escape;
use crate::scan_error::ScanError; use crate::scan_error::ScanError;
use crate::{Findings, BACKEND}; use crate::{Findings, BACKEND};
use itertools::Itertools;
/// The current version of fif, as defined in Cargo.toml. /// The current version of fif, as defined in Cargo.toml.
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
@ -83,25 +84,36 @@ pub trait Format {
// TODO: clean this up - it's kinda messy // TODO: clean this up - it's kinda messy
self.header(entries, f)?; self.header(entries, f)?;
for entry in entries { // output will be generated in the order:
match entry { // - files that couldn't be read
Ok(finding) => { // - files with no known mime type
if let Some(ext) = finding.recommended_extension() { // - files with no known extension
self.rename(f, finding.file, &finding.file.with_extension(ext.as_str()))? // - files with a known extension
} else { // files that already have a correct extension won't be represented in the output.
self.no_known_extension(f, finding.file)?
}
}
Err(error) => { // sort errors so unreadable files appear before files with unknown mimetypes - ScanError impls Ord such that
// something went wrong 0uo // ScanError::File > ScanError::Mime
match error { let errors = entries.iter().filter_map(|e| e.as_ref().err()).sorted();
// failed to read the file // sort files so that files with no known extension come before those with known extensions - None > Some("jpg")
ScanError::File(path) => self.unreadable(f, path)?, let findings = entries
// file was read successfully, but we couldn't determine a mimetype .iter()
ScanError::Mime(path) => self.unknown_type(f, path)?, .filter_map(|e| e.as_ref().ok())
} .sorted_by(|a, b| b.recommended_extension().cmp(&a.recommended_extension()).reverse());
}
for error in errors {
match error {
// failed to read the file
ScanError::File(path) => self.unreadable(f, path)?,
// file was read successfully, but we couldn't determine a mimetype
ScanError::Mime(path) => self.unknown_type(f, path)?,
}
}
for finding in findings {
if let Some(ext) = finding.recommended_extension() {
self.rename(f, finding.file, &finding.file.with_extension(ext.as_str()))?
} else {
self.no_known_extension(f, finding.file)?
} }
} }

View File

@ -18,7 +18,7 @@
#![warn(trivial_casts, unused_lifetimes, unused_qualifications)] #![warn(trivial_casts, unused_lifetimes, unused_qualifications)]
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io::{stdout, BufWriter}; use std::io::{stdout, BufWriter, Write};
use std::path::Path; use std::path::Path;
use std::process::exit; use std::process::exit;
@ -130,8 +130,6 @@ fn main() {
exit(0); exit(0);
} }
// TODO: sort entries in order of: valid moves, unknown mime, unknown ext, error
let mut buffered_stdout = BufWriter::new(stdout()); let mut buffered_stdout = BufWriter::new(stdout());
let result = match args.output_format { let result = match args.output_format {
@ -145,6 +143,11 @@ fn main() {
exit(exitcode::IOERR); exit(exitcode::IOERR);
} }
if buffered_stdout.flush().is_err() {
error!("Failed to flush stdout.");
exit(exitcode::IOERR);
}
debug!("Done"); debug!("Done");
} }

View File

@ -1,7 +1,7 @@
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter, Result};
use std::path::Path; use std::path::Path;
#[derive(Debug)] #[derive(Debug, PartialEq, PartialOrd, Ord, Eq)]
pub enum ScanError<'a> { pub enum ScanError<'a> {
/// Something went wrong while trying to read the given file. /// Something went wrong while trying to read the given file.
File(&'a Path), File(&'a Path),