| 1 | use serde::{Deserialize, Serialize};
|
| 2 |
|
| 3 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
| 4 | pub struct PosFile {
|
| 5 | pub version: u32,
|
| 6 | pub metadata: PosMetadata,
|
| 7 | pub body: String,
|
| 8 | }
|
| 9 |
|
| 10 | #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
| 11 | pub struct PosMetadata {
|
| 12 | #[serde(skip_serializing_if = "Option::is_none")]
|
| 13 | pub title: Option<String>,
|
| 14 | pub page_width_mm: f32,
|
| 15 | #[serde(skip_serializing_if = "Option::is_none")]
|
| 16 | pub chars_per_line: Option<u32>,
|
| 17 | #[serde(skip_serializing_if = "Option::is_none")]
|
| 18 | pub codepage: Option<String>,
|
| 19 | }
|
| 20 |
|
| 21 | impl Default for PosMetadata {
|
| 22 | fn default() -> Self {
|
| 23 | Self {
|
| 24 | title: None,
|
| 25 | page_width_mm: 80.0,
|
| 26 | chars_per_line: None,
|
| 27 | codepage: Some("CP437".into()),
|
| 28 | }
|
| 29 | }
|
| 30 | }
|
| 31 |
|
| 32 | impl PosFile {
|
| 33 | pub fn new(body: impl Into<String>) -> Self {
|
| 34 | Self {
|
| 35 | version: 1,
|
| 36 | metadata: PosMetadata::default(),
|
| 37 | body: body.into(),
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | pub fn to_json_pretty(&self) -> Result<String, serde_json::Error> {
|
| 42 | serde_json::to_string_pretty(self)
|
| 43 | }
|
| 44 |
|
| 45 | pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
|
| 46 | serde_json::from_str(json)
|
| 47 | }
|
| 48 |
|
| 49 | pub fn save_to_path(&self, path: &std::path::Path) -> Result<(), SaveError> {
|
| 50 | let json = self.to_json_pretty()?;
|
| 51 | std::fs::write(path, json)?;
|
| 52 | Ok(())
|
| 53 | }
|
| 54 |
|
| 55 | pub fn load_from_path(path: &std::path::Path) -> Result<Self, LoadError> {
|
| 56 | let json = std::fs::read_to_string(path)?;
|
| 57 | let json = json.strip_prefix('\u{feff}').unwrap_or(&json);
|
| 58 | let file = PosFile::from_json(json).map_err(LoadError::Json)?;
|
| 59 | if file.version != 1 {
|
| 60 | return Err(LoadError::UnsupportedVersion(file.version));
|
| 61 | }
|
| 62 | Ok(file)
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | impl PosMetadata {
|
| 67 | pub fn printable_dots(&self) -> u16 {
|
| 68 | // Most ESC/POS printers effectively expose Font A widths of:
|
| 69 | // 32 columns -> 384 dots, 48 columns -> 576 dots.
|
| 70 | // Keep image/raster width aligned with the text column model.
|
| 71 | (self.chars_per_line().max(1) * 12) as u16
|
| 72 | }
|
| 73 |
|
| 74 | pub fn chars_per_line(&self) -> u32 {
|
| 75 | self.chars_per_line.unwrap_or_else(|| {
|
| 76 | if self.page_width_mm <= 58.0 {
|
| 77 | 32
|
| 78 | } else {
|
| 79 | 48
|
| 80 | }
|
| 81 | })
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | #[derive(Debug)]
|
| 86 | pub enum SaveError {
|
| 87 | Json(serde_json::Error),
|
| 88 | Io(std::io::Error),
|
| 89 | }
|
| 90 |
|
| 91 | impl From<serde_json::Error> for SaveError {
|
| 92 | fn from(value: serde_json::Error) -> Self {
|
| 93 | Self::Json(value)
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | impl From<std::io::Error> for SaveError {
|
| 98 | fn from(value: std::io::Error) -> Self {
|
| 99 | Self::Io(value)
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | #[derive(Debug)]
|
| 104 | pub enum LoadError {
|
| 105 | Io(std::io::Error),
|
| 106 | Json(serde_json::Error),
|
| 107 | UnsupportedVersion(u32),
|
| 108 | }
|
| 109 |
|
| 110 | impl From<std::io::Error> for LoadError {
|
| 111 | fn from(value: std::io::Error) -> Self {
|
| 112 | Self::Io(value)
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | #[cfg(test)]
|
| 117 | mod tests {
|
| 118 | use super::*;
|
| 119 |
|
| 120 | #[test]
|
| 121 | fn roundtrip_json() {
|
| 122 | let file = PosFile::new("# Test");
|
| 123 | let json = file.to_json_pretty().unwrap();
|
| 124 | assert!(!json.contains("default_feed_before_cut"));
|
| 125 | let loaded = PosFile::from_json(&json).unwrap();
|
| 126 | assert_eq!(loaded, file);
|
| 127 | }
|
| 128 |
|
| 129 | #[test]
|
| 130 | fn chars_per_line_defaults() {
|
| 131 | let narrow = PosMetadata {
|
| 132 | page_width_mm: 58.0,
|
| 133 | ..Default::default()
|
| 134 | };
|
| 135 | assert_eq!(narrow.chars_per_line(), 32);
|
| 136 | assert_eq!(narrow.printable_dots(), 384);
|
| 137 | let very_narrow = PosMetadata {
|
| 138 | page_width_mm: 53.0,
|
| 139 | ..Default::default()
|
| 140 | };
|
| 141 | assert_eq!(very_narrow.chars_per_line(), 32);
|
| 142 | assert_eq!(very_narrow.printable_dots(), 384);
|
| 143 | let wide = PosMetadata::default();
|
| 144 | assert_eq!(wide.chars_per_line(), 48);
|
| 145 | assert_eq!(wide.printable_dots(), 576);
|
| 146 | }
|
| 147 |
|
| 148 | #[test]
|
| 149 | fn ignores_legacy_preview_mode_field() {
|
| 150 | let json = r#"{
|
| 151 | "version": 1,
|
| 152 | "metadata": {
|
| 153 | "page_width_mm": 80.0
|
| 154 | },
|
| 155 | "body": "hello"
|
| 156 | }"#;
|
| 157 | let loaded = PosFile::from_json(json).unwrap();
|
| 158 | assert_eq!(loaded.body, "hello");
|
| 159 | assert!(!loaded.to_json_pretty().unwrap().contains("preview_mode"));
|
| 160 | assert!(
|
| 161 | !loaded
|
| 162 | .to_json_pretty()
|
| 163 | .unwrap()
|
| 164 | .contains("default_feed_before_cut")
|
| 165 | );
|
| 166 | assert!(
|
| 167 | !loaded
|
| 168 | .to_json_pretty()
|
| 169 | .unwrap()
|
| 170 | .contains("cut_after_print")
|
| 171 | );
|
| 172 | }
|
| 173 |
|
| 174 | #[test]
|
| 175 | fn ignores_legacy_cut_after_print_field() {
|
| 176 | let json = r#"{
|
| 177 | "version": 1,
|
| 178 | "metadata": {
|
| 179 | "page_width_mm": 80.0,
|
| 180 | "cut_after_print": true
|
| 181 | },
|
| 182 | "body": "hello"
|
| 183 | }"#;
|
| 184 | let loaded = PosFile::from_json(json).unwrap();
|
| 185 | assert_eq!(loaded.body, "hello");
|
| 186 | assert!(
|
| 187 | !loaded
|
| 188 | .to_json_pretty()
|
| 189 | .unwrap()
|
| 190 | .contains("cut_after_print")
|
| 191 | );
|
| 192 | }
|
| 193 |
|
| 194 | #[test]
|
| 195 | fn save_and_load_file() {
|
| 196 | let dir = tempfile::tempdir().unwrap();
|
| 197 | let path = dir.path().join("test.pos");
|
| 198 | let file = PosFile::new("hello");
|
| 199 | file.save_to_path(&path).unwrap();
|
| 200 | let loaded = PosFile::load_from_path(&path).unwrap();
|
| 201 | assert_eq!(loaded.body, "hello");
|
| 202 | }
|
| 203 | }
|