Skip to main content

lex_babel/ir/
nodes.rs

1//! Core data structures for the Intermediate Representation (IR).
2
3/// A universal, semantic representation of a document node.
4#[derive(Debug, Clone, PartialEq)]
5pub enum DocNode {
6    Document(Document),
7    Heading(Heading),
8    Paragraph(Paragraph),
9    List(List),
10    ListItem(ListItem),
11    Definition(Definition),
12    Verbatim(Verbatim),
13    Annotation(Annotation),
14    Inline(InlineContent),
15    Table(Table),
16    Image(Image),
17    Video(Video),
18    Audio(Audio),
19}
20
21/// Represents the root of a document.
22#[derive(Debug, Clone, PartialEq)]
23pub struct Document {
24    pub children: Vec<DocNode>,
25}
26
27/// Represents a heading with a specific level.
28#[derive(Debug, Clone, PartialEq)]
29pub struct Heading {
30    pub level: usize,
31    pub content: Vec<InlineContent>,
32    pub children: Vec<DocNode>,
33}
34
35/// Represents a paragraph of text.
36#[derive(Debug, Clone, PartialEq)]
37pub struct Paragraph {
38    pub content: Vec<InlineContent>,
39}
40
41/// Decoration style for ordered lists.
42#[derive(Debug, Clone, Copy, PartialEq)]
43pub enum ListStyle {
44    /// Unordered: `-`, `*`, `+`
45    Bullet,
46    /// Numeric: `1.`, `2.`, `3.`
47    Numeric,
48    /// Lowercase alphabetic: `a.`, `b.`, `c.`
49    AlphaLower,
50    /// Uppercase alphabetic: `A.`, `B.`, `C.`
51    AlphaUpper,
52    /// Lowercase roman: `i.`, `ii.`, `iii.`
53    RomanLower,
54    /// Uppercase roman: `I.`, `II.`, `III.`
55    RomanUpper,
56}
57
58impl ListStyle {
59    pub fn is_ordered(self) -> bool {
60        !matches!(self, ListStyle::Bullet)
61    }
62}
63
64/// Whether list markers use short or extended (hierarchical) form.
65#[derive(Debug, Clone, Copy, PartialEq)]
66pub enum ListForm {
67    /// Short form: single level marker (e.g., `1.`, `a)`)
68    Short,
69    /// Extended form: multi-level nested index (e.g., `1.2.3`, `I.a.2`)
70    Extended,
71}
72
73/// Represents a list of items.
74#[derive(Debug, Clone, PartialEq)]
75pub struct List {
76    pub items: Vec<ListItem>,
77    pub ordered: bool,
78    pub style: ListStyle,
79    pub form: ListForm,
80}
81
82/// Represents an item in a list.
83#[derive(Debug, Clone, PartialEq)]
84pub struct ListItem {
85    pub content: Vec<InlineContent>,
86    pub children: Vec<DocNode>,
87}
88
89/// Represents a definition of a term.
90#[derive(Debug, Clone, PartialEq)]
91pub struct Definition {
92    pub term: Vec<InlineContent>,
93    pub description: Vec<DocNode>,
94}
95
96/// Represents a block of verbatim text.
97#[derive(Debug, Clone, PartialEq)]
98pub struct Verbatim {
99    pub subject: Option<String>,
100    pub language: Option<String>,
101    pub content: String,
102}
103
104/// Represents an annotation.
105#[derive(Debug, Clone, PartialEq)]
106pub struct Annotation {
107    pub label: String,
108    pub parameters: Vec<(String, String)>,
109    pub content: Vec<DocNode>,
110}
111
112/// Represents a table.
113#[derive(Debug, Clone, PartialEq)]
114pub struct Table {
115    pub rows: Vec<TableRow>,
116    pub header: Vec<TableRow>,
117    pub caption: Option<Vec<InlineContent>>,
118}
119
120/// Represents a table row.
121#[derive(Debug, Clone, PartialEq)]
122pub struct TableRow {
123    pub cells: Vec<TableCell>,
124}
125
126/// Represents a table cell.
127#[derive(Debug, Clone, PartialEq)]
128pub struct TableCell {
129    pub content: Vec<DocNode>,
130    pub header: bool,
131    pub align: TableCellAlignment,
132}
133
134/// Alignment of a table cell.
135#[derive(Debug, Clone, Copy, PartialEq)]
136pub enum TableCellAlignment {
137    Left,
138    Center,
139    Right,
140    None,
141}
142
143/// Represents inline content, such as text, bold, italics, etc.
144#[derive(Debug, Clone, PartialEq)]
145pub enum InlineContent {
146    Text(String),
147    Bold(Vec<InlineContent>),
148    Italic(Vec<InlineContent>),
149    Code(String),
150    Math(String),
151    Reference(String),
152    Marker(String),
153    Image(Image),
154}
155
156/// Represents an image.
157#[derive(Debug, Clone, PartialEq)]
158pub struct Image {
159    pub src: String,
160    pub alt: String,
161    pub title: Option<String>,
162}
163
164/// Represents a video.
165#[derive(Debug, Clone, PartialEq)]
166pub struct Video {
167    pub src: String,
168    pub title: Option<String>,
169    pub poster: Option<String>,
170}
171
172/// Represents an audio file.
173#[derive(Debug, Clone, PartialEq)]
174pub struct Audio {
175    pub src: String,
176    pub title: Option<String>,
177}