Add FromHashMap impl for Misc

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

@ -43,13 +43,11 @@ impl FromHashMap for Manual {
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(edition) = map.get(K_EDITION).and_then(|s| Some(s.clone())) {
Some(edition.clone());
}
if let Some(date) = map.get(K_DATE) {
if let Some(date) = parse_date(date) {
manual.date = Some(date);
}
manual.date = parse_date(date);
}
Some(Box::new(manual))

@ -1,10 +1,15 @@
use crate::utils::date::LocalDate;
use crate::bibliography::keys::{K_AUTHOR, K_DATE, K_HOW_PUBLISHED, 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 source that does not fit any of the other types
#[derive(Clone, Debug)]
pub struct Misc {
pub author: Option<String>,
pub title: Option<String>,
pub url: Option<String>,
pub how_published: Option<String>,
pub date: Option<LocalDate>,
}
@ -15,8 +20,33 @@ impl Misc {
Self {
author: None,
title: None,
url: None,
how_published: None,
date: None,
}
}
}
impl FromHashMap for Misc {
fn from_hash_map(map: &HashMap<String, String, RandomState>) -> Option<Box<Self>> {
let mut misc = Misc::new();
if let Some(author) = map.get(K_AUTHOR) {
misc.author = Some(author.clone())
}
if let Some(title) = map.get(K_TITLE) {
misc.title = Some(title.clone())
}
if let Some(url) = map.get(K_URL) {
misc.url = Some(url.clone())
}
if let Some(how_pub) = map.get(K_HOW_PUBLISHED) {
misc.how_published = Some(how_pub.clone());
}
if let Some(date) = map.get(K_DATE) {
misc.date = parse_date(date);
}
Some(Box::new(misc))
}
}

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

@ -116,4 +116,15 @@ mod tests {
let entry = BibliographyEntry::from_hash_map(&map).unwrap();
assert_eq!(entry.bib_type.name(), "manual".to_string())
}
#[test]
fn it_creates_misc_from_hashmaps() {
let mut map: HashMap<String, String> = HashMap::new();
map.insert("key".to_string(), "test_entry".to_string());
map.insert("type".to_string(), "misc".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(), "misc".to_string())
}
}

Loading…
Cancel
Save