Add FromHashMap impl for InCollection

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

@ -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<String>,
volume: Option<String>,
series: Option<String>,
chapter: Option<String>,
pages: Option<String>,
position: Option<String>,
address: Option<String>,
edition: Option<String>,
}
@ -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<String, String, RandomState>) -> Option<Box<Self>> {
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))
}
}

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

@ -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";

@ -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<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_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())
}
}

Loading…
Cancel
Save