new and improved rustfmt
This commit is contained in:
parent
b93e111357
commit
0c0d3f12ea
5 changed files with 35 additions and 43 deletions
|
@ -1,4 +1,5 @@
|
||||||
max_width = 120
|
max_width = 120
|
||||||
|
fn_single_line = true
|
||||||
hard_tabs = true
|
hard_tabs = true
|
||||||
tab_spaces = 2
|
tab_spaces = 2
|
||||||
newline_style = "Unix"
|
newline_style = "Unix"
|
|
@ -6,7 +6,7 @@ use crate::inspectors::mime_extension_lookup;
|
||||||
use crate::string_type::String;
|
use crate::string_type::String;
|
||||||
|
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
use serde::{Serializer, ser::SerializeStruct};
|
use serde::{ser::SerializeStruct, Serializer};
|
||||||
|
|
||||||
/// Information about a scanned file.
|
/// Information about a scanned file.
|
||||||
#[derive(Ord, PartialOrd, Eq, PartialEq)]
|
#[derive(Ord, PartialOrd, Eq, PartialEq)]
|
||||||
|
@ -21,8 +21,10 @@ pub struct Findings<'a> {
|
||||||
|
|
||||||
#[cfg(feature = "json")]
|
#[cfg(feature = "json")]
|
||||||
impl<'a> serde::Serialize for Findings<'a> {
|
impl<'a> serde::Serialize for Findings<'a> {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
S: Serializer {
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
// the second parameter is the number of fields in the struct -- in this case, 3
|
// the second parameter is the number of fields in the struct -- in this case, 3
|
||||||
let mut state = serializer.serialize_struct("Findings", 3)?;
|
let mut state = serializer.serialize_struct("Findings", 3)?;
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@ use crate::scan_error::ScanError;
|
||||||
use crate::{Findings, BACKEND};
|
use crate::{Findings, BACKEND};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
|
||||||
/// A macro for creating an array of `Writable`s without needing to pepper your code with `into()`s.
|
/// A macro for creating an array of `Writable`s without needing to pepper your code with `into()`s.
|
||||||
/// # Usage
|
/// # Usage
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -66,26 +65,18 @@ pub enum Writable<'a> {
|
||||||
|
|
||||||
// the lifetime of a lifetime
|
// the lifetime of a lifetime
|
||||||
impl<'a> From<&'a str> for Writable<'a> {
|
impl<'a> From<&'a str> for Writable<'a> {
|
||||||
fn from(s: &'a str) -> Writable<'a> {
|
fn from(s: &'a str) -> Writable<'a> { Writable::String(s) }
|
||||||
Writable::String(s)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a Path> for Writable<'a> {
|
impl<'a> From<&'a Path> for Writable<'a> {
|
||||||
fn from(p: &'a Path) -> Writable<'a> {
|
fn from(p: &'a Path) -> Writable<'a> { Writable::Path(p) }
|
||||||
Writable::Path(p)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a OsStr> for Writable<'a> {
|
impl<'a> From<&'a OsStr> for Writable<'a> {
|
||||||
fn from(p: &'a OsStr) -> Writable<'a> {
|
fn from(p: &'a OsStr) -> Writable<'a> { Writable::Path(p.as_ref()) }
|
||||||
Writable::Path(p.as_ref())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generated_by() -> String {
|
fn generated_by() -> String { format!("Generated by fif {} ({} backend)", VERSION.unwrap_or("???"), BACKEND) }
|
||||||
format!("Generated by fif {} ({} backend)", VERSION.unwrap_or("???"), BACKEND)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn smart_write<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
|
fn smart_write<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
|
||||||
// ehhhh
|
// ehhhh
|
||||||
|
@ -194,9 +185,7 @@ pub trait Format {
|
||||||
pub struct Shell {}
|
pub struct Shell {}
|
||||||
|
|
||||||
impl Format for Shell {
|
impl Format for Shell {
|
||||||
fn new() -> Self {
|
fn new() -> Self { Self {} }
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
|
fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
|
||||||
smart_write(f, writablesln!("mv -v -i -- ", from, Space, to))
|
smart_write(f, writablesln!("mv -v -i -- ", from, Space, to))
|
||||||
|
@ -238,9 +227,7 @@ impl Format for Shell {
|
||||||
pub struct PowerShell {}
|
pub struct PowerShell {}
|
||||||
|
|
||||||
impl Format for PowerShell {
|
impl Format for PowerShell {
|
||||||
fn new() -> Self {
|
fn new() -> Self { Self {} }
|
||||||
Self {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
|
fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
|
||||||
// unfortunately there doesn't seem to be an equivalent of sh's `mv -i` -- passing the '-Confirm' flag will prompt
|
// unfortunately there doesn't seem to be an equivalent of sh's `mv -i` -- passing the '-Confirm' flag will prompt
|
||||||
|
@ -309,16 +296,19 @@ impl Format for Json {
|
||||||
findings: &'a Vec<&'a Findings<'a>>,
|
findings: &'a Vec<&'a Findings<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = serde_json::to_writer_pretty(f, &SerdeEntries {
|
let result = serde_json::to_writer_pretty(
|
||||||
errors: &entries.iter().filter_map(|e| e.as_ref().err()).sorted().collect(),
|
f,
|
||||||
findings: &entries.iter().filter_map(|f| f.as_ref().ok()).sorted().collect()
|
&SerdeEntries {
|
||||||
});
|
errors: &entries.iter().filter_map(|e| e.as_ref().err()).sorted().collect(),
|
||||||
|
findings: &entries.iter().filter_map(|f| f.as_ref().ok()).sorted().collect(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
log::error!("Error while serialising: {}", err);
|
log::error!("Error while serialising: {}", err);
|
||||||
return Err(err.into())
|
return Err(err.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ fn main() {
|
||||||
.filter(
|
.filter(
|
||||||
|result| result.is_err() || !result.as_ref().unwrap().valid,
|
|result| result.is_err() || !result.as_ref().unwrap().valid,
|
||||||
// TODO: find a way to trace! the valid files without doing ↓
|
// TODO: find a way to trace! the valid files without doing ↓
|
||||||
// || if result.as_ref().unwrap().valid { trace!("{:?} is fine", result.as_ref().unwrap().file); false } else { true }
|
// || if result.as_ref().unwrap().valid { trace!("{:?} ok", result.as_ref().unwrap().file); false } else { true }
|
||||||
)
|
)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
@ -230,9 +230,7 @@ fn wanted_file(
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a file path, returns its extension, using [`std::path::Path::extension`].
|
/// Given a file path, returns its extension, using [`std::path::Path::extension`].
|
||||||
fn extension_from_path(path: &Path) -> Option<&OsStr> {
|
fn extension_from_path(path: &Path) -> Option<&OsStr> { path.extension() }
|
||||||
path.extension()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Inspects the given entry, returning a [`Findings`] on success and a [`ScanError`] on failure.
|
/// Inspects the given entry, returning a [`Findings`] on success and a [`ScanError`] on failure.
|
||||||
///
|
///
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::findings::Findings;
|
use crate::findings::Findings;
|
||||||
use crate::formats::{Format, Shell, PowerShell};
|
use crate::formats::{Format, PowerShell, Shell};
|
||||||
use crate::inspectors::{mime_extension_lookup, BUF_SIZE};
|
use crate::inspectors::{mime_extension_lookup, BUF_SIZE};
|
||||||
use crate::mime_db::MimeDb;
|
use crate::mime_db::MimeDb;
|
||||||
use crate::string_type::String;
|
use crate::string_type::String;
|
||||||
|
@ -335,18 +335,17 @@ fn outputs_move_commands() {
|
||||||
valid: false,
|
valid: false,
|
||||||
mime: IMAGE_JPEG,
|
mime: IMAGE_JPEG,
|
||||||
})];
|
})];
|
||||||
|
|
||||||
for format in &["Shell", "PowerShell"] {
|
for format in &["Shell", "PowerShell"] {
|
||||||
let mut cursor = std::io::Cursor::new(Vec::new());
|
let mut cursor = std::io::Cursor::new(Vec::new());
|
||||||
let mut contents = std::string::String::new();
|
let mut contents = std::string::String::new();
|
||||||
|
|
||||||
match *format {
|
match *format {
|
||||||
"Shell" => Shell::new()
|
"Shell" => Shell::new().write_all(&mut cursor, &entries),
|
||||||
.write_all(&mut cursor, &entries),
|
"PowerShell" => PowerShell::new().write_all(&mut cursor, &entries),
|
||||||
"PowerShell" => PowerShell::new()
|
_ => unreachable!(),
|
||||||
.write_all(&mut cursor, &entries),
|
}
|
||||||
_ => unreachable!()
|
.expect("Failed to write to cursor");
|
||||||
}.expect("Failed to write to cursor");
|
|
||||||
|
|
||||||
cursor.set_position(0);
|
cursor.set_position(0);
|
||||||
cursor
|
cursor
|
||||||
|
@ -366,8 +365,8 @@ fn outputs_move_commands() {
|
||||||
#[test]
|
#[test]
|
||||||
/// Ensure JSON output is valid.
|
/// Ensure JSON output is valid.
|
||||||
fn test_json() {
|
fn test_json() {
|
||||||
use std::io::Read;
|
|
||||||
use crate::formats::Json;
|
use crate::formats::Json;
|
||||||
|
use std::io::Read;
|
||||||
// create an example finding stating that "misnamed_file.png" has been identified as a jpeg file
|
// create an example finding stating that "misnamed_file.png" has been identified as a jpeg file
|
||||||
let entries = vec![Ok(Findings {
|
let entries = vec![Ok(Findings {
|
||||||
file: Path::new("misnamed_file.png"),
|
file: Path::new("misnamed_file.png"),
|
||||||
|
@ -378,7 +377,9 @@ fn test_json() {
|
||||||
let mut cursor = std::io::Cursor::new(Vec::new());
|
let mut cursor = std::io::Cursor::new(Vec::new());
|
||||||
let mut contents = std::string::String::new();
|
let mut contents = std::string::String::new();
|
||||||
|
|
||||||
Json::new().write_all(&mut cursor, &entries).expect("Failed to write to cursor");
|
Json::new()
|
||||||
|
.write_all(&mut cursor, &entries)
|
||||||
|
.expect("Failed to write to cursor");
|
||||||
|
|
||||||
cursor.set_position(0);
|
cursor.set_position(0);
|
||||||
cursor
|
cursor
|
||||||
|
|
Loading…
Reference in a new issue