From 0ebfb5fd5ef6671fe68a2cefc57ebf8abd6ed611 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 2 Sep 2020 18:26:59 +0200 Subject: [PATCH] Add FromHashMap impl for InCollection Signed-off-by: trivernis --- src/bibliography/bib_types/in_collection.rs | 46 ++++++++++++++++++--- src/bibliography/bib_types/mod.rs | 3 ++ src/bibliography/keys.rs | 2 + src/lib.rs | 16 +++++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/bibliography/bib_types/in_collection.rs b/src/bibliography/bib_types/in_collection.rs index a7d0655..9f18500 100644 --- a/src/bibliography/bib_types/in_collection.rs +++ b/src/bibliography/bib_types/in_collection.rs @@ -1,4 +1,11 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{ + K_ADDRESS, K_AUTHOR, K_DATE, K_EDITION, K_EDITOR, K_POSITION, K_PUBLISHER, K_SERIES, K_TITLE, + K_VOLUME, +}; +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 in a collection #[derive(Clone, Debug)] @@ -10,8 +17,7 @@ pub struct InCollection { editor: Option, volume: Option, series: Option, - chapter: Option, - pages: Option, + position: Option, address: Option, edition: Option, } @@ -27,10 +33,40 @@ impl InCollection { editor: None, volume: None, series: None, - chapter: None, - pages: None, + position: None, address: None, edition: None, } } } + +impl FromHashMap for InCollection { + fn from_hash_map(map: &HashMap) -> Option> { + let author = map.get(K_AUTHOR)?; + let title = map.get(K_TITLE)?; + let publisher = map.get(K_PUBLISHER)?; + let date = parse_date(map.get(K_DATE)?)?; + let mut in_col = InCollection::new(author.clone(), title.clone(), publisher.clone(), date); + + if let Some(editor) = map.get(K_EDITOR) { + in_col.editor = Some(editor.clone()); + } + if let Some(volume) = map.get(K_VOLUME) { + in_col.volume = Some(volume.clone()); + } + if let Some(series) = map.get(K_SERIES) { + in_col.series = Some(series.clone()); + } + if let Some(position) = map.get(K_POSITION) { + in_col.position = Some(position.clone()); + } + if let Some(address) = map.get(K_ADDRESS) { + in_col.address = Some(address.clone()); + } + if let Some(edition) = map.get(K_EDITION) { + in_col.edition = Some(edition.clone()); + } + + Some(Box::new(in_col)) + } +} diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index 80cac43..b1d85c2 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -74,6 +74,9 @@ impl FromHashMap for BibliographyType { T_BOOK => Some(Box::new(Self::Book(*Book::from_hash_map(map)?))), T_BOOKLET => Some(Box::new(Self::Booklet(*Booklet::from_hash_map(map)?))), T_IN_BOOK => Some(Box::new(Self::InBook(*InBook::from_hash_map(map)?))), + T_IN_COLLECTION => Some(Box::new(Self::InCollection(*InCollection::from_hash_map( + map, + )?))), _ => None, } } diff --git a/src/bibliography/keys.rs b/src/bibliography/keys.rs index 4f1355a..d50c859 100644 --- a/src/bibliography/keys.rs +++ b/src/bibliography/keys.rs @@ -15,6 +15,8 @@ pub const K_NUMBER: &str = "number"; pub const K_PAGES: &str = "pages"; 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 T_ARTICLE: &str = "article"; pub const T_BOOK: &str = "book"; diff --git a/src/lib.rs b/src/lib.rs index 6542bb7..a281077 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,4 +89,20 @@ mod tests { let entry = BibliographyEntry::from_hash_map(&map).unwrap(); assert_eq!(entry.bib_type.name(), "in_book".to_string()) } + + #[test] + fn it_creates_in_collection_from_hashmaps() { + let mut map: HashMap = HashMap::new(); + 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("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()) + } }