fif/src/findings.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

use std::path::Path;
2021-02-18 09:48:38 +00:00
use mime_guess::Mime;
2021-02-18 09:48:38 +00:00
use crate::inspectors::mime_extension_lookup;
2021-04-20 05:20:10 +00:00
use crate::string_type::String;
2021-02-18 09:48:38 +00:00
2021-05-05 22:57:42 +00:00
#[cfg(feature = "json")]
use serde::{Serializer, ser::SerializeStruct};
/// Information about a scanned file.
#[derive(Ord, PartialOrd, Eq, PartialEq)]
pub struct Findings<'a> {
/// The location of the scanned file.
pub file: &'a Path,
/// Whether or not the file's extension is valid for its mimetype.
pub valid: bool,
/// The file's mimetype.
pub mime: Mime,
}
2021-05-05 22:57:42 +00:00
#[cfg(feature = "json")]
impl<'a> serde::Serialize for Findings<'a> {
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)?;
state.serialize_field("file", &self.file)?;
state.serialize_field("valid", &self.valid)?;
state.serialize_field("mime", &self.mime.essence_str())?;
state.end()
}
}
impl<'a> Findings<'a> {
pub fn recommended_extension(&self) -> Option<String> {
2021-04-28 13:19:04 +00:00
mime_extension_lookup(self.mime.clone()).map(|extensions| extensions[0].clone())
}
}