new and improved rustfmt

This commit is contained in:
Lynne Megido 2021-05-06 09:06:05 +10:00
parent b93e111357
commit 0c0d3f12ea
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
5 changed files with 35 additions and 43 deletions

View file

@ -1,4 +1,5 @@
max_width = 120
fn_single_line = true
hard_tabs = true
tab_spaces = 2
newline_style = "Unix"

View file

@ -6,7 +6,7 @@ use crate::inspectors::mime_extension_lookup;
use crate::string_type::String;
#[cfg(feature = "json")]
use serde::{Serializer, ser::SerializeStruct};
use serde::{ser::SerializeStruct, Serializer};
/// Information about a scanned file.
#[derive(Ord, PartialOrd, Eq, PartialEq)]
@ -21,8 +21,10 @@ pub struct Findings<'a> {
#[cfg(feature = "json")]
impl<'a> serde::Serialize for Findings<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: Serializer {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
// the second parameter is the number of fields in the struct -- in this case, 3
let mut state = serializer.serialize_struct("Findings", 3)?;

View file

@ -13,7 +13,6 @@ use crate::scan_error::ScanError;
use crate::{Findings, BACKEND};
use itertools::Itertools;
/// A macro for creating an array of `Writable`s without needing to pepper your code with `into()`s.
/// # Usage
/// ```
@ -66,26 +65,18 @@ pub enum Writable<'a> {
// the lifetime of a lifetime
impl<'a> From<&'a str> for Writable<'a> {
fn from(s: &'a str) -> Writable<'a> {
Writable::String(s)
}
fn from(s: &'a str) -> Writable<'a> { Writable::String(s) }
}
impl<'a> From<&'a Path> for Writable<'a> {
fn from(p: &'a Path) -> Writable<'a> {
Writable::Path(p)
}
fn from(p: &'a Path) -> Writable<'a> { Writable::Path(p) }
}
impl<'a> From<&'a OsStr> for Writable<'a> {
fn from(p: &'a OsStr) -> Writable<'a> {
Writable::Path(p.as_ref())
}
fn from(p: &'a OsStr) -> Writable<'a> { Writable::Path(p.as_ref()) }
}
fn generated_by() -> String {
format!("Generated by fif {} ({} backend)", VERSION.unwrap_or("???"), BACKEND)
}
fn generated_by() -> String { format!("Generated by fif {} ({} backend)", VERSION.unwrap_or("???"), BACKEND) }
fn smart_write<W: Write>(f: &mut W, writeables: &[Writable]) -> io::Result<()> {
// ehhhh
@ -194,9 +185,7 @@ pub trait Format {
pub struct Shell {}
impl Format for Shell {
fn new() -> Self {
Self {}
}
fn new() -> Self { Self {} }
fn rename<W: Write>(&self, f: &mut W, from: &Path, to: &Path) -> io::Result<()> {
smart_write(f, writablesln!("mv -v -i -- ", from, Space, to))
@ -238,9 +227,7 @@ impl Format for Shell {
pub struct PowerShell {}
impl Format for PowerShell {
fn new() -> Self {
Self {}
}
fn new() -> Self { Self {} }
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
@ -309,16 +296,19 @@ impl Format for Json {
findings: &'a Vec<&'a Findings<'a>>,
}
let result = serde_json::to_writer_pretty(f, &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()
});
let result = serde_json::to_writer_pretty(
f,
&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 {
log::error!("Error while serialising: {}", err);
return Err(err.into())
return Err(err.into());
}
Ok(())
}
}
}

View file

@ -118,7 +118,7 @@ fn main() {
.filter(
|result| result.is_err() || !result.as_ref().unwrap().valid,
// 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();
@ -230,9 +230,7 @@ fn wanted_file(
}
/// Given a file path, returns its extension, using [`std::path::Path::extension`].
fn extension_from_path(path: &Path) -> Option<&OsStr> {
path.extension()
}
fn extension_from_path(path: &Path) -> Option<&OsStr> { path.extension() }
/// Inspects the given entry, returning a [`Findings`] on success and a [`ScanError`] on failure.
///

View file

@ -1,5 +1,5 @@
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::mime_db::MimeDb;
use crate::string_type::String;
@ -335,18 +335,17 @@ fn outputs_move_commands() {
valid: false,
mime: IMAGE_JPEG,
})];
for format in &["Shell", "PowerShell"] {
let mut cursor = std::io::Cursor::new(Vec::new());
let mut contents = std::string::String::new();
match *format {
"Shell" => Shell::new()
.write_all(&mut cursor, &entries),
"PowerShell" => PowerShell::new()
.write_all(&mut cursor, &entries),
_ => unreachable!()
}.expect("Failed to write to cursor");
"Shell" => Shell::new().write_all(&mut cursor, &entries),
"PowerShell" => PowerShell::new().write_all(&mut cursor, &entries),
_ => unreachable!(),
}
.expect("Failed to write to cursor");
cursor.set_position(0);
cursor
@ -366,8 +365,8 @@ fn outputs_move_commands() {
#[test]
/// Ensure JSON output is valid.
fn test_json() {
use std::io::Read;
use crate::formats::Json;
use std::io::Read;
// create an example finding stating that "misnamed_file.png" has been identified as a jpeg file
let entries = vec![Ok(Findings {
file: Path::new("misnamed_file.png"),
@ -378,7 +377,9 @@ fn test_json() {
let mut cursor = std::io::Cursor::new(Vec::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