Add function to insert a hashmap directly

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 4 years ago
parent f12494b836
commit 583e091308
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,4 +1,6 @@
use crate::bibliography::bibliography_entry::{BibliographyEntry, BibliographyEntryReference}; use crate::bibliography::bibliography_entry::{BibliographyEntry, BibliographyEntryReference};
use crate::bibliography::keys::K_KEY;
use crate::bibliography::FromHashMap;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -22,12 +24,18 @@ impl BibliographyDictionary {
.insert(entry.key(), Arc::new(Mutex::new(entry))); .insert(entry.key(), Arc::new(Mutex::new(entry)));
} }
/// Inserts a bibliography entry represented as a HashMap
pub fn insert_map(&mut self, map: &HashMap<String, String>) -> 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 /// Returns the reference to the bibliography entry with the given key
pub fn get(&mut self, key: &str) -> Option<BibliographyEntryReference> { pub fn get(&mut self, key: &str) -> Option<BibliographyEntryReference> {
if let Some(entry) = self.entries.get(&key.to_string()) { self.entries.get(&key.to_string()).cloned()
Some(Arc::clone(entry))
} else {
None
}
} }
} }

@ -41,9 +41,7 @@ impl FromHashMap for BibliographyEntry {
let mut entry = Self::new(key.clone()); let mut entry = Self::new(key.clone());
if let Some(note) = map.get(K_NOTE) { entry.note = map.get(K_NOTE).cloned();
entry.note = Some(note.clone())
}
entry.bib_type = *bib_type; entry.bib_type = *bib_type;
entry.raw_fields = map.clone(); entry.raw_fields = map.clone();

Loading…
Cancel
Save