diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index d84207d..ef487ac 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -79,6 +79,7 @@ impl FromHashMap for BibliographyType { )?))), T_MANUAL => Some(Box::new(Self::Manual(*Manual::from_hash_map(map)?))), T_MISC => Some(Box::new(Self::Misc(*Misc::from_hash_map(map)?))), + T_REPOSITORY => Some(Box::new(Self::Repository(*Repository::from_hash_map(map)?))), _ => None, } } diff --git a/src/bibliography/bib_types/repository.rs b/src/bibliography/bib_types/repository.rs index 67530f0..91d72b8 100644 --- a/src/bibliography/bib_types/repository.rs +++ b/src/bibliography/bib_types/repository.rs @@ -1,4 +1,8 @@ -use crate::utils::date::LocalDate; +use crate::bibliography::keys::{K_ACCESSED_AT, K_AUTHOR, K_CMS, K_LICENSE, 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 repository source that represents any git repository or similar /// structures @@ -25,3 +29,18 @@ impl Repository { } } } + +impl FromHashMap for Repository { + fn from_hash_map(map: &HashMap) -> Option> { + let author = map.get(K_AUTHOR)?; + let title = map.get(K_TITLE)?; + let mut repo = Repository::new(author.clone(), title.clone()); + + repo.url = map.get(K_URL).cloned(); + repo.license = map.get(K_LICENSE).cloned(); + repo.cms = map.get(K_CMS).cloned(); + repo.accessed_at = map.get(K_ACCESSED_AT).and_then(|d| parse_date(d)); + + Some(Box::new(repo)) + } +} diff --git a/src/bibliography/keys.rs b/src/bibliography/keys.rs index dd8cc53..2e2c941 100644 --- a/src/bibliography/keys.rs +++ b/src/bibliography/keys.rs @@ -18,6 +18,8 @@ pub const K_HOW_PUBLISHED: &str = "how_published"; pub const K_EDITOR: &str = "editor"; pub const K_POSITION: &str = "position"; pub const K_ORGANIZATION: &str = "organization"; +pub const K_CMS: &str = "cms"; +pub const K_ACCESSED_AT: &str = "accessed_at"; pub const T_ARTICLE: &str = "article"; pub const T_BOOK: &str = "book"; diff --git a/src/lib.rs b/src/lib.rs index 706f52f..d990354 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,4 +127,17 @@ mod tests { let entry = BibliographyEntry::from_hash_map(&map).unwrap(); assert_eq!(entry.bib_type.name(), "misc".to_string()) } + + #[test] + fn it_creates_repos_from_hashmaps() { + let mut map: HashMap = HashMap::new(); + map.insert("key".to_string(), "test_entry".to_string()); + map.insert("type".to_string(), "repository".to_string()); + map.insert("author".to_string(), "trivernis".to_string()); + map.insert("title".to_string(), "snekdown".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(), "repository".to_string()) + } }