use super::Element;
use super::clone::{elements_to_owned, string_to_owned};
use std::borrow::Cow;
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct Bibliography<'t>(Vec<(Cow<'t, str>, Vec<Element<'t>>)>);
impl<'t> Bibliography<'t> {
pub fn new() -> Self {
Bibliography::default()
}
pub fn add(&mut self, label: Cow<'t, str>, elements: Vec<Element<'t>>) {
if self.get(&label).is_some() {
warn!("Duplicate reference in bibliography: {label}");
return;
}
self.0.push((label, elements));
}
pub fn get(&self, label: &str) -> Option<(usize, &[Element<'t>])> {
for (index, (ref_label, elements)) in self.0.iter().enumerate() {
if label == ref_label {
return Some((index + 1, elements));
}
}
None
}
#[inline]
pub fn slice(&self) -> &[(Cow<'t, str>, Vec<Element<'t>>)] {
&self.0
}
pub fn to_owned(&self) -> Bibliography<'static> {
Bibliography(
self.0
.iter()
.map(|(label, elements)| {
(string_to_owned(label), elements_to_owned(elements))
})
.collect(),
)
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct BibliographyList<'t>(Vec<Bibliography<'t>>);
impl<'t> BibliographyList<'t> {
pub fn new() -> Self {
BibliographyList::default()
}
pub fn push(&mut self, bibliography: Bibliography<'t>) {
self.0.push(bibliography);
}
pub fn append(&mut self, other: &mut Self) {
self.0.append(&mut other.0);
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn next_index(&self) -> usize {
self.0.len()
}
pub fn get_reference(&self, label: &str) -> Option<(usize, &[Element<'t>])> {
for bibliography in &self.0 {
let reference = bibliography.get(label);
if reference.is_some() {
return reference;
}
}
None
}
pub fn get_bibliography(&self, index: usize) -> &Bibliography<'t> {
&self.0[index]
}
pub fn to_owned(&self) -> BibliographyList<'static> {
BibliographyList(self.0.iter().map(|b| b.to_owned()).collect())
}
}