latexml_core/common/object.rs
1///======================================================================
2/// Exported generic functions for dealing with `LaTeXML`'s objects
3///======================================================================
4use std::fmt::Debug;
5
6use crate::{
7 Digested,
8 common::{error::*, locator::Locator},
9 tokens::Tokens,
10};
11
12/// Base object for all LaTeXML Objects.
13///
14/// Defines basic default methods for comparison, printing
15pub trait Object {
16 fn stringify(&self) -> String { String::from("<unknown>") }
17
18 fn isa_box(&self) -> bool { false }
19 fn is_expandable(&self) -> bool { false }
20 fn is_definition(&self) -> bool { false }
21 fn is_comment(&self) -> bool { false }
22
23 // These should really only make sense for Data objects within the
24 // processing stream.
25 fn be_digested(self) -> Result<Digested>
26 where
27 Self: Sized,
28 Self: Debug,
29 {
30 panic!("Was it really intended to digest? We don't know how! {self:?}");
31 }
32 /// The object's stored source locator, if it has one. `None` is the honest
33 /// "no recorded source position" (replacing the old `Locator::default()`
34 /// `file!()/line!()` sentinel). For "where the parser is *now*" (error
35 /// reporting, box creation), use the free fn `gullet::get_locator()`.
36 fn get_locator(&self) -> Option<Locator> { None }
37
38 /// each concrete object needs to provide its own path back to tokens
39 fn revert(&self) -> Result<Tokens> { Ok(Tokens::new(vec![])) }
40}