Wrap the intermediate Registry in an IntermediateContext.

This is currently just to maintain consistency with the render phase's RenderContext but in the future it should allow us to include immutable data that is not locked behind the ArcMutex.
This commit is contained in:
Tom Alexander
2023-12-21 13:53:56 -05:00
parent 2ae4839ce0
commit 8b85c02ef1
34 changed files with 266 additions and 229 deletions

View File

@@ -5,10 +5,11 @@ use std::collections::HashMap;
use super::ast_node::IAstNode;
use super::ast_node::IntoIAstNode;
use super::RefRegistry;
use super::IntermediateContext;
type IdCounter = u16;
#[derive(Debug)]
pub(crate) struct Registry<'orig, 'parse> {
id_counter: IdCounter,
targets: HashMap<&'parse str, String>,
@@ -47,15 +48,15 @@ impl<'orig, 'parse> Registry<'orig, 'parse> {
///
/// This needs to be incremented to be 1-indexed for render.
pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
registry: RefRegistry<'orig, 'parse>,
intermediate_context: IntermediateContext<'orig, 'parse>,
label: Option<&'parse str>,
definition: &'orig Vec<Object<'parse>>,
) -> Result<(usize, usize), CustomError> {
if let None = label {
// If it has no label then it must always get a new ID.
let contents = convert_reference_contents(registry.clone(), definition).await?;
let contents = convert_reference_contents(intermediate_context.clone(), definition).await?;
let pos = {
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
registry.footnote_ids.push((None, contents));
registry.footnote_ids.len() - 1
};
@@ -63,8 +64,8 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
}
let reference_count = if let Some(label) = label {
promote_footnote_definition(registry.clone(), label).await?;
let mut registry = registry.lock().unwrap();
promote_footnote_definition(intermediate_context.clone(), label).await?;
let mut registry = intermediate_context.registry.lock().unwrap();
let reference_count = registry
.footnote_reference_counts
.entry(label)
@@ -75,7 +76,8 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
0
};
let existing_index = registry
let existing_index = intermediate_context
.registry
.lock()
.unwrap()
.footnote_ids
@@ -83,8 +85,9 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
.position(|(id, _definition)| *id == label);
if let Some(existing_id) = existing_index {
if !definition.is_empty() {
let contents = convert_reference_contents(registry.clone(), definition).await?;
let mut registry = registry.lock().unwrap();
let contents =
convert_reference_contents(intermediate_context.clone(), definition).await?;
let mut registry = intermediate_context.registry.lock().unwrap();
let entry = registry
.footnote_ids
.get_mut(existing_id)
@@ -94,13 +97,13 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
Ok((existing_id, reference_count))
} else {
let existing_id = {
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
registry.footnote_ids.push((label, Vec::new()));
registry.footnote_ids.len() - 1
};
let contents = convert_reference_contents(registry.clone(), definition).await?;
let contents = convert_reference_contents(intermediate_context.clone(), definition).await?;
{
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
let entry = registry
.footnote_ids
.get_mut(existing_id)
@@ -113,24 +116,24 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
/// Update the definition to a footnote but do not mark it as referenced.
pub(crate) async fn register_footnote_definition<'orig, 'parse>(
registry: RefRegistry<'orig, 'parse>,
intermediate_context: IntermediateContext<'orig, 'parse>,
label: &'parse str,
definition: &'orig Vec<Element<'parse>>,
) -> Result<(), CustomError> {
let has_existing: bool = {
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
registry
.footnote_ids
.iter_mut()
.any(|(id, _definition)| *id == Some(label))
};
if !has_existing {
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
registry.on_deck_footnote_ids.insert(label, definition);
return Ok(());
}
let contents = convert_definition_contents(registry.clone(), definition).await?;
let mut registry = registry.lock().unwrap();
let contents = convert_definition_contents(intermediate_context.clone(), definition).await?;
let mut registry = intermediate_context.registry.lock().unwrap();
if let Some((_existing_id, existing_definition)) = registry
.footnote_ids
.iter_mut()
@@ -142,13 +145,13 @@ pub(crate) async fn register_footnote_definition<'orig, 'parse>(
}
async fn convert_reference_contents<'orig, 'parse>(
registry: RefRegistry<'orig, 'parse>,
intermediate_context: IntermediateContext<'orig, 'parse>,
contents: &'orig Vec<Object<'parse>>,
) -> Result<Vec<IAstNode>, CustomError> {
let contents = {
let mut ret = Vec::new();
for obj in contents.iter() {
ret.push(obj.into_ast_node(registry.clone()).await?);
ret.push(obj.into_ast_node(intermediate_context.clone()).await?);
}
ret
};
@@ -157,13 +160,13 @@ async fn convert_reference_contents<'orig, 'parse>(
}
async fn convert_definition_contents<'orig, 'parse>(
registry: RefRegistry<'orig, 'parse>,
intermediate_context: IntermediateContext<'orig, 'parse>,
contents: &'orig Vec<Element<'parse>>,
) -> Result<Vec<IAstNode>, CustomError> {
let contents = {
let mut ret = Vec::new();
for obj in contents.iter() {
ret.push(obj.into_ast_node(registry.clone()).await?);
ret.push(obj.into_ast_node(intermediate_context.clone()).await?);
}
ret
};
@@ -173,23 +176,23 @@ async fn convert_definition_contents<'orig, 'parse>(
/// Take a footnote definition that has not yet received a reference and move it into the active footnotes.
pub(crate) async fn promote_footnote_definition<'orig, 'parse>(
registry: RefRegistry<'orig, 'parse>,
intermediate_context: IntermediateContext<'orig, 'parse>,
label: &'parse str,
) -> Result<(), CustomError> {
let definition = {
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
let definition = registry.on_deck_footnote_ids.remove(label);
definition
};
if let Some(elements) = definition {
let existing_id = {
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
registry.footnote_ids.push((Some(label), Vec::new()));
registry.footnote_ids.len() - 1
};
let contents = convert_definition_contents(registry.clone(), elements).await?;
let contents = convert_definition_contents(intermediate_context.clone(), elements).await?;
{
let mut registry = registry.lock().unwrap();
let mut registry = intermediate_context.registry.lock().unwrap();
let entry = registry
.footnote_ids
.get_mut(existing_id)