Add assigning of bib entries to bib manager

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

@ -28,4 +28,17 @@ impl BibManager {
pub fn entry_dictionary(&self) -> Arc<Mutex<BibliographyDictionary>> {
Arc::clone(&self.entry_dictionary)
}
/// Assigns the corresponding bib entry to each bib reference
pub fn assign_entries_to_references(&self) {
let entry_dict = self.entry_dictionary.lock().unwrap();
let mut root_anchor = self.root_ref_anchor.lock().unwrap();
root_anchor.flatten();
let entries = root_anchor.references();
entries.iter().for_each(|e| {
if let Some(bib) = entry_dict.get(e.key()) {
e.anchor().lock().unwrap().entry = Some(bib)
}
})
}
}

@ -35,7 +35,7 @@ impl BibliographyDictionary {
}
/// Returns the reference to the bibliography entry with the given key
pub fn get(&mut self, key: &str) -> Option<BibliographyEntryReference> {
pub fn get(&self, key: &str) -> Option<BibliographyEntryReference> {
self.entries.get(&key.to_string()).cloned()
}
}

@ -31,6 +31,33 @@ mod tests {
assert_eq!(root_anchor.references().len(), 3)
}
#[test]
fn it_assigns_bib_entries_to_references() {
let manager = BibManager::new();
let ref1_1 = BibRef::new("ref1".to_string());
let anchor1 = ref1_1.anchor();
manager.root_ref_anchor().lock().unwrap().insert(ref1_1);
let ref1_2 = BibRef::new("ref1".to_string());
let anchor2 = ref1_2.anchor();
manager.root_ref_anchor().lock().unwrap().insert(ref1_2);
let ref3 = BibRef::new("ref2".to_string());
let anchor3 = ref3.anchor();
manager.root_ref_anchor().lock().unwrap().insert(ref3);
let mut map: HashMap<String, String> = HashMap::new();
map.insert("key".to_string(), "ref1".to_string());
map.insert("type".to_string(), "article".to_string());
map.insert("author".to_string(), "test".to_string());
map.insert("title".to_string(), "test_title".to_string());
map.insert("journal".to_string(), "test_journal".to_string());
map.insert("date".to_string(), "01.09.2020".to_string());
manager.entry_dictionary().lock().unwrap().insert_map(&map);
manager.assign_entries_to_references();
assert!(anchor1.lock().unwrap().entry.is_some());
assert!(anchor2.lock().unwrap().entry.is_some());
assert!(anchor3.lock().unwrap().entry.is_none());
}
#[test]
fn it_creates_articles_from_hashmaps() {
let mut map: HashMap<String, String> = HashMap::new();

@ -12,7 +12,7 @@ pub struct BibRef {
/// and to access the corresponding bibliography entry.
#[derive(Clone, Debug)]
pub struct BibRefAnchor {
entry: Option<BibliographyEntryReference>,
pub entry: Option<BibliographyEntryReference>,
}
impl BibRef {
@ -28,4 +28,9 @@ impl BibRef {
pub fn anchor(&self) -> Arc<Mutex<BibRefAnchor>> {
Arc::clone(&self.anchor)
}
/// Returns the key of the BibRef
pub fn key(&self) -> &String {
&self.key
}
}

Loading…
Cancel
Save