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