Add FromHashMap impl for TechReport

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent c119046923
commit 161a87076d
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -80,6 +80,7 @@ impl FromHashMap for BibliographyType {
T_MANUAL => Some(Box::new(Self::Manual(*Manual::from_hash_map(map)?))), T_MANUAL => Some(Box::new(Self::Manual(*Manual::from_hash_map(map)?))),
T_MISC => Some(Box::new(Self::Misc(*Misc::from_hash_map(map)?))), T_MISC => Some(Box::new(Self::Misc(*Misc::from_hash_map(map)?))),
T_REPOSITORY => Some(Box::new(Self::Repository(*Repository::from_hash_map(map)?))), T_REPOSITORY => Some(Box::new(Self::Repository(*Repository::from_hash_map(map)?))),
T_TECH_REPORT => Some(Box::new(Self::TechReport(*TechReport::from_hash_map(map)?))),
_ => None, _ => None,
} }
} }

@ -1,4 +1,8 @@
use crate::utils::date::LocalDate; use crate::bibliography::keys::{K_ADDRESS, K_AUTHOR, K_DATE, K_INSTITUTION, K_NUMBER, K_TITLE};
use crate::bibliography::FromHashMap;
use crate::utils::date::{parse_date, LocalDate};
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
/// A tech report for example a white paper /// A tech report for example a white paper
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -24,3 +28,19 @@ impl TechReport {
} }
} }
} }
impl FromHashMap for TechReport {
fn from_hash_map(map: &HashMap<String, String, RandomState>) -> Option<Box<Self>> {
let author = map.get(K_AUTHOR)?;
let title = map.get(K_TITLE)?;
let institution = map.get(K_INSTITUTION)?;
let date = map.get(K_DATE).and_then(|d| parse_date(d))?;
let mut tech_report =
TechReport::new(author.clone(), title.clone(), institution.clone(), date);
tech_report.number = map.get(K_NUMBER).cloned();
tech_report.address = map.get(K_ADDRESS).cloned();
Some(Box::new(tech_report))
}
}

@ -20,6 +20,7 @@ pub const K_POSITION: &str = "position";
pub const K_ORGANIZATION: &str = "organization"; pub const K_ORGANIZATION: &str = "organization";
pub const K_CMS: &str = "cms"; pub const K_CMS: &str = "cms";
pub const K_ACCESSED_AT: &str = "accessed_at"; pub const K_ACCESSED_AT: &str = "accessed_at";
pub const K_INSTITUTION: &str = "institution";
pub const T_ARTICLE: &str = "article"; pub const T_ARTICLE: &str = "article";
pub const T_BOOK: &str = "book"; pub const T_BOOK: &str = "book";

@ -140,4 +140,19 @@ mod tests {
let entry = BibliographyEntry::from_hash_map(&map).unwrap(); let entry = BibliographyEntry::from_hash_map(&map).unwrap();
assert_eq!(entry.bib_type.name(), "repository".to_string()) assert_eq!(entry.bib_type.name(), "repository".to_string())
} }
#[test]
fn it_creates_tech_reports_from_hashmaps() {
let mut map: HashMap<String, String> = HashMap::new();
map.insert("key".to_string(), "test_entry".to_string());
map.insert("type".to_string(), "tech_report".to_string());
map.insert("author".to_string(), "test".to_string());
map.insert("title".to_string(), "test tech".to_string());
map.insert("institution".to_string(), "test".to_string());
map.insert("note".to_string(), "This is a test".to_string());
map.insert("date".to_string(), "02.09.2020".to_string());
let entry = BibliographyEntry::from_hash_map(&map).unwrap();
assert_eq!(entry.bib_type.name(), "tech_report".to_string())
}
} }

Loading…
Cancel
Save