Add BibEntries, BibTypes and dictionaries

main
trivernis 4 years ago
parent 01bec2fdcf
commit 3efe9df1c4

@ -0,0 +1,31 @@
use std::sync::{Arc, Mutex};
use crate::references::anchor::BibListAnchor;
use crate::bibliography::bibliography_dict::BibliographyDictionary;
/// The root manager for that should be used for further reference operations that
/// go beyond insertion.
#[derive(Clone, Debug)]
pub struct BibManager {
root_ref_anchor: Arc<Mutex<BibListAnchor>>,
entry_dictionary: Arc<Mutex<BibliographyDictionary>>,
}
impl BibManager {
/// Creates a new BibRefManager with an empty root anchor
pub fn new() -> Self {
Self {
root_ref_anchor: Arc::new(Mutex::new(BibListAnchor::new())),
entry_dictionary: Arc::new(Mutex::new(BibliographyDictionary::new())),
}
}
/// Returns the BibRefManagers root anchor that.
pub fn root_ref_anchor(&self) -> Arc<Mutex<BibListAnchor>> {
Arc::clone(&self.root_ref_anchor)
}
/// Returns the reference to the entry dictionary
pub fn entry_dictionary(&self) -> Arc<Mutex<BibliographyDictionary>> {
Arc::clone(&self.entry_dictionary)
}
}

@ -0,0 +1,16 @@
/// A type of bibliography entry
#[derive(Clone, Debug)]
pub enum BibliographyType {
Article,
Book,
Booklet,
InBook,
InCollection,
Manual,
Thesis,
TechReport,
Unpublished,
Misc,
Url,
Repository,
}

@ -0,0 +1,35 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use crate::bibliography::bibliography_entry::{BibliographyEntryReference, BibliographyEntry};
/// A dictionary that contains all bibliography entries
#[derive(Clone, Debug)]
pub struct BibliographyDictionary {
entries: HashMap<String, BibliographyEntryReference>,
}
impl BibliographyDictionary {
/// Creates a new empty BibliographyDictionary
pub fn new() -> Self {
Self {
entries: HashMap::new(),
}
}
/// Inserts a bibliography entry into the map
pub fn insert(&mut self, entry: BibliographyEntry) {
self.entries.insert(entry.key(), Arc::new(Mutex::new(entry)));
}
/// Returns the reference to the bibliography entry with the given key
pub fn get(&mut self, key: &str) -> Option<BibliographyEntryReference> {
if let Some(entry) = self.entries.get(&key.to_string()) {
Some(Arc::clone(entry))
} else {
None
}
}
}

@ -0,0 +1,30 @@
use std::sync::{Arc, Mutex};
use crate::bibliography::bib_types::BibliographyType;
/// A single bibliography entry
#[derive(Clone, Debug)]
pub struct BibliographyEntry {
key: String,
note: Option<String>,
bib_type: BibliographyType,
}
pub type BibliographyEntryReference = Arc<Mutex<BibliographyEntry>>;
impl BibliographyEntry {
/// Creates a new bibliography entry with the given key
pub fn new(key: String) -> Self {
Self {
key,
note: None,
bib_type: BibliographyType::Misc,
}
}
/// Returns the key of the bibliography entry
pub fn key(&self) -> String {
self.key.clone()
}
}

@ -0,0 +1,3 @@
pub mod bibliography_entry;
pub mod bibliography_dict;
pub mod bib_types;

@ -1,14 +1,16 @@
pub mod references; pub mod references;
pub mod bibliography;
pub mod bib_manager;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::references::reference_manager::BibRefManager;
use crate::references::bib_reference::BibRef; use crate::references::bib_reference::BibRef;
use crate::bib_manager::BibManager;
#[test] #[test]
fn it_inserts_and_flattens() { fn it_inserts_and_flattens() {
let manager = BibRefManager::new(); let manager = BibManager::new();
let root_anchor = manager.root_anchor(); let root_anchor = manager.root_ref_anchor();
let mut root_anchor = root_anchor.lock().unwrap(); let mut root_anchor = root_anchor.lock().unwrap();
root_anchor.insert(BibRef::new("test".to_string())); root_anchor.insert(BibRef::new("test".to_string()));
let child_anchor = root_anchor.create_anchor(); let child_anchor = root_anchor.create_anchor();

@ -1,13 +1,31 @@
use crate::bibliography::bibliography_entry::BibliographyEntryReference;
use std::sync::{Arc, Mutex};
/// A reference to a bibliography entry
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct BibRef { pub struct BibRef {
key: String, key: String,
anchor: Arc<Mutex<BibRefAnchor>>,
}
/// An anchor of a BibRef that can be used in a DOM to remember the place of a BibRef
/// and to access the corresponding bibliography entry.
#[derive(Clone, Debug)]
pub struct BibRefAnchor {
entry: Option<BibliographyEntryReference>
} }
impl BibRef { impl BibRef {
/// Creates a new BibRef with a given key /// Creates a new BibRef with a given key
pub fn new(key: String) -> Self { pub fn new(key: String) -> Self {
Self { Self {
key key,
anchor: Arc::new(Mutex::new(BibRefAnchor {entry: None}))
} }
} }
/// Returns the anchor of the BibRef
pub fn anchor(&self) -> Arc<Mutex<BibRefAnchor>> {
Arc::clone(&self.anchor)
}
} }

@ -1,3 +1,2 @@
pub mod reference_manager;
pub mod bib_reference; pub mod bib_reference;
pub mod anchor; pub mod anchor;

@ -1,23 +0,0 @@
use std::sync::{Arc, Mutex};
use crate::references::anchor::BibListAnchor;
/// The root manager for references that should be used for further reference operations that
/// go beyond insertion.
#[derive(Clone, Debug)]
pub struct BibRefManager {
root_anchor: Arc<Mutex<BibListAnchor>>,
}
impl BibRefManager {
/// Creates a new BibRefManager with an empty root anchor
pub fn new() -> Self {
Self {
root_anchor: Arc::new(Mutex::new(BibListAnchor::new()))
}
}
/// Returns the BibRefManagers root anchor that.
pub fn root_anchor(&self) -> Arc<Mutex<BibListAnchor>> {
Arc::clone(&self.root_anchor)
}
}
Loading…
Cancel
Save