Add FromHashMap impl for in_book

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

@ -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<String, String, RandomState>) -> Option<Box<Self>> {
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))
}
}

@ -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,
}
}

@ -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<String, String> = 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())
}
}

Loading…
Cancel
Save