From aa0b68939be327d0958fb987fa84926b487545d6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 2 Sep 2020 19:11:19 +0200 Subject: [PATCH] Add FromHashMap impl for Thesis Signed-off-by: trivernis --- src/bibliography/bib_types/mod.rs | 1 + src/bibliography/bib_types/thesis.rs | 20 +++++++++++++++++++- src/bibliography/keys.rs | 1 + src/lib.rs | 15 +++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index d9516f4..95adc24 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -81,6 +81,7 @@ impl FromHashMap for BibliographyType { 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_TECH_REPORT => Some(Box::new(Self::TechReport(*TechReport::from_hash_map(map)?))), + T_THESIS => Some(Box::new(Self::Thesis(*Thesis::from_hash_map(map)?))), _ => None, } } diff --git a/src/bibliography/bib_types/thesis.rs b/src/bibliography/bib_types/thesis.rs index a62735c..8b82568 100644 --- a/src/bibliography/bib_types/thesis.rs +++ b/src/bibliography/bib_types/thesis.rs @@ -1,4 +1,8 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{K_ADDRESS, K_AUTHOR, K_DATE, K_SCHOOL, K_TITLE}; +use crate::bibliography::FromHashMap; +use crate::utils::date::{parse_date, LocalDate}; +use std::collections::hash_map::RandomState; +use std::collections::HashMap; /// A thesis source entry #[derive(Clone, Debug)] @@ -22,3 +26,17 @@ impl Thesis { } } } + +impl FromHashMap for Thesis { + fn from_hash_map(map: &HashMap) -> Option> { + let author = map.get(K_AUTHOR)?; + let title = map.get(K_TITLE)?; + let school = map.get(K_SCHOOL)?; + let date = map.get(K_DATE).and_then(|d| parse_date(d))?; + let mut thesis = Thesis::new(author.clone(), title.clone(), school.clone(), date.clone()); + + thesis.address = map.get(K_ADDRESS).cloned(); + + Some(Box::new(thesis)) + } +} diff --git a/src/bibliography/keys.rs b/src/bibliography/keys.rs index bd11ce9..b624acd 100644 --- a/src/bibliography/keys.rs +++ b/src/bibliography/keys.rs @@ -21,6 +21,7 @@ pub const K_ORGANIZATION: &str = "organization"; pub const K_CMS: &str = "cms"; pub const K_ACCESSED_AT: &str = "accessed_at"; pub const K_INSTITUTION: &str = "institution"; +pub const K_SCHOOL: &str = "school"; pub const T_ARTICLE: &str = "article"; pub const T_BOOK: &str = "book"; diff --git a/src/lib.rs b/src/lib.rs index a0e8b1f..33dae27 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,4 +155,19 @@ mod tests { let entry = BibliographyEntry::from_hash_map(&map).unwrap(); assert_eq!(entry.bib_type.name(), "tech_report".to_string()) } + + #[test] + fn it_creates_thesis_from_hashmaps() { + let mut map: HashMap = HashMap::new(); + map.insert("key".to_string(), "test_entry".to_string()); + map.insert("type".to_string(), "thesis".to_string()); + map.insert("author".to_string(), "test".to_string()); + map.insert("title".to_string(), "test tech".to_string()); + map.insert("school".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(), "thesis".to_string()) + } }