Assign the reference counts.

This commit is contained in:
Tom Alexander
2023-10-29 22:27:47 -04:00
parent ff03140007
commit 0da37b25e3
2 changed files with 20 additions and 9 deletions

View File

@@ -13,6 +13,7 @@ pub(crate) struct Registry<'orig, 'parse> {
id_counter: IdCounter,
targets: HashMap<&'parse str, String>,
footnote_ids: Vec<(Option<&'parse str>, Vec<IAstNode>)>,
footnote_reference_counts: HashMap<&'parse str, usize>,
on_deck_footnote_ids: HashMap<&'parse str, &'orig Vec<Element<'parse>>>,
}
@@ -22,6 +23,7 @@ impl<'orig, 'parse> Registry<'orig, 'parse> {
id_counter: 0,
targets: HashMap::new(),
footnote_ids: Vec::new(),
footnote_reference_counts: HashMap::new(),
on_deck_footnote_ids: HashMap::new(),
}
}
@@ -48,7 +50,7 @@ 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> {
) -> 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?;
@@ -57,12 +59,21 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
registry.footnote_ids.push((None, contents));
registry.footnote_ids.len() - 1
};
return Ok(pos);
return Ok((pos, 0));
}
if let Some(label) = label {
let reference_count = if let Some(label) = label {
promote_footnote_definition(registry.clone(), label).await?;
}
let mut registry = registry.lock().unwrap();
let reference_count = registry
.footnote_reference_counts
.entry(label)
.and_modify(|count| *count += 1)
.or_insert(0);
*reference_count
} else {
0
};
let existing_index = registry
.lock()
@@ -80,7 +91,7 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
.expect("If-statement proves this to be Some.");
entry.1 = contents;
}
Ok(existing_id)
Ok((existing_id, reference_count))
} else {
let existing_id = {
let mut registry = registry.lock().unwrap();
@@ -96,7 +107,7 @@ pub(crate) async fn get_footnote_reference_id<'orig, 'parse>(
.expect("If-statement proves this to be Some.");
entry.1 = contents;
}
Ok(existing_id)
Ok((existing_id, reference_count))
}
}