Browse Source

use SmartString properly

edition-2021
Lynne Megido 3 months ago
parent
commit
2884f9fe4c
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
  1. 2
      src/files.rs
  2. 4
      src/findings.rs
  3. 22
      src/tests/mod.rs

2
src/files.rs

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

4
src/findings.rs

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

22
src/tests/mod.rs

@ -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
/// contain "jpg", "png", "pdf", and "zip", respectively.
fn recommend_ext() {
use std::string::String as StdString;
assert!(mime_extension_lookup(IMAGE_JPEG.essence_str().into())
.unwrap()
.contains(&StdString::from("jpg")));
.contains(&String::from("jpg")));
assert!(mime_extension_lookup(IMAGE_PNG.essence_str().into())
.unwrap()
.contains(&StdString::from("png")));
.contains(&String::from("png")));
assert!(mime_extension_lookup(APPLICATION_PDF.essence_str().into())
.unwrap()
.contains(&StdString::from("pdf")));
.contains(&String::from("pdf")));
assert!(mime_extension_lookup(application_zip().essence_str().into())
.unwrap()
.contains(&StdString::from("zip")));
.contains(&String::from("zip")));
}
#[test]
@ -463,3 +462,16 @@ fn verbosity() {
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")
);
}

Loading…
Cancel
Save