diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index 4c51380..c3458e2 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -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, } } diff --git a/src/bibliography/bib_types/website.rs b/src/bibliography/bib_types/website.rs index a3c331b..1c7f5ea 100644 --- a/src/bibliography/bib_types/website.rs +++ b/src/bibliography/bib_types/website.rs @@ -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, pub author: Option, pub accessed_at: Option, - pub date: Option, + pub date: Option, } impl Website { @@ -22,3 +26,17 @@ impl Website { } } } + +impl FromHashMap for Website { + fn from_hash_map(map: &HashMap) -> Option> { + 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)) + } +} diff --git a/src/lib.rs b/src/lib.rs index 89ec1d4..7653180 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 = 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()) + } }