From 583e0913087a37ab7e119921d1f287e63b68a46d Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 2 Sep 2020 19:26:24 +0200 Subject: [PATCH] Add function to insert a hashmap directly Signed-off-by: trivernis --- src/bibliography/bibliography_dict.rs | 18 +++++++++++++----- src/bibliography/bibliography_entry.rs | 4 +--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/bibliography/bibliography_dict.rs b/src/bibliography/bibliography_dict.rs index 17c3020..6548666 100644 --- a/src/bibliography/bibliography_dict.rs +++ b/src/bibliography/bibliography_dict.rs @@ -1,4 +1,6 @@ use crate::bibliography::bibliography_entry::{BibliographyEntry, BibliographyEntryReference}; +use crate::bibliography::keys::K_KEY; +use crate::bibliography::FromHashMap; use std::collections::HashMap; use std::sync::{Arc, Mutex}; @@ -22,12 +24,18 @@ impl BibliographyDictionary { .insert(entry.key(), Arc::new(Mutex::new(entry))); } + /// Inserts a bibliography entry represented as a HashMap + pub fn insert_map(&mut self, map: &HashMap) -> Option<()> { + let key = map.get(K_KEY)?; + let entry = *BibliographyEntry::from_hash_map(map)?; + self.entries + .insert(key.clone(), Arc::new(Mutex::new(entry))); + + Some(()) + } + /// Returns the reference to the bibliography entry with the given key pub fn get(&mut self, key: &str) -> Option { - if let Some(entry) = self.entries.get(&key.to_string()) { - Some(Arc::clone(entry)) - } else { - None - } + self.entries.get(&key.to_string()).cloned() } } diff --git a/src/bibliography/bibliography_entry.rs b/src/bibliography/bibliography_entry.rs index e75e7ee..39d9bab 100644 --- a/src/bibliography/bibliography_entry.rs +++ b/src/bibliography/bibliography_entry.rs @@ -41,9 +41,7 @@ impl FromHashMap for BibliographyEntry { let mut entry = Self::new(key.clone()); - if let Some(note) = map.get(K_NOTE) { - entry.note = Some(note.clone()) - } + entry.note = map.get(K_NOTE).cloned(); entry.bib_type = *bib_type; entry.raw_fields = map.clone();