Add FromHashMap impl for Website

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

@ -85,6 +85,7 @@ impl FromHashMap for BibliographyType {
T_UNPUBLISHED => Some(Box::new(Self::Unpublished(*Unpublished::from_hash_map(
map,
)?))),
T_WEBSITE => Some(Box::new(Self::Website(*Website::from_hash_map(map)?))),
_ => None,
}
}

@ -1,4 +1,8 @@
use crate::utils::date::LocalDate;
use crate::bibliography::keys::{K_ACCESSED_AT, K_AUTHOR, K_DATE, K_TITLE, K_URL};
use crate::bibliography::FromHashMap;
use crate::utils::date::{parse_date, LocalDate};
use std::collections::hash_map::RandomState;
use std::collections::HashMap;
/// A website source that can only consists of an url
#[derive(Clone, Debug)]
@ -7,7 +11,7 @@ pub struct Website {
pub title: Option<String>,
pub author: Option<String>,
pub accessed_at: Option<LocalDate>,
pub date: Option<String>,
pub date: Option<LocalDate>,
}
impl Website {
@ -22,3 +26,17 @@ impl Website {
}
}
}
impl FromHashMap for Website {
fn from_hash_map(map: &HashMap<String, String, RandomState>) -> Option<Box<Self>> {
let url = map.get(K_URL)?;
let mut website = Website::new(url.clone());
website.title = map.get(K_TITLE).cloned();
website.author = map.get(K_AUTHOR).cloned();
website.accessed_at = map.get(K_ACCESSED_AT).and_then(|d| parse_date(d));
website.date = map.get(K_DATE).and_then(|d| parse_date(d));
Some(Box::new(website))
}
}

@ -183,4 +183,16 @@ mod tests {
let entry = BibliographyEntry::from_hash_map(&map).unwrap();
assert_eq!(entry.bib_type.name(), "unpublished".to_string())
}
#[test]
fn it_creates_websites_from_hashmaps() {
let mut map: HashMap<String, String> = HashMap::new();
map.insert("key".to_string(), "test_entry".to_string());
map.insert("type".to_string(), "website".to_string());
map.insert("url".to_string(), "https://github.com".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(), "website".to_string())
}
}

Loading…
Cancel
Save