pub mod attribute;
mod align;
mod anchor;
mod bibliography;
mod clear_float;
mod clone;
mod code;
mod container;
mod date;
mod definition_list;
mod element;
mod embed;
mod file_source;
mod heading;
mod link;
mod list;
mod module;
mod partial;
mod ruby;
mod tab;
mod table;
mod tag;
mod variables;
pub use self::align::*;
pub use self::anchor::*;
pub use self::attribute::AttributeMap;
pub use self::bibliography::*;
pub use self::clear_float::*;
pub use self::code::CodeBlock;
pub use self::container::*;
pub use self::date::DateItem;
pub use self::definition_list::*;
pub use self::element::*;
pub use self::embed::*;
pub use self::file_source::*;
pub use self::heading::*;
pub use self::link::*;
pub use self::list::*;
pub use self::module::*;
pub use self::partial::*;
pub use self::ruby::*;
pub use self::tab::*;
pub use self::table::*;
pub use self::tag::*;
pub use self::variables::*;
use self::clone::{elements_lists_to_owned, elements_to_owned, string_to_owned};
use crate::parsing::{ParseError, ParseOutcome};
use std::borrow::Cow;
use std::ops::Not;
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct SyntaxTree<'t> {
pub elements: Vec<Element<'t>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub table_of_contents: Vec<Element<'t>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub html_blocks: Vec<Cow<'t, str>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub code_blocks: Vec<CodeBlock<'t>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub footnotes: Vec<Vec<Element<'t>>>,
#[serde(default, skip_serializing_if = "Not::not")]
pub needs_footnote_block: bool,
#[serde(default, skip_serializing_if = "BibliographyList::is_empty")]
pub bibliographies: BibliographyList<'t>,
#[serde(skip)]
pub wikitext_len: usize,
}
impl<'t> SyntaxTree<'t> {
pub(crate) fn from_element_result(
elements: Vec<Element<'t>>,
errors: Vec<ParseError>,
(html_blocks, code_blocks): (Vec<Cow<'t, str>>, Vec<CodeBlock<'t>>),
table_of_contents: Vec<Element<'t>>,
(footnotes, needs_footnote_block): (Vec<Vec<Element<'t>>>, bool),
bibliographies: BibliographyList<'t>,
wikitext_len: usize,
) -> ParseOutcome<Self> {
let tree = SyntaxTree {
elements,
table_of_contents,
html_blocks,
code_blocks,
footnotes,
needs_footnote_block,
bibliographies,
wikitext_len,
};
ParseOutcome::new(tree, errors)
}
pub fn to_owned(&self) -> SyntaxTree<'static> {
SyntaxTree {
elements: elements_to_owned(&self.elements),
table_of_contents: elements_to_owned(&self.table_of_contents),
html_blocks: self
.html_blocks
.iter()
.map(|html| string_to_owned(html))
.collect(),
code_blocks: self
.code_blocks
.iter()
.map(|code| code.to_owned())
.collect(),
footnotes: elements_lists_to_owned(&self.footnotes),
needs_footnote_block: self.needs_footnote_block,
bibliographies: self.bibliographies.to_owned(),
wikitext_len: self.wikitext_len,
}
}
}
#[test]
fn borrowed_to_owned() {
use std::mem;
let tree_1: SyntaxTree<'_> = SyntaxTree::default();
let tree_2: SyntaxTree<'static> = tree_1.to_owned();
mem::drop(tree_1);
let tree_3: SyntaxTree<'static> = tree_2.clone();
mem::drop(tree_3);
}