From 659a7e79c3cffca7fbc6399b55bb140d9199d6ac Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 2 Sep 2020 18:33:52 +0200 Subject: [PATCH] Add FromHashMap impl for Manual Signed-off-by: trivernis --- src/bibliography/bib_types/manual.rs | 33 +++++++++++++++++++++++++++- src/bibliography/bib_types/mod.rs | 1 + src/bibliography/keys.rs | 1 + src/lib.rs | 15 +++++++++++-- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/bibliography/bib_types/manual.rs b/src/bibliography/bib_types/manual.rs index 2c69ffb..abe6b52 100644 --- a/src/bibliography/bib_types/manual.rs +++ b/src/bibliography/bib_types/manual.rs @@ -1,4 +1,8 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{K_ADDRESS, K_AUTHOR, K_DATE, K_EDITION, K_ORGANIZATION, K_TITLE}; +use crate::bibliography::FromHashMap; +use crate::utils::date::{parse_date, LocalDate}; +use std::collections::hash_map::RandomState; +use std::collections::HashMap; /// A manual entry source #[derive(Clone, Debug)] @@ -24,3 +28,30 @@ impl Manual { } } } + +impl FromHashMap for Manual { + fn from_hash_map(map: &HashMap) -> Option> { + let title = map.get(K_TITLE)?; + let mut manual = Manual::new(title.clone()); + + if let Some(author) = map.get(K_AUTHOR) { + manual.author = Some(author.clone()); + } + if let Some(org) = map.get(K_ORGANIZATION) { + manual.organization = Some(org.clone()); + } + 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(date) = map.get(K_DATE) { + if let Some(date) = parse_date(date) { + manual.date = Some(date); + } + } + + Some(Box::new(manual)) + } +} diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index b1d85c2..b6d97a7 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -77,6 +77,7 @@ impl FromHashMap for BibliographyType { T_IN_COLLECTION => Some(Box::new(Self::InCollection(*InCollection::from_hash_map( map, )?))), + T_MANUAL => Some(Box::new(Self::Manual(*Manual::from_hash_map(map)?))), _ => None, } } diff --git a/src/bibliography/keys.rs b/src/bibliography/keys.rs index d50c859..dd8cc53 100644 --- a/src/bibliography/keys.rs +++ b/src/bibliography/keys.rs @@ -17,6 +17,7 @@ pub const K_NOTE: &str = "note"; pub const K_HOW_PUBLISHED: &str = "how_published"; pub const K_EDITOR: &str = "editor"; pub const K_POSITION: &str = "position"; +pub const K_ORGANIZATION: &str = "organization"; pub const T_ARTICLE: &str = "article"; pub const T_BOOK: &str = "book"; diff --git a/src/lib.rs b/src/lib.rs index a281077..ba8a974 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -96,13 +96,24 @@ mod tests { map.insert("key".to_string(), "test_entry".to_string()); map.insert("author".to_string(), "test".to_string()); map.insert("title".to_string(), "test_title".to_string()); - map.insert("type".to_string(), "in_collection".to_string()); map.insert("publisher".to_string(), "test_publisher".to_string()); - map.insert("position".to_string(), "page 2".to_string()); + map.insert("type".to_string(), "in_collection".to_string()); map.insert("date".to_string(), "01.09.2020".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(), "in_collection".to_string()) } + + #[test] + fn it_creates_manuals_from_hashmaps() { + let mut map: HashMap = HashMap::new(); + map.insert("key".to_string(), "test_entry".to_string()); + map.insert("title".to_string(), "test_title".to_string()); + map.insert("type".to_string(), "manual".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(), "manual".to_string()) + } }