use std::io; use std::io::Write; use crate::Findings; use crate::scanerror::ScanError; use std::path::PathBuf; use snailquote::escape; type Entries = [Result]; pub trait Format { fn new() -> Self; fn rename(&self, f: &mut T, from: &str, to: &str) -> io::Result<()>; fn no_known_extension(&self, f: &mut T, path: &str) -> io::Result<()>; fn unreadable(&self, f: &mut T, path: &str) -> io::Result<()>; fn unknown_type(&self, f: &mut T, path: &str) -> io::Result<()>; fn write_all(&self, entries: &Entries, f: &mut T) -> io::Result<()> { // TODO: clean this up - it's horrifying for entry in entries { match entry { Ok(finding) => { // the file was successfully scanned, and a mimetype was detected if !finding.valid { // the file's extension is wrong! match finding.recommended_extension() { Some(ext) => { // there's a known extension for this mimetype!! self.rename( f, &finding.file.to_string_lossy(), &finding.file.with_extension(ext.as_str()).to_string_lossy() )? } None => { // unfortunately, there's no known extension for this mimetype :( self.no_known_extension(f, &finding.file.to_string_lossy())? } } } } Err(error) => { // something went wrong 0uo match error.0 { // failed to read the file ScanError::File => self.unreadable(f, &error.1.to_string_lossy())?, // file was read successfully, but we couldn't determine a mimetype ScanError::Mime => self.unknown_type(f, &error.1.to_string_lossy())? } } } } Ok(()) } } pub struct Script {} impl Format for Script { fn new() -> Self { return Script {} } fn rename(&self, f: &mut T, from: &str, to: &str) -> io::Result<()> { // TODO: string escaping aaaaaaAAAAAAAAAA writeln!(f, "mv -v -i -- {} {}", escape(from), escape(to)) } fn no_known_extension(&self, f: &mut T, path: &str) -> io::Result<()> { writeln!(f, "echo No known extension for {}!", escape(path)) } fn unreadable(&self, f: &mut T, path: &str) -> io::Result<()> { writeln!(f, "# Failed to read {}", escape(path)) } fn unknown_type(&self, f: &mut T, path: &str) -> io::Result<()> { writeln!(f, "# Failed to detect mime type for {}", escape(path)) } }