micron_parser/parsing/
model.rs1use std::mem::discriminant;
2
3#[derive(Debug, PartialEq)]
4pub struct Document {
5 pub(crate) sections: Vec<Section>,
6}
7
8impl Document {
9 pub fn sections(self) -> Vec<Section> {
10 self.sections
11 }
12}
13
14#[derive(Debug, PartialEq)]
15pub enum Block {
16 Line(Line),
17 EmptyLine(EmptyLine),
18 Ruler(Ruler),
19}
20
21#[derive(Debug, PartialEq)]
22pub struct Line {
23 pub nodes: Vec<InlineNode>,
24 pub alignment: Alignment,
25}
26
27impl Default for Line {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl Line {
34 fn new() -> Self {
35 Self {
36 nodes: Vec::new(),
37 alignment: Alignment::default(),
38 }
39 }
40
41 pub(crate) fn add_node(&mut self, node: InlineNode) {
42 self.nodes.push(node);
43 }
44}
45
46#[derive(Debug, PartialEq)]
47pub struct Section {
48 pub level: u8,
49 pub header: Option<Line>,
50 pub blocks: Vec<Block>,
51}
52
53impl Default for Section {
54 fn default() -> Self {
55 Self::new()
56 }
57}
58
59impl Section {
60 fn new() -> Self {
61 Self {
62 level: 0,
63 header: None,
64 blocks: Vec::new(),
65 }
66 }
67
68 pub(crate) fn add_to_header(&mut self, node: InlineNode) {
69 if let Some(header) = self.header.as_mut() {
70 header.add_node(node);
71 } else {
72 self.header = Some(Line::new());
73 self.add_to_header(node);
74 }
75 }
76
77 pub(crate) fn set_header_alignment(&mut self, alignment: Alignment) {
78 if let Some(header) = self.header.as_mut() {
79 header.alignment = alignment;
80 }
81 }
82}
83
84#[derive(Debug, PartialEq, Default)]
85pub struct EmptyLine {
86 pub style: StyleSet,
87}
88
89#[derive(Debug, PartialEq)]
90pub struct Ruler {
91 pub symbol: Option<String>,
92 pub style: StyleSet,
93}
94
95impl Default for Ruler {
96 fn default() -> Self {
97 Self::new()
98 }
99}
100
101impl Ruler {
102 fn new() -> Self {
103 Self {
104 symbol: None,
105 style: StyleSet::default(),
106 }
107 }
108}
109
110#[derive(Debug, PartialEq)]
111pub enum InlineNode {
112 Text { style: StyleSet, text: String },
113 Newline { style: StyleSet },
114 Link { style: StyleSet, link: Link },
115 Field { style: StyleSet, field: Field },
116}
117
118#[derive(Debug, Default, Clone, Copy, PartialEq)]
119pub enum Alignment {
120 #[default]
121 Default,
122 Left,
123 Center,
124 Right,
125}
126
127#[derive(Debug, Clone, Default, PartialEq, Eq)]
128pub struct StyleSet(pub Vec<Style>);
129
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub enum Style {
132 Bold,
133 Italic,
134 Underline,
135 ForegroundColor(String),
136 BackgroundColor(String),
137}
138
139impl StyleSet {
140 fn pos_of(&self, modifier: &Style) -> Option<usize> {
141 self.0
142 .iter()
143 .position(|m| discriminant(m) == discriminant(modifier))
144 }
145
146 pub(crate) fn set(&mut self, modifier: Style) {
147 self.unset(&modifier);
148 self.0.push(modifier);
149 }
150
151 pub(crate) fn unset(&mut self, modifier: &Style) {
152 while let Some(i) = self.pos_of(modifier) {
153 self.0.remove(i);
154 }
155 }
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub struct Link {
160 pub label: Option<String>,
161 pub url: String,
162 pub fields: Vec<String>,
163}
164
165#[derive(Debug, Clone, PartialEq)]
166pub struct Field {
167 pub kind: FieldKind,
168 pub name: String,
169 pub value: String,
170}
171
172#[derive(Debug, Clone, PartialEq)]
173pub enum FieldKind {
174 Checkbox { pre_checked: bool },
175 Radio { pre_checked: bool },
176 Input { hidden: bool, size: Option<u8> },
177}