Add FromHashMap trait and implementations

Implemenations for:
- BibliographyEntry
- BibliographyType

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

@ -10,7 +10,9 @@ use crate::bibliography::bib_types::tech_report::TechReport;
use crate::bibliography::bib_types::thesis::Thesis; use crate::bibliography::bib_types::thesis::Thesis;
use crate::bibliography::bib_types::unpublished::Unpublished; use crate::bibliography::bib_types::unpublished::Unpublished;
use crate::bibliography::bib_types::website::Website; use crate::bibliography::bib_types::website::Website;
use crate::bibliography::FromHashMap;
use chrono::{Date, Local}; use chrono::{Date, Local};
use std::collections::HashMap;
pub mod article; pub mod article;
pub mod book; pub mod book;
@ -43,3 +45,15 @@ pub enum BibliographyType {
Website(Website), Website(Website),
Repository(Repository), Repository(Repository),
} }
impl FromHashMap for BibliographyType {
fn from_hash_map(map: &HashMap<String, String>) -> Option<Box<Self>> {
if map.contains_key("type") {
match map.get("type").unwrap().to_lowercase() {
_ => None,
}
} else {
None
}
}
}

@ -1,5 +1,8 @@
use crate::bibliography::bib_types::misc::Misc; use crate::bibliography::bib_types::misc::Misc;
use crate::bibliography::bib_types::BibliographyType; 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}; use std::sync::{Arc, Mutex};
/// A single bibliography entry /// A single bibliography entry
@ -8,6 +11,7 @@ pub struct BibliographyEntry {
key: String, key: String,
pub note: Option<String>, pub note: Option<String>,
pub bib_type: BibliographyType, pub bib_type: BibliographyType,
pub raw_fields: HashMap<String, String>,
} }
pub type BibliographyEntryReference = Arc<Mutex<BibliographyEntry>>; pub type BibliographyEntryReference = Arc<Mutex<BibliographyEntry>>;
@ -19,6 +23,7 @@ impl BibliographyEntry {
key, key,
note: None, note: None,
bib_type: BibliographyType::Misc(Misc::new()), bib_type: BibliographyType::Misc(Misc::new()),
raw_fields: HashMap::new(),
} }
} }
@ -27,3 +32,25 @@ impl BibliographyEntry {
self.key.clone() self.key.clone()
} }
} }
impl FromHashMap for BibliographyEntry {
fn from_hash_map(map: &HashMap<String, String, RandomState>) -> Option<Box<Self>> {
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
}
}
}

@ -1,3 +1,11 @@
use std::collections::HashMap;
pub mod bib_types; pub mod bib_types;
pub mod bibliography_dict; pub mod bibliography_dict;
pub mod bibliography_entry; 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<String, String>) -> Option<Box<Self>>;
}

Loading…
Cancel
Save