Add function to parse a toml bibliography file

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

@ -1,7 +1,7 @@
[package]
name = "bibliographix"
description = "A bibliography management crate."
version = "0.3.2"
version = "0.4.0"
authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018"
license = "Apache-2.0"
@ -12,4 +12,5 @@ repository = "https://github.com/Trivernis/bibliographix"
[dependencies]
chrono = "0.4.15"
chrono-english = "0.1.4"
chrono-english = "0.1.4"
toml = "0.5.6"

@ -1,7 +1,14 @@
use crate::bibliography::bibliography_dict::BibliographyDictionary;
use crate::bibliography::bibliography_entry::BibliographyEntryReference;
use crate::bibliography::bibliography_entry::{BibliographyEntry, BibliographyEntryReference};
use crate::bibliography::keys::K_KEY;
use crate::bibliography::FromHashMap;
use crate::references::anchor::BibListAnchor;
use std::collections::HashMap;
use std::io;
use std::io::BufRead;
use std::iter::FromIterator;
use std::sync::{Arc, Mutex};
use toml::Value;
/// The root manager for that should be used for further reference operations that
/// go beyond insertion.
@ -71,4 +78,36 @@ impl BibManager {
entries
}
/// Reads a toml bibliography file and inserts each entry into the dictionary
pub fn read_bib_file(&self, reader: &mut impl BufRead) -> io::Result<()> {
let mut contents = String::new();
reader.read_to_string(&mut contents)?;
let bib_content = contents.parse::<Value>()?;
let mut entry_dict = self.entry_dictionary.lock().unwrap();
if let Some(table) = bib_content.as_table() {
let mut entries = table
.iter()
.filter_map(|(k, v)| {
let entry_iter = v
.as_table()?
.iter()
.filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_string())));
let mut entry_map: HashMap<String, String> = HashMap::from_iter(entry_iter);
entry_map.insert(K_KEY.to_string(), k.clone());
Some(*BibliographyEntry::from_hash_map(&entry_map)?)
})
.collect::<Vec<BibliographyEntry>>();
while let Some(entry) = entries.pop() {
entry_dict.insert(entry)
}
}
Ok(())
}
}

Loading…
Cancel
Save