From af3ba931d2577bb7e8a4a379ba23774603f4b8f4 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 1 Sep 2020 21:35:03 +0200 Subject: [PATCH] Add FromHashMap trait and implementations Implemenations for: - BibliographyEntry - BibliographyType Signed-off-by: trivernis --- src/bibliography/bib_types/mod.rs | 14 +++++++++++++ src/bibliography/bibliography_entry.rs | 27 ++++++++++++++++++++++++++ src/bibliography/mod.rs | 8 ++++++++ 3 files changed, 49 insertions(+) diff --git a/src/bibliography/bib_types/mod.rs b/src/bibliography/bib_types/mod.rs index a08a06a..0cc1e06 100644 --- a/src/bibliography/bib_types/mod.rs +++ b/src/bibliography/bib_types/mod.rs @@ -10,7 +10,9 @@ use crate::bibliography::bib_types::tech_report::TechReport; use crate::bibliography::bib_types::thesis::Thesis; use crate::bibliography::bib_types::unpublished::Unpublished; use crate::bibliography::bib_types::website::Website; +use crate::bibliography::FromHashMap; use chrono::{Date, Local}; +use std::collections::HashMap; pub mod article; pub mod book; @@ -43,3 +45,15 @@ pub enum BibliographyType { Website(Website), Repository(Repository), } + +impl FromHashMap for BibliographyType { + fn from_hash_map(map: &HashMap) -> Option> { + if map.contains_key("type") { + match map.get("type").unwrap().to_lowercase() { + _ => None, + } + } else { + None + } + } +} diff --git a/src/bibliography/bibliography_entry.rs b/src/bibliography/bibliography_entry.rs index 04c43aa..f71e029 100644 --- a/src/bibliography/bibliography_entry.rs +++ b/src/bibliography/bibliography_entry.rs @@ -1,5 +1,8 @@ use crate::bibliography::bib_types::misc::Misc; use crate::bibliography::bib_types::BibliographyType; +use crate::bibliography::FromHashMap; +use std::collections::hash_map::RandomState; +use std::collections::HashMap; use std::sync::{Arc, Mutex}; /// A single bibliography entry @@ -8,6 +11,7 @@ pub struct BibliographyEntry { key: String, pub note: Option, pub bib_type: BibliographyType, + pub raw_fields: HashMap, } pub type BibliographyEntryReference = Arc>; @@ -19,6 +23,7 @@ impl BibliographyEntry { key, note: None, bib_type: BibliographyType::Misc(Misc::new()), + raw_fields: HashMap::new(), } } @@ -27,3 +32,25 @@ impl BibliographyEntry { self.key.clone() } } + +impl FromHashMap for BibliographyEntry { + fn from_hash_map(map: &HashMap) -> Option> { + if let Some(key) = map.get("key") { + if let Some(bib_type) = BibliographyType::from_hash_map(map) { + let mut entry = Self::new(key.clone()); + + if let Some(note) = map.get("note") { + entry.note = Some(note.clone()) + } + entry.bib_type = *bib_type; + entry.raw_fields = map.clone(); + + Some(Box::new(entry)) + } else { + None + } + } else { + None + } + } +} diff --git a/src/bibliography/mod.rs b/src/bibliography/mod.rs index e8b0d5a..533c22a 100644 --- a/src/bibliography/mod.rs +++ b/src/bibliography/mod.rs @@ -1,3 +1,11 @@ +use std::collections::HashMap; + pub mod bib_types; pub mod bibliography_dict; pub mod bibliography_entry; + +/// A trait that provides the from_has_map function that can be used +/// to create a bibliography source type from a hashmap +pub trait FromHashMap { + fn from_hash_map(map: &HashMap) -> Option>; +}