BlueGit

timfa posprint

Files

posprint / src / dsl / mod.rs

mod.rs

fixes
timfa Tim Falken · pushed by timfa · 4 months ago
4ab2186f History
mod.rs 4.4 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 Heading {
34 level: u8,
35 align: Align,
36 inlines: Vec<Inline>,
37 },
38 Rule,
39 Feed(u8),
40 Cut,
41 Image {
42 path: String,
43 width: ImageWidth,
44 align: Align,
45 },
46 Barcode {
47 mode: BarcodeMode,
48 symbology: BarcodeSymbology,
49 data: String,
50 height: u8,
51 align: Align,
52 show_text: bool,
53 },
54 Qr {
55 mode: QrMode,
56 data: String,
57 size: u8,
58 ecc: QrEcc,
59 version: Option<u8>,
60 align: Align,
61 },
62 Date {
63 format: Option<String>,
64 align: Align,
65 },
66 Time {
67 format: TimeFormatSpec,
68 align: Align,
69 },
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
73pub enum TimeFormatSpec {
74 #[default]
75 Hours24,
76 Hours12,
77}
78
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
80pub enum Align {
81 #[default]
82 Left,
83 Center,
84 Right,
85}
86
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
88pub enum BarcodeMode {
89 #[default]
90 Native,
91 Raster,
92}
93
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
95pub enum QrMode {
96 #[default]
97 Native,
98 Raster,
99}
100
101#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub enum ImageWidth {
103 Percent(u8),
104 Dots(u16),
105 Default,
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq)]
109pub enum BarcodeSymbology {
110 Code128,
111 Ean13,
112 Ean8,
113 Code39,
114 Code93,
115 UpcA,
116 UpcE,
117 Itf,
118 Codabar,
119 Code11,
120}
121
122#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum QrEcc {
124 L,
125 M,
126 Q,
127 H,
128}
129
130#[derive(Debug, Clone, PartialEq, Eq)]
131pub enum Inline {
132 Text(String),
133 Bold(Vec<Inline>),
134 Italic(Vec<Inline>),
135 Code(String),
136 Param(String),
137}
138
139impl BarcodeSymbology {
140 pub const ALL: &'static [BarcodeSymbology] = &[
141 BarcodeSymbology::Code128,
142 BarcodeSymbology::Ean13,
143 BarcodeSymbology::Ean8,
144 BarcodeSymbology::Code39,
145 BarcodeSymbology::Code93,
146 BarcodeSymbology::UpcA,
147 BarcodeSymbology::UpcE,
148 BarcodeSymbology::Itf,
149 BarcodeSymbology::Codabar,
150 BarcodeSymbology::Code11,
151 ];
152
153 pub fn from_name(name: &str) -> Option<Self> {
154 match name.to_ascii_uppercase().as_str() {
155 "CODE128" => Some(Self::Code128),
156 "EAN13" => Some(Self::Ean13),
157 "EAN8" => Some(Self::Ean8),
158 "CODE39" => Some(Self::Code39),
159 "CODE93" => Some(Self::Code93),
160 "UPC-A" | "UPCA" => Some(Self::UpcA),
161 "UPC-E" | "UPCE" => Some(Self::UpcE),
162 "ITF" | "ITF14" | "I25" => Some(Self::Itf),
163 "CODABAR" => Some(Self::Codabar),
164 "CODE11" => Some(Self::Code11),
165 _ => None,
166 }
167 }
168
169 pub fn as_str(self) -> &'static str {
170 match self {
171 Self::Code128 => "CODE128",
172 Self::Ean13 => "EAN13",
173 Self::Ean8 => "EAN8",
174 Self::Code39 => "CODE39",
175 Self::Code93 => "CODE93",
176 Self::UpcA => "UPC-A",
177 Self::UpcE => "UPC-E",
178 Self::Itf => "ITF",
179 Self::Codabar => "CODABAR",
180 Self::Code11 => "CODE11",
181 }
182 }
183
184 pub fn supports_native(self) -> bool {
185 !matches!(self, Self::Code93 | Self::Code11)
186 }
187}
188
189impl QrEcc {
190 pub fn from_name(name: &str) -> Option<Self> {
191 match name.to_ascii_uppercase().as_str() {
192 "L" => Some(Self::L),
193 "M" => Some(Self::M),
194 "Q" => Some(Self::Q),
195 "H" => Some(Self::H),
196 _ => None,
197 }
198 }
199}
200
201mod parser;
202
203pub use parser::parse;