use SmartString properly

This commit is contained in:
Lynne Megido 2021-08-28 18:21:53 +10:00
parent fa49dd9fb5
commit 2884f9fe4c
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
3 changed files with 20 additions and 8 deletions

View file

@ -1,7 +1,7 @@
use crate::findings::{Findings, ScanError}; use crate::findings::{Findings, ScanError};
use crate::mime_db::MimeDb; use crate::mime_db::MimeDb;
use crate::parameters::ScanOpts; use crate::parameters::ScanOpts;
use crate::MIMEDB; use crate::{String, MIMEDB};
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::fs::File; use std::fs::File;

View file

@ -37,8 +37,8 @@ impl serde::Serialize for Findings {
} }
impl Findings { impl Findings {
pub fn recommended_extension(&self) -> Option<std::string::String> { pub fn recommended_extension(&self) -> Option<String> {
mime_extension_lookup(self.mime.essence_str().into()).map(|extensions| extensions[0].clone()) mime_extension_lookup(self.mime.essence_str().into()).map(|extensions| extensions[0].clone().into())
} }
} }

View file

@ -66,19 +66,18 @@ fn detect_type() {
/// Ensure that `mime_extension_lookup` works as expected, and that the set of extensions for JPEG, PNG, PDF, and ZIP /// Ensure that `mime_extension_lookup` works as expected, and that the set of extensions for JPEG, PNG, PDF, and ZIP
/// contain "jpg", "png", "pdf", and "zip", respectively. /// contain "jpg", "png", "pdf", and "zip", respectively.
fn recommend_ext() { fn recommend_ext() {
use std::string::String as StdString;
assert!(mime_extension_lookup(IMAGE_JPEG.essence_str().into()) assert!(mime_extension_lookup(IMAGE_JPEG.essence_str().into())
.unwrap() .unwrap()
.contains(&StdString::from("jpg"))); .contains(&String::from("jpg")));
assert!(mime_extension_lookup(IMAGE_PNG.essence_str().into()) assert!(mime_extension_lookup(IMAGE_PNG.essence_str().into())
.unwrap() .unwrap()
.contains(&StdString::from("png"))); .contains(&String::from("png")));
assert!(mime_extension_lookup(APPLICATION_PDF.essence_str().into()) assert!(mime_extension_lookup(APPLICATION_PDF.essence_str().into())
.unwrap() .unwrap()
.contains(&StdString::from("pdf"))); .contains(&String::from("pdf")));
assert!(mime_extension_lookup(application_zip().essence_str().into()) assert!(mime_extension_lookup(application_zip().essence_str().into())
.unwrap() .unwrap()
.contains(&StdString::from("zip"))); .contains(&String::from("zip")));
} }
#[test] #[test]
@ -463,3 +462,16 @@ fn verbosity() {
assert_eq!(Parameters::parse_from(&["fif", flags]).default_verbosity(), level); assert_eq!(Parameters::parse_from(&["fif", flags]).default_verbosity(), level);
} }
} }
#[test]
/// Ensures that smart strings don't deviate from std's Strings
fn validate_string_type() {
use fif::String as SmartString;
use std::string::String as StdString;
assert_eq!(SmartString::new(), StdString::new());
assert_eq!(SmartString::from("smol"), StdString::from("smol"));
assert_eq!(
SmartString::from("A long and therefore heap-allocated string"),
StdString::from("A long and therefore heap-allocated string")
);
}