diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index 95adc24..4c51380 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -82,6 +82,9 @@ impl FromHashMap for BibliographyType { 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)?))), + T_UNPUBLISHED => Some(Box::new(Self::Unpublished(*Unpublished::from_hash_map( + map, + )?))), _ => None, } } diff --git a/src/bibliography/bib_types/unpublished.rs b/src/bibliography/bib_types/unpublished.rs index ceb9d42..2b5691c 100644 --- a/src/bibliography/bib_types/unpublished.rs +++ b/src/bibliography/bib_types/unpublished.rs @@ -1,4 +1,8 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{K_AUTHOR, K_DATE, K_TITLE}; +use crate::bibliography::FromHashMap; +use crate::utils::date::{parse_date, LocalDate}; +use std::collections::hash_map::RandomState; +use std::collections::HashMap; /// A source that is not formally published #[derive(Clone, Debug)] @@ -18,3 +22,15 @@ impl Unpublished { } } } + +impl FromHashMap for Unpublished { + fn from_hash_map(map: &HashMap) -> Option> { + let author = map.get(K_AUTHOR)?; + let title = map.get(K_TITLE)?; + let mut unpub = Unpublished::new(author.clone(), title.clone()); + + unpub.date = map.get(K_DATE).and_then(|d| parse_date(d)); + + Some(Box::new(unpub)) + } +} diff --git a/src/lib.rs b/src/lib.rs index 33dae27..89ec1d4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,7 +162,7 @@ mod tests { 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("title".to_string(), "test".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()); @@ -170,4 +170,17 @@ mod tests { let entry = BibliographyEntry::from_hash_map(&map).unwrap(); assert_eq!(entry.bib_type.name(), "thesis".to_string()) } + + #[test] + fn it_creates_unpublished_from_hashmaps() { + let mut map: HashMap = HashMap::new(); + map.insert("key".to_string(), "test_entry".to_string()); + map.insert("type".to_string(), "unpublished".to_string()); + map.insert("author".to_string(), "test".to_string()); + map.insert("title".to_string(), "test".to_string()); + map.insert("note".to_string(), "This is a test".to_string()); + + let entry = BibliographyEntry::from_hash_map(&map).unwrap(); + assert_eq!(entry.bib_type.name(), "unpublished".to_string()) + } }