use std::fmt; #[derive(Debug, Clone, PartialEq, Eq)] pub struct ParseError { pub line: usize, pub column: usize, pub message: String, } impl fmt::Display for ParseError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, "line {}, column {}: {}", self.line, self.column, self.message ) } } impl std::error::Error for ParseError {} #[derive(Debug, Clone, PartialEq, Eq)] pub struct Document { pub blocks: Vec, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum Block { Paragraph { align: Align, inlines: Vec, }, /// Multiple `:left:` / `:center:` / `:right:` text segments on one source line. /// Printed as one space-padded row (ESC/POS cannot dual-justify on one line). AlignedRow { parts: Vec<(Align, Vec)>, }, List { align: Align, items: Vec, }, Heading { level: u8, align: Align, inlines: Vec, }, Rule, Feed(u8), Cut, Image { path: String, width: ImageWidth, align: Align, }, Barcode { mode: BarcodeMode, symbology: BarcodeSymbology, data: String, height: u8, align: Align, show_text: bool, }, Qr { mode: QrMode, data: String, size: u8, ecc: QrEcc, version: Option, align: Align, }, Date { format: Option, align: Align, }, Time { format: TimeFormatSpec, align: Align, }, } /// One bullet in a [`Block::List`], with nesting depth (0 = top level). #[derive(Debug, Clone, PartialEq, Eq)] pub struct ListItem { pub depth: u8, pub inlines: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum TimeFormatSpec { #[default] Hours24, Hours12, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum Align { #[default] Left, Center, Right, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum BarcodeMode { #[default] Native, Raster, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum QrMode { #[default] Native, Raster, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ImageWidth { Percent(u8), Dots(u16), Default, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum BarcodeSymbology { Code128, Ean13, Ean8, Code39, Code93, UpcA, UpcE, Itf, Codabar, Code11, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum QrEcc { L, M, Q, H, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum Inline { Text(String), /// Text parameter: `${Name}`. Param(String), /// Boolean checkbox parameter: `$[Name]` → `[ ]` or `[x]` at print time. CheckboxParam(String), /// Inline checkbox: `[]` / `[ ]` (empty) or `[x]` / `[X]` (checked). Checkbox { checked: bool, }, } /// ASCII checkbox glyphs — CP437-safe and inline with the native printer font. pub fn checkbox_glyph(checked: bool) -> &'static str { if checked { "[x]" } else { "[ ]" } } impl BarcodeSymbology { pub const ALL: &'static [BarcodeSymbology] = &[ BarcodeSymbology::Code128, BarcodeSymbology::Ean13, BarcodeSymbology::Ean8, BarcodeSymbology::Code39, BarcodeSymbology::Code93, BarcodeSymbology::UpcA, BarcodeSymbology::UpcE, BarcodeSymbology::Itf, BarcodeSymbology::Codabar, BarcodeSymbology::Code11, ]; pub fn from_name(name: &str) -> Option { match name.to_ascii_uppercase().as_str() { "CODE128" => Some(Self::Code128), "EAN13" => Some(Self::Ean13), "EAN8" => Some(Self::Ean8), "CODE39" => Some(Self::Code39), "CODE93" => Some(Self::Code93), "UPC-A" | "UPCA" => Some(Self::UpcA), "UPC-E" | "UPCE" => Some(Self::UpcE), "ITF" | "ITF14" | "I25" => Some(Self::Itf), "CODABAR" => Some(Self::Codabar), "CODE11" => Some(Self::Code11), _ => None, } } pub fn as_str(self) -> &'static str { match self { Self::Code128 => "CODE128", Self::Ean13 => "EAN13", Self::Ean8 => "EAN8", Self::Code39 => "CODE39", Self::Code93 => "CODE93", Self::UpcA => "UPC-A", Self::UpcE => "UPC-E", Self::Itf => "ITF", Self::Codabar => "CODABAR", Self::Code11 => "CODE11", } } pub fn supports_native(self) -> bool { !matches!(self, Self::Code93 | Self::Code11) } } impl QrEcc { pub fn from_name(name: &str) -> Option { match name.to_ascii_uppercase().as_str() { "L" => Some(Self::L), "M" => Some(Self::M), "Q" => Some(Self::Q), "H" => Some(Self::H), _ => None, } } } mod parser; pub use parser::{document_ends_with_cut, parse};