use std::collections::HashMap;
use std::ops::Index;
use errors::ArmorlibError;
use binary_object::BinaryObject;
pub struct ScanObject {
pub metadata: HashMap<String, HashMap<String, String>>,
pub filetype: Option<String>,
pub detected_filetypes: Vec<String>,
pub binary_object: BinaryObject,
}
impl ScanObject {
pub fn get_metadata(&self, path: &str) -> Result<&String, ArmorlibError> {
let path = String::from(path);
let key_pair: Vec<&str> = path.split('/').collect();
let (preprocessor, key) = (
String::from(*key_pair.index(0_usize)),
String::from(*key_pair.index(1_usize)),
);
match self.metadata.get(&preprocessor) {
Some(map) => match map.get(&key) {
Some(value) => Ok(value),
None => Err(ArmorlibError::MissingMetadata(path.clone())),
},
None => Err(ArmorlibError::MissingPreprocessor(preprocessor)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_metadata() {
let metadata: HashMap<String, HashMap<String, String>> = hashmap!{
String::from("module1") => hashmap!{
String::from("key") => String::from("m1::value"),
String::from("key2") => String::from("m1::value2")
},
String::from("module2") => hashmap!{
String::from("key3") => String::from("m2::value3"),
String::from("key2") => String::from("m2::value2")
}
};
let scan_object = ScanObject {
metadata: metadata,
filetype: None,
detected_filetypes: vec![],
binary_object: BinaryObject::from(vec![]),
};
assert_eq!(
scan_object.get_metadata("module1/key").unwrap(),
&String::from("m1::value")
);
assert_eq!(
scan_object.get_metadata("module1/key2").unwrap(),
&String::from("m1::value2")
);
assert_eq!(
scan_object.get_metadata("module2/key3").unwrap(),
&String::from("m2::value3")
);
assert_eq!(
scan_object.get_metadata("module2/key2").unwrap(),
&String::from("m2::value2")
);
assert_eq!(
scan_object.get_metadata("fakemodule/key2"),
Err(ArmorlibError::MissingPreprocessor(String::from(
"fakemodule"
)))
);
assert_eq!(
scan_object.get_metadata("module2/key4"),
Err(ArmorlibError::MissingMetadata(String::from("module2/key4")))
);
}
}