cleaned up formats.rs
This commit is contained in:
parent
729576b803
commit
b95c8ec85c
1 changed files with 72 additions and 37 deletions
|
@ -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> {
|
||||||
|
String(&'a str),
|
||||||
|
Path(&'a PathBuf),
|
||||||
|
Space,
|
||||||
|
Newline
|
||||||
|
}
|
||||||
|
|
||||||
|
// the lifetime of a lifetime
|
||||||
|
impl<'a> From<&'a str> for Writable<'a> {
|
||||||
|
fn from(s: &'a str) -> Writable<'a> {
|
||||||
|
Writable::String(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
match path.to_str() {
|
||||||
Some(string) => {
|
Some(string) => {
|
||||||
write!(f, "{}", escape(string))
|
write!(f, "{}", escape(string))?
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
write!(f, "'")?;
|
write!(f, "'''")?;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
f.write_all(&*path.as_os_str().as_bytes())?;
|
f.write_all(&*path.as_os_str().as_bytes())?;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
write!(f, "{}", path.as_os_str().to_string_lossy())?; // TODO: implement bonked strings for 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>>())?;
|
// f.write_all(&*path.as_os_str().encode_wide().collect::<Vec<u16>>())?;
|
||||||
write!(f, "'")
|
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 !finding.valid {
|
|
||||||
// the file's extension is wrong - check for known extension
|
|
||||||
if let Some(ext) = finding.recommended_extension() {
|
if let Some(ext) = finding.recommended_extension() {
|
||||||
self.rename(f, &finding.file, &finding.file.with_extension(ext.as_str()))?
|
self.rename(f, &finding.file, &finding.file.with_extension(ext.as_str()))?
|
||||||
} else {
|
} else {
|
||||||
self.no_known_extension(f, &finding.file)?
|
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<()> {
|
||||||
|
|
Loading…
Reference in a new issue