Add FromHashMap impl for Unpublished

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

@ -82,6 +82,9 @@ impl FromHashMap for BibliographyType {
T_REPOSITORY => Some(Box::new(Self::Repository(*Repository::from_hash_map(map)?))), T_REPOSITORY => Some(Box::new(Self::Repository(*Repository::from_hash_map(map)?))),
T_TECH_REPORT => Some(Box::new(Self::TechReport(*TechReport::from_hash_map(map)?))), T_TECH_REPORT => Some(Box::new(Self::TechReport(*TechReport::from_hash_map(map)?))),
T_THESIS => Some(Box::new(Self::Thesis(*Thesis::from_hash_map(map)?))), T_THESIS => Some(Box::new(Self::Thesis(*Thesis::from_hash_map(map)?))),
T_UNPUBLISHED => Some(Box::new(Self::Unpublished(*Unpublished::from_hash_map(
map,
)?))),
_ => None, _ => None,
} }
} }

@ -1,4 +1,8 @@
use crate::utils::date::LocalDate; use crate::bibliography::keys::{K_AUTHOR, K_DATE, K_TITLE};
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 not formally published /// A source that is not formally published
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -18,3 +22,15 @@ impl Unpublished {
} }
} }
} }
impl FromHashMap for Unpublished {
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 mut unpub = Unpublished::new(author.clone(), title.clone());
unpub.date = map.get(K_DATE).and_then(|d| parse_date(d));
Some(Box::new(unpub))
}
}

@ -162,7 +162,7 @@ mod tests {
map.insert("key".to_string(), "test_entry".to_string()); map.insert("key".to_string(), "test_entry".to_string());
map.insert("type".to_string(), "thesis".to_string()); map.insert("type".to_string(), "thesis".to_string());
map.insert("author".to_string(), "test".to_string()); map.insert("author".to_string(), "test".to_string());
map.insert("title".to_string(), "test tech".to_string()); map.insert("title".to_string(), "test".to_string());
map.insert("school".to_string(), "test".to_string()); map.insert("school".to_string(), "test".to_string());
map.insert("note".to_string(), "This is a test".to_string()); map.insert("note".to_string(), "This is a test".to_string());
map.insert("date".to_string(), "02.09.2020".to_string()); map.insert("date".to_string(), "02.09.2020".to_string());
@ -170,4 +170,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(), "thesis".to_string()) assert_eq!(entry.bib_type.name(), "thesis".to_string())
} }
#[test]
fn it_creates_unpublished_from_hashmaps() {
let mut map: HashMap<String, String> = HashMap::new();
map.insert("key".to_string(), "test_entry".to_string());
map.insert("type".to_string(), "unpublished".to_string());
map.insert("author".to_string(), "test".to_string());
map.insert("title".to_string(), "test".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(), "unpublished".to_string())
}
} }

Loading…
Cancel
Save