Add FromHashMap impl for Manual

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent 0ebfb5fd5e
commit 659a7e79c3
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_EDITION, K_ORGANIZATION, K_TITLE};
use crate::bibliography::FromHashMap;
use crate::utils::date::{parse_date, LocalDate};
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
/// A manual entry source /// A manual entry source
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -24,3 +28,30 @@ impl Manual {
} }
} }
} }
impl FromHashMap for Manual {
fn from_hash_map(map: &HashMap<String, String, RandomState>) -> Option<Box<Self>> {
let title = map.get(K_TITLE)?;
let mut manual = Manual::new(title.clone());
if let Some(author) = map.get(K_AUTHOR) {
manual.author = Some(author.clone());
}
if let Some(org) = map.get(K_ORGANIZATION) {
manual.organization = Some(org.clone());
}
if let Some(address) = map.get(K_ADDRESS) {
manual.address = Some(address.clone());
}
if let Some(edition) = map.get(K_EDITION) {
manual.edition = Some(edition.clone());
}
if let Some(date) = map.get(K_DATE) {
if let Some(date) = parse_date(date) {
manual.date = Some(date);
}
}
Some(Box::new(manual))
}
}

@ -77,6 +77,7 @@ impl FromHashMap for BibliographyType {
T_IN_COLLECTION => Some(Box::new(Self::InCollection(*InCollection::from_hash_map( T_IN_COLLECTION => Some(Box::new(Self::InCollection(*InCollection::from_hash_map(
map, map,
)?))), )?))),
T_MANUAL => Some(Box::new(Self::Manual(*Manual::from_hash_map(map)?))),
_ => None, _ => None,
} }
} }

@ -17,6 +17,7 @@ pub const K_NOTE: &str = "note";
pub const K_HOW_PUBLISHED: &str = "how_published"; pub const K_HOW_PUBLISHED: &str = "how_published";
pub const K_EDITOR: &str = "editor"; pub const K_EDITOR: &str = "editor";
pub const K_POSITION: &str = "position"; pub const K_POSITION: &str = "position";
pub const K_ORGANIZATION: &str = "organization";
pub const T_ARTICLE: &str = "article"; pub const T_ARTICLE: &str = "article";
pub const T_BOOK: &str = "book"; pub const T_BOOK: &str = "book";

@ -96,13 +96,24 @@ mod tests {
map.insert("key".to_string(), "test_entry".to_string()); map.insert("key".to_string(), "test_entry".to_string());
map.insert("author".to_string(), "test".to_string()); map.insert("author".to_string(), "test".to_string());
map.insert("title".to_string(), "test_title".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("publisher".to_string(), "test_publisher".to_string());
map.insert("position".to_string(), "page 2".to_string()); map.insert("type".to_string(), "in_collection".to_string());
map.insert("date".to_string(), "01.09.2020".to_string()); map.insert("date".to_string(), "01.09.2020".to_string());
map.insert("note".to_string(), "This is a test".to_string()); map.insert("note".to_string(), "This is a test".to_string());
let entry = BibliographyEntry::from_hash_map(&map).unwrap(); let entry = BibliographyEntry::from_hash_map(&map).unwrap();
assert_eq!(entry.bib_type.name(), "in_collection".to_string()) assert_eq!(entry.bib_type.name(), "in_collection".to_string())
} }
#[test]
fn it_creates_manuals_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(), "manual".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(), "manual".to_string())
}
} }

Loading…
Cancel
Save