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