Skip to main content

asciidoc_parser/document/
catalog.rs

1use std::collections::HashMap;
2
3use crate::{content::FootnoteDeferred, internal::debug::DebugHashMapFrom};
4
5/// Document catalog for tracking referenceable elements.
6///
7/// The catalog maintains a registry of all elements that can be referenced
8/// via cross-references, including anchors, sections, and bibliography entries.
9/// It provides functionality for registering new references, resolving
10/// reference text to IDs, and detecting duplicate IDs.
11#[derive(Clone, Eq, PartialEq)]
12pub struct Catalog {
13    /// Primary registry mapping IDs to reference entries.
14    pub(crate) refs: HashMap<String, RefEntry>,
15
16    /// Reverse lookup cache: reftext -> ID.
17    pub(crate) reftext_to_id: HashMap<String, String>,
18
19    /// Footnotes registered (in document order) while substituting inline
20    /// macros. Each entry corresponds to a `footnote:[…]` macro that *defined*
21    /// a footnote; subsequent references to an existing footnote (via a
22    /// repeated ID) reuse an entry rather than adding a new one.
23    ///
24    /// A nested document (an AsciiDoc table cell) keeps its own footnote list:
25    /// footnotes defined inside a cell are *not* shared with the main document.
26    pub(crate) footnotes: Vec<Footnote>,
27}
28
29impl Default for Catalog {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl Catalog {
36    pub(crate) fn new() -> Self {
37        Self {
38            refs: HashMap::new(),
39            reftext_to_id: HashMap::new(),
40            footnotes: Vec::new(),
41        }
42    }
43
44    /// Register a new referenceable element in the catalog.
45    ///
46    /// # Arguments
47    /// * `id` - The unique identifier for the element
48    /// * `reftext` - Optional reference text for the element
49    /// * `ref_type` - Type of referenceable element
50    ///
51    /// # Returns
52    /// * `Ok(())` if the element was successfully registered
53    /// * `Err(DuplicateIdError)` if the ID is already in use
54    pub(crate) fn register_ref(
55        &mut self,
56        id: &str,
57        reftext: Option<&str>,
58        ref_type: RefType,
59    ) -> Result<(), DuplicateIdError> {
60        if self.refs.contains_key(id) {
61            return Err(DuplicateIdError(id.to_string()));
62        }
63
64        let entry = RefEntry {
65            id: id.to_string(),
66            reftext: reftext.map(|s| s.to_owned()),
67            ref_type,
68        };
69
70        self.refs.insert(id.to_string(), entry);
71
72        if let Some(reftext) = reftext {
73            self.reftext_to_id
74                .entry(reftext.to_string())
75                .or_insert_with(|| id.to_string());
76        }
77
78        Ok(())
79    }
80
81    /// Generate a unique ID based on a base ID and register it in the catalog.
82    ///
83    /// If the base ID is not in use, it is returned as-is. Otherwise, numeric
84    /// suffixes are appended until a unique ID is found. The generated ID is
85    /// then registered in the catalog with the provided parameters.
86    ///
87    /// # Arguments
88    /// * `base_id` - The base identifier to use
89    /// * `reftext` - Optional reference text for the element
90    /// * `ref_type` - Type of referenceable element
91    ///
92    /// # Returns
93    /// The unique ID that was generated and registered.
94    pub(crate) fn generate_and_register_unique_id(
95        &mut self,
96        base_id: &str,
97        reftext: Option<&str>,
98        ref_type: RefType,
99    ) -> String {
100        let unique_id = if !self.contains_id(base_id) {
101            base_id.to_string()
102        } else {
103            let mut counter = 2;
104            loop {
105                let candidate = format!("{}-{}", base_id, counter);
106                if !self.contains_id(&candidate) {
107                    break candidate;
108                }
109                counter += 1;
110            }
111        };
112
113        // Register the generated unique ID.
114        let entry = RefEntry {
115            id: unique_id.clone(),
116            reftext: reftext.map(|s| s.to_owned()),
117            ref_type,
118        };
119
120        self.refs.insert(unique_id.clone(), entry);
121
122        if let Some(reftext) = reftext {
123            self.reftext_to_id
124                .entry(reftext.to_string())
125                .or_insert_with(|| unique_id.clone());
126        }
127
128        unique_id
129    }
130
131    /// Returns a reference entry by ID, if it exists.
132    pub fn get_ref(&self, id: &str) -> Option<&RefEntry> {
133        self.refs.get(id)
134    }
135
136    /// Returns `true` if an ID is already registered in the catalog.
137    pub fn contains_id(&self, id: &str) -> bool {
138        self.refs.contains_key(id)
139    }
140
141    /// Resolve reference text to an ID, if possible.
142    pub fn resolve_id(&self, reftext: &str) -> Option<String> {
143        self.reftext_to_id.get(reftext).cloned()
144    }
145
146    /* Disabling for now until I know if we'll need these.
147
148    /// Returns an iterator over all registered reference IDs.
149    pub fn ids(&self) -> impl Iterator<Item = &String> {
150        self.refs.keys()
151    }
152
153    /// Returns an iterator over all reference entries.
154    pub fn entries(&self) -> impl Iterator<Item = (&String, &RefEntry)> {
155        self.refs.iter()
156    }
157    */
158
159    /// Returns the number of registered references.
160    pub fn len(&self) -> usize {
161        self.refs.len()
162    }
163
164    /// Returns `true` if the catalog contains no registered references.
165    pub fn is_empty(&self) -> bool {
166        self.refs.is_empty()
167    }
168
169    /// Returns the footnotes registered in this document, in document order.
170    pub fn footnotes(&self) -> &[Footnote] {
171        &self.footnotes
172    }
173
174    /// Registers a newly-defined [`Footnote`].
175    pub(crate) fn register_footnote(&mut self, footnote: Footnote) {
176        self.footnotes.push(footnote);
177    }
178
179    /// Returns the registered footnote with the given ID, if one exists.
180    pub(crate) fn footnote_with_id(&self, id: &str) -> Option<&Footnote> {
181        self.footnotes.iter().find(|f| f.id.as_deref() == Some(id))
182    }
183
184    /// Removes and returns the current footnote list, leaving an empty list
185    /// behind. Used to give a nested document (an AsciiDoc table cell) its own
186    /// footnote registry so its footnotes are not shared with the enclosing
187    /// document.
188    pub(crate) fn take_footnotes(&mut self) -> Vec<Footnote> {
189        std::mem::take(&mut self.footnotes)
190    }
191
192    /// Restores a previously-[taken](Self::take_footnotes) footnote list,
193    /// discarding any footnotes registered in the meantime.
194    pub(crate) fn restore_footnotes(&mut self, footnotes: Vec<Footnote>) {
195        self.footnotes = footnotes;
196    }
197}
198
199/// A footnote registered while substituting the inline `footnote:[…]` macro.
200///
201/// A footnote is defined at the location of its reference, but its text is
202/// extracted to an item in the document's footnote list. The same footnote can
203/// be referenced from multiple locations by assigning it an ID at the first
204/// occurrence and repeating that ID (with empty text) afterward; only the
205/// defining occurrence produces a `Footnote` entry.
206#[derive(Clone, Eq, PartialEq)]
207pub struct Footnote {
208    /// The footnote's number, assigned in document order via the
209    /// `footnote-number` counter. Normally a consecutive integer (`1`, `2`, …),
210    /// but stored as a string because the counter honors any seed the document
211    /// sets (e.g. `:footnote-number: z` yields `aa`, `ab`, … as Asciidoctor
212    /// does).
213    pub index: String,
214
215    /// The optional ID assigned to this footnote (the target of the macro, e.g.
216    /// `disclaimer` in `footnote:disclaimer[…]`). `None` for an anonymous
217    /// footnote.
218    pub id: Option<String>,
219
220    /// The already-substituted text of the footnote. When the footnote contains
221    /// cross-references, this reflects the unresolved fallback rendering until
222    /// the document's references are resolved, after which it reflects the
223    /// resolved links; it is always clean, user-facing text.
224    pub text: String,
225
226    /// Deferred cross-references discovered in the footnote text, awaiting
227    /// resolution. `None` for the common case of a footnote with no
228    /// cross-references.
229    pub(crate) deferred: Option<Box<FootnoteDeferred>>,
230}
231
232impl Footnote {
233    /// Resolves any cross-references embedded in this footnote's text using
234    /// `resolver`, then rebuilds [`text`](Self::text) from the resolved state.
235    /// Any unresolved target is reported in `warnings`.
236    ///
237    /// A footnote with no cross-references is left untouched.
238    pub(crate) fn resolve_references(
239        &mut self,
240        resolver: &dyn crate::parser::ReferenceResolver,
241        renderer: &dyn crate::parser::InlineSubstitutionRenderer,
242        warnings: &mut Vec<crate::parser::ReferenceWarning>,
243    ) {
244        if let Some(deferred) = self.deferred.as_mut() {
245            deferred.resolve(resolver, warnings);
246            self.text = deferred.render(renderer);
247        }
248    }
249}
250
251impl std::fmt::Debug for Footnote {
252    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
253        // The deferred cross-reference state is an internal implementation
254        // detail, omitted unless present so that the (very common)
255        // cross-reference-free footnote debugs as a plain field set.
256        let mut s = f.debug_struct("Footnote");
257        s.field("index", &self.index);
258        s.field("id", &self.id);
259        s.field("text", &self.text);
260
261        if let Some(deferred) = self.deferred.as_ref() {
262            s.field("deferred", deferred);
263        }
264
265        s.finish()
266    }
267}
268
269impl std::fmt::Debug for Catalog {
270    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
271        f.debug_struct("Catalog")
272            .field("refs", &DebugHashMapFrom(&self.refs))
273            .field("reftext_to_id", &DebugHashMapFrom(&self.reftext_to_id))
274            .field("footnotes", &self.footnotes)
275            .finish()
276    }
277}
278/// Type of referenceable element in the document.
279#[derive(Clone, PartialEq, Eq)]
280pub enum RefType {
281    /// Standard anchor element (`[[id]]` or `[[id,reftext]]`).
282    Anchor,
283
284    /// Section heading that can be referenced.
285    Section,
286
287    /// Bibliography reference (`[[[id]]]` or `[[[id,reftext]]]`).
288    Bibliography,
289}
290
291impl std::fmt::Debug for RefType {
292    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
293        match self {
294            Self::Anchor => f.write_str("RefType::Anchor"),
295            Self::Section => f.write_str("RefType::Section"),
296            Self::Bibliography => f.write_str("RefType::Bibliography"),
297        }
298    }
299}
300
301/// Entry in the document catalog representing a referenceable element.
302#[derive(Clone, Debug, Eq, PartialEq)]
303pub struct RefEntry {
304    /// The unique identifier for this element.
305    pub id: String,
306
307    /// Reference text for this element (explicit or computed).
308    pub reftext: Option<String>,
309
310    /// Type of referenceable element.
311    pub ref_type: RefType,
312}
313
314/// Error that occurs when attempting to register a duplicate ID.
315#[derive(Clone, Debug, Eq, PartialEq)]
316pub(crate) struct DuplicateIdError(pub(crate) String);
317
318impl std::fmt::Display for DuplicateIdError {
319    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
320        write!(f, "ID '{}' already registered", self.0)
321    }
322}
323
324impl std::error::Error for DuplicateIdError {}
325
326#[cfg(test)]
327mod tests {
328    #![allow(clippy::unwrap_used)]
329
330    use super::*;
331
332    #[test]
333    fn new_catalog_is_empty() {
334        let catalog = Catalog::new();
335        assert!(catalog.is_empty());
336        assert_eq!(catalog.len(), 0);
337    }
338
339    #[test]
340    fn register_ref_success() {
341        let mut catalog = Catalog::new();
342
343        let result = catalog.register_ref("test-id", Some("Test Reference"), RefType::Anchor);
344
345        assert!(result.is_ok());
346        assert_eq!(catalog.len(), 1);
347        assert!(catalog.contains_id("test-id"));
348    }
349
350    #[test]
351    fn register_duplicate_id_fails() {
352        let mut catalog = Catalog::new();
353
354        // Register first reference.
355        catalog
356            .register_ref("test-id", Some("First"), RefType::Anchor)
357            .unwrap();
358
359        // Attempt to register duplicate.
360        let result = catalog.register_ref("test-id", Some("Second"), RefType::Section);
361
362        let error = result.unwrap_err();
363        assert_eq!(error.0, "test-id");
364    }
365
366    #[test]
367    fn generate_and_register_unique_id() {
368        let mut catalog = Catalog::new();
369
370        // Test with available ID.
371        let id1 = catalog.generate_and_register_unique_id(
372            "available",
373            Some("Available Ref"),
374            RefType::Anchor,
375        );
376        assert_eq!(id1, "available");
377        assert!(catalog.contains_id("available"));
378        assert_eq!(
379            catalog.resolve_id("Available Ref"),
380            Some("available".to_string())
381        );
382
383        // Test with taken IDs.
384        catalog
385            .register_ref("taken", None, RefType::Anchor)
386            .unwrap();
387        catalog
388            .register_ref("taken-2", None, RefType::Anchor)
389            .unwrap();
390
391        let id2 = catalog.generate_and_register_unique_id("taken", None, RefType::Section);
392        assert_eq!(id2, "taken-3");
393        assert!(catalog.contains_id("taken-3"));
394    }
395
396    #[test]
397    fn get_ref() {
398        let mut catalog = Catalog::new();
399
400        catalog
401            .register_ref("test-id", Some("Test Reference"), RefType::Bibliography)
402            .unwrap();
403
404        let entry = catalog.get_ref("test-id").unwrap();
405        assert_eq!(entry.id, "test-id");
406        assert_eq!(entry.reftext, Some("Test Reference".to_string()));
407        assert_eq!(entry.ref_type, RefType::Bibliography);
408
409        assert!(catalog.get_ref("nonexistent").is_none());
410    }
411
412    #[test]
413    fn resolve_id() {
414        let mut catalog = Catalog::new();
415
416        catalog
417            .register_ref("anchor1", Some("Reference Text"), RefType::Anchor)
418            .unwrap();
419
420        catalog
421            .register_ref("anchor2", Some("Another Reference"), RefType::Section)
422            .unwrap();
423
424        assert_eq!(
425            catalog.resolve_id("Reference Text"),
426            Some("anchor1".to_string())
427        );
428        assert_eq!(
429            catalog.resolve_id("Another Reference"),
430            Some("anchor2".to_string())
431        );
432        assert_eq!(catalog.resolve_id("Nonexistent"), None);
433    }
434
435    #[test]
436    fn resolve_id_first_wins_on_duplicates() {
437        let mut catalog = Catalog::new();
438
439        // Register two different IDs with same reftext.
440        catalog
441            .register_ref("first", Some("Same Text"), RefType::Anchor)
442            .unwrap();
443
444        catalog
445            .register_ref("second", Some("Same Text"), RefType::Section)
446            .unwrap();
447
448        assert_eq!(catalog.resolve_id("Same Text"), Some("first".to_string()));
449    }
450
451    #[test]
452    fn duplicate_id_error_impl_display() {
453        let did_error = DuplicateIdError("foo".to_string());
454        assert_eq!(did_error.to_string(), "ID 'foo' already registered");
455    }
456
457    #[test]
458    fn ref_type_impl_debug() {
459        assert_eq!(format!("{:#?}", RefType::Anchor), "RefType::Anchor");
460        assert_eq!(format!("{:#?}", RefType::Section), "RefType::Section");
461
462        assert_eq!(
463            format!("{:#?}", RefType::Bibliography),
464            "RefType::Bibliography"
465        );
466    }
467}