fif/src/formats.rs

121 lines
3.4 KiB
Rust
Raw Normal View History

use std::io::{self, Write};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use snailquote::escape;
use crate::scanerror::ScanError;
2021-02-18 09:50:22 +00:00
use crate::Findings;
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
type Entries = [Result<Findings, (ScanError, PathBuf)>];
fn write_pathbuf<W: Write>(f: &mut W, path: &PathBuf) -> io::Result<()> {
match path.to_str() {
2021-02-18 09:48:38 +00:00
Some(string) => {
write!(f, "{}", escape(string))
}
None => {
write!(f, "'")?;
#[cfg(unix)]
2021-02-18 09:48:38 +00:00
f.write_all(&*path.as_os_str().as_bytes())?;
#[cfg(windows)]
2021-02-19 17:57:19 +00:00
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, "'")
}
}
}
pub trait Format {
fn new() -> Self;
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()>;
fn no_known_extension<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
fn unknown_type<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()>;
fn header<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<()> {
// TODO: clean this up - it's horrifying
self.header(entries, f)?;
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 - check for known extension
if let Some(ext) = finding.recommended_extension() {
2021-02-18 09:48:38 +00:00
self.rename(f, &finding.file, &finding.file.with_extension(ext.as_str()))?
} else {
self.no_known_extension(f, &finding.file)?
}
}
}
Err(error) => {
// something went wrong 0uo
match error.0 {
// failed to read the file
ScanError::File => self.unreadable(f, &error.1)?,
// file was read successfully, but we couldn't determine a mimetype
2021-02-18 09:48:38 +00:00
ScanError::Mime => self.unknown_type(f, &error.1)?,
}
}
}
}
self.footer(entries, f)
}
}
// TODO: maybe make a batch script version for windows
pub struct Script {}
impl Format for Script {
2021-02-18 09:48:38 +00:00
fn new() -> Self {
Self {}
}
fn rename<W: Write>(&self, f: &mut W, from: &PathBuf, to: &PathBuf) -> io::Result<()> {
// TODO: surely there's a better way...
write!(f, "mv -v -i -- ")?;
write_pathbuf(f, from)?;
write!(f, " ")?;
write_pathbuf(f, to)?;
2021-02-18 09:48:38 +00:00
writeln!(f,)
}
fn no_known_extension<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
write!(f, "printf No known extension for ")?;
write_pathbuf(f, path)?;
writeln!(f,"\nprintf '\n'")
}
fn unreadable<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
write!(f, "# Failed to read ")?;
write_pathbuf(f, path)?;
2021-02-18 09:48:38 +00:00
writeln!(f,)
}
fn unknown_type<W: Write>(&self, f: &mut W, path: &PathBuf) -> io::Result<()> {
write!(f, "# Failed to detect mime type for ")?;
write_pathbuf(f, path)?;
2021-02-18 09:48:38 +00:00
writeln!(f,)
}
fn header<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
2021-02-18 09:48:38 +00:00
write!(
f,
"#!/usr/bin/env sh\n# Generated by fif {}.\n\nset -e\n\n",
2021-02-18 09:48:38 +00:00
VERSION.unwrap_or("???")
)
}
fn footer<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
writeln!(f, "\nprintf 'Done.\\n'")
}
2021-02-18 09:48:38 +00:00
}