natter/src/intermediate/registry.rs

129 lines
3.9 KiB
Rust
Raw Normal View History

use std::collections::HashMap;
use std::marker::PhantomData;
// use super::ast_node::IntoIAstNode;
2023-10-29 18:14:10 +00:00
use crate::error::CustomError;
2023-10-29 16:31:48 +00:00
use organic::types::Element;
2023-10-29 16:06:40 +00:00
use organic::types::Object;
2023-10-29 17:51:32 +00:00
use super::ast_node::IAstNode;
type IdCounter = u16;
pub(crate) struct Registry<'orig, 'parse> {
id_counter: IdCounter,
2023-10-27 18:54:54 +00:00
targets: HashMap<&'parse str, String>,
footnote_ids: Vec<(Option<&'parse str>, Vec<IAstNode>)>,
_phantom: PhantomData<&'orig ()>,
}
impl<'orig, 'parse> Registry<'orig, 'parse> {
pub(crate) fn new() -> Registry<'orig, 'parse> {
Registry {
id_counter: 0,
targets: HashMap::new(),
2023-10-29 16:06:40 +00:00
footnote_ids: Vec::new(),
_phantom: PhantomData,
}
}
pub(crate) fn get_target<'reg>(&'reg mut self, body: &'parse str) -> &'reg String {
self.targets.entry(body).or_insert_with(|| {
self.id_counter += 1;
format!("target_{}", self.id_counter)
})
}
2023-10-29 16:06:40 +00:00
2023-10-29 19:36:15 +00:00
pub(crate) fn get_footnote_ids(&self) -> impl Iterator<Item = (usize, &Vec<IAstNode>)> {
self.footnote_ids
.iter()
.map(|(_label, definition)| definition)
.enumerate()
2023-10-29 19:36:15 +00:00
}
2023-10-29 16:06:40 +00:00
/// Get a 0-indexed ID for a footnote.
///
/// This needs to be incremented to be 1-indexed for render.
pub(crate) async fn get_footnote_reference_id<'reg>(
&'reg mut self,
2023-10-29 16:06:40 +00:00
label: Option<&'parse str>,
definition: &'orig Vec<Object<'parse>>,
2023-10-29 18:14:10 +00:00
) -> Result<usize, CustomError> {
2023-10-29 16:06:40 +00:00
if let None = label {
// If it has no label then it must always get a new ID.
2023-10-29 18:14:10 +00:00
let contents = convert_reference_contents(self, definition).await?;
self.footnote_ids.push((None, contents));
return Ok(self.footnote_ids.len() - 1);
2023-10-29 16:06:40 +00:00
}
if let Some(existing_id) = self
.footnote_ids
.iter()
.position(|(id, _definition)| *id == label)
{
if !definition.is_empty() {
2023-10-29 18:14:10 +00:00
let contents = convert_reference_contents(self, definition).await?;
2023-10-29 16:06:40 +00:00
let entry = self
.footnote_ids
.get_mut(existing_id)
.expect("If-statement proves this to be Some.");
2023-10-29 18:14:10 +00:00
entry.1 = contents;
2023-10-29 16:06:40 +00:00
}
2023-10-29 18:14:10 +00:00
Ok(existing_id)
2023-10-29 16:06:40 +00:00
} else {
2023-10-29 18:14:10 +00:00
let contents = convert_reference_contents(self, definition).await?;
self.footnote_ids.push((label, contents));
Ok(self.footnote_ids.len() - 1)
2023-10-29 16:06:40 +00:00
}
}
2023-10-29 16:31:48 +00:00
/// Update the definition to a footnote but do not mark it as referenced.
pub(crate) async fn register_footnote_definition<'reg>(
&'reg mut self,
2023-10-29 16:31:48 +00:00
label: &'parse str,
definition: &'orig Vec<Element<'parse>>,
2023-10-29 18:14:10 +00:00
) -> Result<(), CustomError> {
let contents = convert_definition_contents(self, definition).await?;
if let Some((_existing_id, existing_definition)) = self
2023-10-29 16:31:48 +00:00
.footnote_ids
.iter_mut()
.find(|(id, _definition)| *id == Some(label))
{
2023-10-29 18:14:10 +00:00
*existing_definition = contents;
2023-10-29 16:31:48 +00:00
}
2023-10-29 18:14:10 +00:00
Ok(())
2023-10-29 16:31:48 +00:00
}
}
async fn convert_reference_contents<'reg, 'orig, 'parse>(
registry: &'reg mut Registry<'orig, 'parse>,
contents: &'orig Vec<Object<'parse>>,
2023-10-29 18:14:10 +00:00
) -> Result<Vec<IAstNode>, CustomError> {
let contents = {
let mut ret = Vec::new();
for obj in contents.iter() {
// TODO
// ret.push(obj.into_ast_node(registry).await?);
2023-10-29 18:14:10 +00:00
}
ret
};
Ok(contents)
}
async fn convert_definition_contents<'reg, 'orig, 'parse>(
registry: &'reg mut Registry<'orig, 'parse>,
contents: &'orig Vec<Element<'parse>>,
2023-10-29 18:14:10 +00:00
) -> Result<Vec<IAstNode>, CustomError> {
let contents = {
let mut ret = Vec::new();
for obj in contents.iter() {
// TODO
// ret.push(obj.into_ast_node(registry).await?);
2023-10-29 18:14:10 +00:00
}
ret
};
Ok(contents)
}