diff --git a/src/bibliography/bib_types/manual.rs b/src/bibliography/bib_types/manual.rs index abe6b52..80b5dd3 100644 --- a/src/bibliography/bib_types/manual.rs +++ b/src/bibliography/bib_types/manual.rs @@ -43,13 +43,11 @@ impl FromHashMap for Manual { if let Some(address) = map.get(K_ADDRESS) { manual.address = Some(address.clone()); } - if let Some(edition) = map.get(K_EDITION) { - manual.edition = Some(edition.clone()); + if let Some(edition) = map.get(K_EDITION).and_then(|s| Some(s.clone())) { + Some(edition.clone()); } if let Some(date) = map.get(K_DATE) { - if let Some(date) = parse_date(date) { - manual.date = Some(date); - } + manual.date = parse_date(date); } Some(Box::new(manual)) diff --git a/src/bibliography/bib_types/misc.rs b/src/bibliography/bib_types/misc.rs index 6f06106..46bd764 100644 --- a/src/bibliography/bib_types/misc.rs +++ b/src/bibliography/bib_types/misc.rs @@ -1,10 +1,15 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{K_AUTHOR, K_DATE, K_HOW_PUBLISHED, K_TITLE, K_URL}; +use crate::bibliography::FromHashMap; +use crate::utils::date::{parse_date, LocalDate}; +use std::collections::hash_map::RandomState; +use std::collections::HashMap; /// A source that does not fit any of the other types #[derive(Clone, Debug)] pub struct Misc { pub author: Option, pub title: Option, + pub url: Option, pub how_published: Option, pub date: Option, } @@ -15,8 +20,33 @@ impl Misc { Self { author: None, title: None, + url: None, how_published: None, date: None, } } } + +impl FromHashMap for Misc { + fn from_hash_map(map: &HashMap) -> Option> { + let mut misc = Misc::new(); + + if let Some(author) = map.get(K_AUTHOR) { + misc.author = Some(author.clone()) + } + if let Some(title) = map.get(K_TITLE) { + misc.title = Some(title.clone()) + } + if let Some(url) = map.get(K_URL) { + misc.url = Some(url.clone()) + } + if let Some(how_pub) = map.get(K_HOW_PUBLISHED) { + misc.how_published = Some(how_pub.clone()); + } + if let Some(date) = map.get(K_DATE) { + misc.date = parse_date(date); + } + + Some(Box::new(misc)) + } +} diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index b6d97a7..d84207d 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -78,6 +78,7 @@ impl FromHashMap for BibliographyType { 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)?))), _ => None, } } diff --git a/src/lib.rs b/src/lib.rs index ba8a974..706f52f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,4 +116,15 @@ mod tests { let entry = BibliographyEntry::from_hash_map(&map).unwrap(); assert_eq!(entry.bib_type.name(), "manual".to_string()) } + + #[test] + fn it_creates_misc_from_hashmaps() { + let mut map: HashMap = HashMap::new(); + map.insert("key".to_string(), "test_entry".to_string()); + map.insert("type".to_string(), "misc".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(), "misc".to_string()) + } }