From ea8067b1e0b0e3d75f38ebb00a3d2652ab7c99ab Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 2 Sep 2020 15:59:17 +0200 Subject: [PATCH] Add FromHashMap impl for in_book Signed-off-by: trivernis --- src/bibliography/bib_types/in_book.rs | 40 ++++++++++++++++++++++++++- src/bibliography/bib_types/mod.rs | 1 + src/lib.rs | 16 +++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/bibliography/bib_types/in_book.rs b/src/bibliography/bib_types/in_book.rs index 448833e..8970823 100644 --- a/src/bibliography/bib_types/in_book.rs +++ b/src/bibliography/bib_types/in_book.rs @@ -1,4 +1,10 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{ + K_ADDRESS, K_AUTHOR, K_DATE, K_EDITION, 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; /// Source that is part of a book #[derive(Clone, Debug)] @@ -36,3 +42,35 @@ impl InBook { } } } + +impl FromHashMap for InBook { + fn from_hash_map(map: &HashMap) -> Option> { + let author = map.get(K_AUTHOR)?; + let title = map.get(K_TITLE)?; + let position = map.get(K_TITLE)?; + let publisher = map.get(K_PUBLISHER)?; + let date = parse_date(map.get(K_DATE)?)?; + let mut in_book = InBook::new( + author.clone(), + title.clone(), + position.clone(), + publisher.clone(), + date, + ); + + if let Some(volume) = map.get(K_VOLUME) { + in_book.volume = Some(volume.clone()); + } + if let Some(series) = map.get(K_SERIES) { + in_book.series = Some(series.clone()); + } + if let Some(address) = map.get(K_ADDRESS) { + in_book.address = Some(address.clone()); + } + if let Some(edition) = map.get(K_EDITION) { + in_book.edition = Some(edition.clone()) + } + + Some(Box::new(in_book)) + } +} diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index ba910df..80cac43 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -73,6 +73,7 @@ impl FromHashMap for BibliographyType { T_ARTICLE => Some(Box::new(Self::Article(*Article::from_hash_map(map)?))), 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)?))), _ => None, } } diff --git a/src/lib.rs b/src/lib.rs index 161baa3..6542bb7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,4 +73,20 @@ mod tests { let entry = BibliographyEntry::from_hash_map(&map).unwrap(); assert_eq!(entry.bib_type.name(), "booklet".to_string()) } + + #[test] + fn it_creates_in_book_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_book".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_book".to_string()) + } }