BlueGit

timfa posprint

Files

posprint / src / dsl / mod.rs

mod.rs

mod.rs 5.1 KiB Download Raw
1use std::fmt;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub struct ParseError {
5 pub line: usize,
6 pub column: usize,
7 pub message: String,
8}
9
10impl fmt::Display for ParseError {
11 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12 write!(
13 f,
14 "line {}, column {}: {}",
15 self.line, self.column, self.message
16 )
17 }
18}
19
20impl std::error::Error for ParseError {}
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct Document {
24 pub blocks: Vec<Block>,
25}
26
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum Block {
29 Paragraph {
30 align: Align,
31 inlines: Vec<Inline>,
32 },
33 List {
34 align: Align,
35 items: Vec<ListItem>,
36 },
37 Heading {
38 level: u8,
39 align: Align,
40 inlines: Vec<Inline>,
41 },
42 Rule,
43 Feed(u8),
44 Cut,
45 Image {
46 path: String,
47 width: ImageWidth,
48 align: Align,
49 },
50 Barcode {
51 mode: BarcodeMode,
52 symbology: BarcodeSymbology,
53 data: String,
54 height: u8,
55 align: Align,
56 show_text: bool,
57 },
58 Qr {
59 mode: QrMode,
60 data: String,
61 size: u8,
62 ecc: QrEcc,
63 version: Option<u8>,
64 align: Align,
65 },
66 Date {
67 format: Option<String>,
68 align: Align,
69 },
70 Time {
71 format: TimeFormatSpec,
72 align: Align,
73 },
74}
75
76/// One bullet in a [`Block::List`], with nesting depth (0 = top level).
77#[derive(Debug, Clone, PartialEq, Eq)]
78pub struct ListItem {
79 pub depth: u8,
80 pub inlines: Vec<Inline>,
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
84pub enum TimeFormatSpec {
85 #[default]
86 Hours24,
87 Hours12,
88}
89
90#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
91pub enum Align {
92 #[default]
93 Left,
94 Center,
95 Right,
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
99pub enum BarcodeMode {
100 #[default]
101 Native,
102 Raster,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
106pub enum QrMode {
107 #[default]
108 Native,
109 Raster,
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum ImageWidth {
114 Percent(u8),
115 Dots(u16),
116 Default,
117}
118
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum BarcodeSymbology {
121 Code128,
122 Ean13,
123 Ean8,
124 Code39,
125 Code93,
126 UpcA,
127 UpcE,
128 Itf,
129 Codabar,
130 Code11,
131}
132
133#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub enum QrEcc {
135 L,
136 M,
137 Q,
138 H,
139}
140
141#[derive(Debug, Clone, PartialEq, Eq)]
142pub enum Inline {
143 Text(String),
144 /// Text parameter: `${Name}`.
145 Param(String),
146 /// Boolean checkbox parameter: `$[Name]` → `[ ]` or `[x]` at print time.
147 CheckboxParam(String),
148 /// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` (checked).
149 Checkbox {
150 checked: bool,
151 },
152}
153
154/// ASCII checkbox glyphs — CP437-safe and inline with the native printer font.
155pub fn checkbox_glyph(checked: bool) -> &'static str {
156 if checked {
157 "[x]"
158 } else {
159 "[ ]"
160 }
161}
162
163impl BarcodeSymbology {
164 pub const ALL: &'static [BarcodeSymbology] = &[
165 BarcodeSymbology::Code128,
166 BarcodeSymbology::Ean13,
167 BarcodeSymbology::Ean8,
168 BarcodeSymbology::Code39,
169 BarcodeSymbology::Code93,
170 BarcodeSymbology::UpcA,
171 BarcodeSymbology::UpcE,
172 BarcodeSymbology::Itf,
173 BarcodeSymbology::Codabar,
174 BarcodeSymbology::Code11,
175 ];
176
177 pub fn from_name(name: &str) -> Option<Self> {
178 match name.to_ascii_uppercase().as_str() {
179 "CODE128" => Some(Self::Code128),
180 "EAN13" => Some(Self::Ean13),
181 "EAN8" => Some(Self::Ean8),
182 "CODE39" => Some(Self::Code39),
183 "CODE93" => Some(Self::Code93),
184 "UPC-A" | "UPCA" => Some(Self::UpcA),
185 "UPC-E" | "UPCE" => Some(Self::UpcE),
186 "ITF" | "ITF14" | "I25" => Some(Self::Itf),
187 "CODABAR" => Some(Self::Codabar),
188 "CODE11" => Some(Self::Code11),
189 _ => None,
190 }
191 }
192
193 pub fn as_str(self) -> &'static str {
194 match self {
195 Self::Code128 => "CODE128",
196 Self::Ean13 => "EAN13",
197 Self::Ean8 => "EAN8",
198 Self::Code39 => "CODE39",
199 Self::Code93 => "CODE93",
200 Self::UpcA => "UPC-A",
201 Self::UpcE => "UPC-E",
202 Self::Itf => "ITF",
203 Self::Codabar => "CODABAR",
204 Self::Code11 => "CODE11",
205 }
206 }
207
208 pub fn supports_native(self) -> bool {
209 !matches!(self, Self::Code93 | Self::Code11)
210 }
211}
212
213impl QrEcc {
214 pub fn from_name(name: &str) -> Option<Self> {
215 match name.to_ascii_uppercase().as_str() {
216 "L" => Some(Self::L),
217 "M" => Some(Self::M),
218 "Q" => Some(Self::Q),
219 "H" => Some(Self::H),
220 _ => None,
221 }
222 }
223}
224
225mod parser;
226
227pub use parser::{document_ends_with_cut, parse};