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