use crate::block::BlockEvent;
use rustc_hash::FxBuildHasher as FastHashBuilder;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct FootnoteDef {
pub label: String,
pub events: Vec<BlockEvent>,
}
#[derive(Debug, Default)]
pub struct FootnoteStore {
defs: Vec<FootnoteDef>,
by_label: HashMap<String, usize, FastHashBuilder>,
}
impl FootnoteStore {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, normalized_label: String, label: String, events: Vec<BlockEvent>) {
if self.by_label.contains_key(&normalized_label) {
return;
}
let idx = self.defs.len();
self.defs.push(FootnoteDef { label, events });
self.by_label.insert(normalized_label, idx);
}
pub fn get_index(&self, label: &str) -> Option<usize> {
self.by_label.get(label).copied()
}
pub fn get(&self, idx: usize) -> Option<&FootnoteDef> {
self.defs.get(idx)
}
pub fn is_empty(&self) -> bool {
self.defs.is_empty()
}
}
pub fn normalize_footnote_label(bytes: &[u8]) -> Option<String> {
if bytes.is_empty() {
return None;
}
for &b in bytes {
if !b.is_ascii_alphanumeric() && b != b'-' && b != b'_' {
return None;
}
}
let mut out = String::with_capacity(bytes.len());
for &b in bytes {
out.push(b.to_ascii_lowercase() as char);
}
Some(out)
}