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/// Represents a list of items.
65#[derive(Debug, Clone, PartialEq)]
66pub struct List {
67    pub items: Vec<ListItem>,
68    pub ordered: bool,
69    pub style: ListStyle,
70}
71
72/// Represents an item in a list.
73#[derive(Debug, Clone, PartialEq)]
74pub struct ListItem {
75    pub content: Vec<InlineContent>,
76    pub children: Vec<DocNode>,
77}
78
79/// Represents a definition of a term.
80#[derive(Debug, Clone, PartialEq)]
81pub struct Definition {
82    pub term: Vec<InlineContent>,
83    pub description: Vec<DocNode>,
84}
85
86/// Represents a block of verbatim text.
87#[derive(Debug, Clone, PartialEq)]
88pub struct Verbatim {
89    pub language: Option<String>,
90    pub content: String,
91}
92
93/// Represents an annotation.
94#[derive(Debug, Clone, PartialEq)]
95pub struct Annotation {
96    pub label: String,
97    pub parameters: Vec<(String, String)>,
98    pub content: Vec<DocNode>,
99}
100
101/// Represents a table.
102#[derive(Debug, Clone, PartialEq)]
103pub struct Table {
104    pub rows: Vec<TableRow>,
105    pub header: Vec<TableRow>,
106    pub caption: Option<Vec<InlineContent>>,
107}
108
109/// Represents a table row.
110#[derive(Debug, Clone, PartialEq)]
111pub struct TableRow {
112    pub cells: Vec<TableCell>,
113}
114
115/// Represents a table cell.
116#[derive(Debug, Clone, PartialEq)]
117pub struct TableCell {
118    pub content: Vec<DocNode>,
119    pub header: bool,
120    pub align: TableCellAlignment,
121}
122
123/// Alignment of a table cell.
124#[derive(Debug, Clone, Copy, PartialEq)]
125pub enum TableCellAlignment {
126    Left,
127    Center,
128    Right,
129    None,
130}
131
132/// Represents inline content, such as text, bold, italics, etc.
133#[derive(Debug, Clone, PartialEq)]
134pub enum InlineContent {
135    Text(String),
136    Bold(Vec<InlineContent>),
137    Italic(Vec<InlineContent>),
138    Code(String),
139    Math(String),
140    Reference(String),
141    Marker(String),
142    Image(Image),
143}
144
145/// Represents an image.
146#[derive(Debug, Clone, PartialEq)]
147pub struct Image {
148    pub src: String,
149    pub alt: String,
150    pub title: Option<String>,
151}
152
153/// Represents a video.
154#[derive(Debug, Clone, PartialEq)]
155pub struct Video {
156    pub src: String,
157    pub title: Option<String>,
158    pub poster: Option<String>,
159}
160
161/// Represents an audio file.
162#[derive(Debug, Clone, PartialEq)]
163pub struct Audio {
164    pub src: String,
165    pub title: Option<String>,
166}