Add FromHashMap impl for booklet

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent bfd573105a
commit 7e4fbcb7cd
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,4 +1,8 @@
use crate::utils::date::LocalDate; use crate::bibliography::keys::{K_ADDRESS, K_AUTHOR, K_DATE, K_HOW_PUBLISHED, K_TITLE};
use crate::bibliography::FromHashMap;
use crate::utils::date::{parse_date, LocalDate};
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
/// A booklet source where only the title can be known /// A booklet source where only the title can be known
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -22,3 +26,25 @@ impl Booklet {
} }
} }
} }
impl FromHashMap for Booklet {
fn from_hash_map(map: &HashMap<String, String, RandomState>) -> Option<Box<Self>> {
let title = map.get(K_TITLE)?;
let mut booklet = Booklet::new(title.clone());
if let Some(author) = map.get(K_AUTHOR) {
booklet.author = Some(author.clone())
}
if let Some(how_published) = map.get(K_HOW_PUBLISHED) {
booklet.how_published = Some(how_published.clone());
}
if let Some(address) = map.get(K_ADDRESS) {
booklet.address = Some(address.clone());
}
if let Some(date) = map.get(K_DATE) {
booklet.date = parse_date(date);
}
Some(Box::new(booklet))
}
}

@ -72,6 +72,7 @@ impl FromHashMap for BibliographyType {
match map.get(K_TYPE)?.as_str() { match map.get(K_TYPE)?.as_str() {
T_ARTICLE => Some(Box::new(Self::Article(*Article::from_hash_map(map)?))), 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_BOOK => Some(Box::new(Self::Book(*Book::from_hash_map(map)?))),
T_BOOKLET => Some(Box::new(Self::Booklet(*Booklet::from_hash_map(map)?))),
_ => None, _ => None,
} }
} }

@ -14,6 +14,7 @@ pub const K_LICENSE: &str = "license";
pub const K_NUMBER: &str = "number"; pub const K_NUMBER: &str = "number";
pub const K_PAGES: &str = "pages"; pub const K_PAGES: &str = "pages";
pub const K_NOTE: &str = "note"; pub const K_NOTE: &str = "note";
pub const K_HOW_PUBLISHED: &str = "how_published";
pub const T_ARTICLE: &str = "article"; pub const T_ARTICLE: &str = "article";
pub const T_BOOK: &str = "book"; pub const T_BOOK: &str = "book";

@ -60,4 +60,17 @@ mod tests {
let entry = BibliographyEntry::from_hash_map(&map).unwrap(); let entry = BibliographyEntry::from_hash_map(&map).unwrap();
assert_eq!(entry.bib_type.name(), "book".to_string()) assert_eq!(entry.bib_type.name(), "book".to_string())
} }
#[test]
fn it_creates_booklet_from_hashmaps() {
let mut map: HashMap<String, String> = 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(), "booklet".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(), "booklet".to_string())
}
} }

Loading…
Cancel
Save