Fix build by making the registry guarded by an ArcMutex.
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
// use super::ast_node::IntoIAstNode;
|
||||
use crate::error::CustomError;
|
||||
@@ -7,6 +9,7 @@ use organic::types::Element;
|
||||
use organic::types::Object;
|
||||
|
||||
use super::ast_node::IAstNode;
|
||||
use super::RefRegistry;
|
||||
|
||||
type IdCounter = u16;
|
||||
|
||||
@@ -40,63 +43,75 @@ impl<'orig, 'parse> Registry<'orig, 'parse> {
|
||||
.map(|(_label, definition)| definition)
|
||||
.enumerate()
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
label: Option<&'parse str>,
|
||||
definition: &'orig Vec<Object<'parse>>,
|
||||
) -> Result<usize, CustomError> {
|
||||
if let None = label {
|
||||
// If it has no label then it must always get a new ID.
|
||||
let contents = convert_reference_contents(self, definition).await?;
|
||||
self.footnote_ids.push((None, contents));
|
||||
return Ok(self.footnote_ids.len() - 1);
|
||||
}
|
||||
|
||||
if let Some(existing_id) = self
|
||||
.footnote_ids
|
||||
.iter()
|
||||
.position(|(id, _definition)| *id == label)
|
||||
{
|
||||
if !definition.is_empty() {
|
||||
let contents = convert_reference_contents(self, definition).await?;
|
||||
let entry = self
|
||||
.footnote_ids
|
||||
.get_mut(existing_id)
|
||||
.expect("If-statement proves this to be Some.");
|
||||
entry.1 = contents;
|
||||
}
|
||||
Ok(existing_id)
|
||||
} else {
|
||||
let contents = convert_reference_contents(self, definition).await?;
|
||||
self.footnote_ids.push((label, contents));
|
||||
Ok(self.footnote_ids.len() - 1)
|
||||
}
|
||||
/// 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<'orig, 'parse>(
|
||||
registry: RefRegistry<'orig, 'parse>,
|
||||
label: Option<&'parse str>,
|
||||
definition: &'orig Vec<Object<'parse>>,
|
||||
) -> Result<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 pos = {
|
||||
let mut registry = registry.lock().unwrap();
|
||||
registry.footnote_ids.push((None, contents));
|
||||
registry.footnote_ids.len() - 1
|
||||
};
|
||||
return Ok(pos);
|
||||
}
|
||||
|
||||
/// Update the definition to a footnote but do not mark it as referenced.
|
||||
pub(crate) async fn register_footnote_definition<'reg>(
|
||||
&'reg mut self,
|
||||
label: &'parse str,
|
||||
definition: &'orig Vec<Element<'parse>>,
|
||||
) -> Result<(), CustomError> {
|
||||
let contents = convert_definition_contents(self, definition).await?;
|
||||
if let Some((_existing_id, existing_definition)) = self
|
||||
.footnote_ids
|
||||
.iter_mut()
|
||||
.find(|(id, _definition)| *id == Some(label))
|
||||
{
|
||||
*existing_definition = contents;
|
||||
let existing_index = registry
|
||||
.lock()
|
||||
.unwrap()
|
||||
.footnote_ids
|
||||
.iter()
|
||||
.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 entry = registry
|
||||
.footnote_ids
|
||||
.get_mut(existing_id)
|
||||
.expect("If-statement proves this to be Some.");
|
||||
entry.1 = contents;
|
||||
}
|
||||
Ok(())
|
||||
Ok(existing_id)
|
||||
} else {
|
||||
let contents = convert_reference_contents(registry.clone(), definition).await?;
|
||||
let pos = {
|
||||
let mut registry = registry.lock().unwrap();
|
||||
registry.footnote_ids.push((label, contents));
|
||||
registry.footnote_ids.len() - 1
|
||||
};
|
||||
Ok(pos)
|
||||
}
|
||||
}
|
||||
|
||||
async fn convert_reference_contents<'reg, 'orig, 'parse>(
|
||||
registry: &'reg mut Registry<'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>,
|
||||
label: &'parse str,
|
||||
definition: &'orig Vec<Element<'parse>>,
|
||||
) -> Result<(), CustomError> {
|
||||
let contents = convert_definition_contents(registry.clone(), definition).await?;
|
||||
let mut registry = registry.lock().unwrap();
|
||||
if let Some((_existing_id, existing_definition)) = registry
|
||||
.footnote_ids
|
||||
.iter_mut()
|
||||
.find(|(id, _definition)| *id == Some(label))
|
||||
{
|
||||
*existing_definition = contents;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn convert_reference_contents<'orig, 'parse>(
|
||||
registry: RefRegistry<'orig, 'parse>,
|
||||
contents: &'orig Vec<Object<'parse>>,
|
||||
) -> Result<Vec<IAstNode>, CustomError> {
|
||||
let contents = {
|
||||
@@ -111,8 +126,8 @@ async fn convert_reference_contents<'reg, 'orig, 'parse>(
|
||||
Ok(contents)
|
||||
}
|
||||
|
||||
async fn convert_definition_contents<'reg, 'orig, 'parse>(
|
||||
registry: &'reg mut Registry<'orig, 'parse>,
|
||||
async fn convert_definition_contents<'orig, 'parse>(
|
||||
registry: RefRegistry<'orig, 'parse>,
|
||||
contents: &'orig Vec<Element<'parse>>,
|
||||
) -> Result<Vec<IAstNode>, CustomError> {
|
||||
let contents = {
|
||||
|
||||
Reference in New Issue
Block a user