2021-03-25 18:46:07 +00:00
|
|
|
use std::path::Path;
|
2021-02-18 09:48:38 +00:00
|
|
|
|
2021-02-14 17:12:27 +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};
|
|
|
|
|
2021-02-28 14:06:05 +00:00
|
|
|
/// Information about a scanned file.
|
2021-04-20 08:52:49 +00:00
|
|
|
#[derive(Ord, PartialOrd, Eq, PartialEq)]
|
2021-03-25 18:46:07 +00:00
|
|
|
pub struct Findings<'a> {
|
2021-02-28 14:06:05 +00:00
|
|
|
/// The location of the scanned file.
|
2021-03-25 18:46:07 +00:00
|
|
|
pub file: &'a Path,
|
2021-02-28 14:06:05 +00:00
|
|
|
/// Whether or not the file's extension is valid for its mimetype.
|
2021-02-14 17:12:27 +00:00
|
|
|
pub valid: bool,
|
2021-02-28 14:06:05 +00:00
|
|
|
/// The file's mimetype.
|
2021-02-14 17:12:27 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 18:46:07 +00:00
|
|
|
impl<'a> Findings<'a> {
|
2021-03-11 17:26:35 +00:00
|
|
|
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())
|
2021-02-14 17:12:27 +00:00
|
|
|
}
|
|
|
|
}
|