cleaned up formats.rs

This commit is contained in:
Lynne Megido 2021-02-22 00:46:51 +10:00
parent 729576b803
commit b95c8ec85c
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90

View file

@ -12,21 +12,52 @@ const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
type Entries = [Result<Findings, (ScanError, PathBuf)>]; type Entries = [Result<Findings, (ScanError, PathBuf)>];
fn write_pathbuf<W: Write>(f: &mut W, path: &PathBuf) -> io::Result<()> { enum Writable<'a> {
match path.to_str() { String(&'a str),
Some(string) => { Path(&'a PathBuf),
write!(f, "{}", escape(string)) Space,
} Newline
None => { }
write!(f, "'")?;
#[cfg(unix)] // the lifetime of a lifetime
f.write_all(&*path.as_os_str().as_bytes())?; impl<'a> From<&'a str> for Writable<'a> {
#[cfg(windows)] fn from(s: &'a str) -> Writable<'a> {
write!(f, "{}", path.as_os_str().to_string_lossy())?; // TODO: implement bonked strings for windows Writable::String(s)
// f.write_all(&*path.as_os_str().encode_wide().collect::<Vec<u16>>())?; }
write!(f, "'") }
impl<'a> From<&'a PathBuf> for Writable<'a> {
fn from(p: &'a PathBuf) -> Writable<'a> {
Writable::Path(p)
}
}
fn smart_write<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
// ehhhh
for writeable in writeables {
match writeable {
Writable::Space => write!(f, " ")?,
Writable::Newline => writeln!(f, )?,
Writable::String(s) => write!(f, "{}", s)?,
Writable::Path(path) => {
match path.to_str() {
Some(string) => {
write!(f, "{}", escape(string))?
}
None => {
write!(f, "'''")?;
#[cfg(unix)]
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>>())?;
write!(f, "'''")?
}
}
}
} }
} }
Ok(())
} }
pub trait Format { pub trait Format {
@ -39,22 +70,19 @@ pub trait Format {
fn footer<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>; fn footer<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
fn write_all<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()> { fn write_all<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()> {
// TODO: clean this up - it's horrifying // TODO: clean this up - it's kinda messy
self.header(entries, f)?; self.header(entries, f)?;
for entry in entries { for entry in entries {
match entry { match entry {
Ok(finding) => { Ok(finding) => {
// the file was successfully scanned, and a mimetype was detected if let Some(ext) = finding.recommended_extension() {
if !finding.valid { self.rename(f, &finding.file, &finding.file.with_extension(ext.as_str()))?
// the file's extension is wrong - check for known extension } else {
if let Some(ext) = finding.recommended_extension() { self.no_known_extension(f, &finding.file)?
self.rename(f, &finding.file, &finding.file.with_extension(ext.as_str()))?
} else {
self.no_known_extension(f, &finding.file)?
}
} }
} }
Err(error) => { Err(error) => {
// something went wrong 0uo // something went wrong 0uo
match error.0 { match error.0 {
@ -80,30 +108,37 @@ impl Format for Script {
} }
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()> { fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()> {
// TODO: surely there's a better way... smart_write(f, &[
write!(f, "mv -v -i -- ")?; "mv -v -i -- ".into(),
write_pathbuf(f, from)?; from.into(),
write!(f, " ")?; Writable::Space,
write_pathbuf(f, to)?; to.into(),
writeln!(f,) Writable::Newline
])
} }
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: &PathBuf) -> io::Result<()> {
write!(f, "echo No known extension for ")?; smart_write(f, &[
write_pathbuf(f, path)?; "echo No known extension for ".into(),
writeln!(f,) 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: &PathBuf) -> io::Result<()> {
write!(f, "# Failed to read ")?; smart_write(f, &[
write_pathbuf(f, path)?; "# Failed to read ".into(),
writeln!(f,) 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: &PathBuf) -> io::Result<()> {
write!(f, "# Failed to detect mime type for ")?; smart_write(f, &[
write_pathbuf(f, path)?; "# Failed to detect mime type for ".into(),
writeln!(f,) path.into(),
Writable::Newline
])
} }
fn header<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> { fn header<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {