BlueGit

timfa posprint

Files

posprint / src / dsl / mod.rs

mod.rs

mod.rs 5.0 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<Vec<Inline>>,
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#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
77pub enum TimeFormatSpec {
78 #[default]
79 Hours24,
80 Hours12,
81}
82
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
84pub enum Align {
85 #[default]
86 Left,
87 Center,
88 Right,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
92pub enum BarcodeMode {
93 #[default]
94 Native,
95 Raster,
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
99pub enum QrMode {
100 #[default]
101 Native,
102 Raster,
103}
104
105#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum ImageWidth {
107 Percent(u8),
108 Dots(u16),
109 Default,
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113pub enum BarcodeSymbology {
114 Code128,
115 Ean13,
116 Ean8,
117 Code39,
118 Code93,
119 UpcA,
120 UpcE,
121 Itf,
122 Codabar,
123 Code11,
124}
125
126#[derive(Debug, Clone, Copy, PartialEq, Eq)]
127pub enum QrEcc {
128 L,
129 M,
130 Q,
131 H,
132}
133
134#[derive(Debug, Clone, PartialEq, Eq)]
135pub enum Inline {
136 Text(String),
137 /// Text parameter: `${Name}`.
138 Param(String),
139 /// Boolean checkbox parameter: `$[Name]` → `[ ]` or `[√]` at print time.
140 CheckboxParam(String),
141 /// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` / `[√]` (checked).
142 Checkbox {
143 checked: bool,
144 },
145}
146
147/// Checkbox glyphs for preview / wrap width. Print maps `√` (U+221A) to CP437 `0xFB`.
148pub fn checkbox_glyph(checked: bool) -> &'static str {
149 if checked {
150 "[\u{221A}]" // √ — CP437 byte 0xFB when encoded for the printer
151 } else {
152 "[ ]"
153 }
154}
155
156impl BarcodeSymbology {
157 pub const ALL: &'static [BarcodeSymbology] = &[
158 BarcodeSymbology::Code128,
159 BarcodeSymbology::Ean13,
160 BarcodeSymbology::Ean8,
161 BarcodeSymbology::Code39,
162 BarcodeSymbology::Code93,
163 BarcodeSymbology::UpcA,
164 BarcodeSymbology::UpcE,
165 BarcodeSymbology::Itf,
166 BarcodeSymbology::Codabar,
167 BarcodeSymbology::Code11,
168 ];
169
170 pub fn from_name(name: &str) -> Option<Self> {
171 match name.to_ascii_uppercase().as_str() {
172 "CODE128" => Some(Self::Code128),
173 "EAN13" => Some(Self::Ean13),
174 "EAN8" => Some(Self::Ean8),
175 "CODE39" => Some(Self::Code39),
176 "CODE93" => Some(Self::Code93),
177 "UPC-A" | "UPCA" => Some(Self::UpcA),
178 "UPC-E" | "UPCE" => Some(Self::UpcE),
179 "ITF" | "ITF14" | "I25" => Some(Self::Itf),
180 "CODABAR" => Some(Self::Codabar),
181 "CODE11" => Some(Self::Code11),
182 _ => None,
183 }
184 }
185
186 pub fn as_str(self) -> &'static str {
187 match self {
188 Self::Code128 => "CODE128",
189 Self::Ean13 => "EAN13",
190 Self::Ean8 => "EAN8",
191 Self::Code39 => "CODE39",
192 Self::Code93 => "CODE93",
193 Self::UpcA => "UPC-A",
194 Self::UpcE => "UPC-E",
195 Self::Itf => "ITF",
196 Self::Codabar => "CODABAR",
197 Self::Code11 => "CODE11",
198 }
199 }
200
201 pub fn supports_native(self) -> bool {
202 !matches!(self, Self::Code93 | Self::Code11)
203 }
204}
205
206impl QrEcc {
207 pub fn from_name(name: &str) -> Option<Self> {
208 match name.to_ascii_uppercase().as_str() {
209 "L" => Some(Self::L),
210 "M" => Some(Self::M),
211 "Q" => Some(Self::Q),
212 "H" => Some(Self::H),
213 _ => None,
214 }
215 }
216}
217
218mod parser;
219
220pub use parser::{document_ends_with_cut, parse};