| 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 file = PosFile::from_json(&json).map_err(LoadError::Json)?;
|
| 58 | if file.version != 1 {
|
| 59 | return Err(LoadError::UnsupportedVersion(file.version));
|
| 60 | }
|
| 61 | Ok(file)
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | impl PosMetadata {
|
| 66 | pub fn printable_dots(&self) -> u16 {
|
| 67 | // 203 DPI is common for thermal printers
|
| 68 | (self.page_width_mm / 25.4 * 203.0).round() as u16
|
| 69 | }
|
| 70 |
|
| 71 | pub fn chars_per_line(&self) -> u32 {
|
| 72 | self.chars_per_line.unwrap_or_else(|| {
|
| 73 | if self.page_width_mm <= 58.0 {
|
| 74 | 32
|
| 75 | } else {
|
| 76 | 48
|
| 77 | }
|
| 78 | })
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | #[derive(Debug)]
|
| 83 | pub enum SaveError {
|
| 84 | Json(serde_json::Error),
|
| 85 | Io(std::io::Error),
|
| 86 | }
|
| 87 |
|
| 88 | impl From<serde_json::Error> for SaveError {
|
| 89 | fn from(value: serde_json::Error) -> Self {
|
| 90 | Self::Json(value)
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | impl From<std::io::Error> for SaveError {
|
| 95 | fn from(value: std::io::Error) -> Self {
|
| 96 | Self::Io(value)
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | #[derive(Debug)]
|
| 101 | pub enum LoadError {
|
| 102 | Io(std::io::Error),
|
| 103 | Json(serde_json::Error),
|
| 104 | UnsupportedVersion(u32),
|
| 105 | }
|
| 106 |
|
| 107 | impl From<std::io::Error> for LoadError {
|
| 108 | fn from(value: std::io::Error) -> Self {
|
| 109 | Self::Io(value)
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | #[cfg(test)]
|
| 114 | mod tests {
|
| 115 | use super::*;
|
| 116 |
|
| 117 | #[test]
|
| 118 | fn roundtrip_json() {
|
| 119 | let file = PosFile::new("# Test");
|
| 120 | let json = file.to_json_pretty().unwrap();
|
| 121 | assert!(!json.contains("default_feed_before_cut"));
|
| 122 | let loaded = PosFile::from_json(&json).unwrap();
|
| 123 | assert_eq!(loaded, file);
|
| 124 | }
|
| 125 |
|
| 126 | #[test]
|
| 127 | fn chars_per_line_defaults() {
|
| 128 | let narrow = PosMetadata {
|
| 129 | page_width_mm: 58.0,
|
| 130 | ..Default::default()
|
| 131 | };
|
| 132 | assert_eq!(narrow.chars_per_line(), 32);
|
| 133 | let wide = PosMetadata::default();
|
| 134 | assert_eq!(wide.chars_per_line(), 48);
|
| 135 | }
|
| 136 |
|
| 137 | #[test]
|
| 138 | fn ignores_legacy_preview_mode_field() {
|
| 139 | let json = r#"{
|
| 140 | "version": 1,
|
| 141 | "metadata": {
|
| 142 | "page_width_mm": 80.0
|
| 143 | },
|
| 144 | "body": "hello"
|
| 145 | }"#;
|
| 146 | let loaded = PosFile::from_json(json).unwrap();
|
| 147 | assert_eq!(loaded.body, "hello");
|
| 148 | assert!(!loaded.to_json_pretty().unwrap().contains("preview_mode"));
|
| 149 | assert!(
|
| 150 | !loaded
|
| 151 | .to_json_pretty()
|
| 152 | .unwrap()
|
| 153 | .contains("default_feed_before_cut")
|
| 154 | );
|
| 155 | assert!(
|
| 156 | !loaded
|
| 157 | .to_json_pretty()
|
| 158 | .unwrap()
|
| 159 | .contains("cut_after_print")
|
| 160 | );
|
| 161 | }
|
| 162 |
|
| 163 | #[test]
|
| 164 | fn ignores_legacy_cut_after_print_field() {
|
| 165 | let json = r#"{
|
| 166 | "version": 1,
|
| 167 | "metadata": {
|
| 168 | "page_width_mm": 80.0,
|
| 169 | "cut_after_print": true
|
| 170 | },
|
| 171 | "body": "hello"
|
| 172 | }"#;
|
| 173 | let loaded = PosFile::from_json(json).unwrap();
|
| 174 | assert_eq!(loaded.body, "hello");
|
| 175 | assert!(
|
| 176 | !loaded
|
| 177 | .to_json_pretty()
|
| 178 | .unwrap()
|
| 179 | .contains("cut_after_print")
|
| 180 | );
|
| 181 | }
|
| 182 |
|
| 183 | #[test]
|
| 184 | fn save_and_load_file() {
|
| 185 | let dir = tempfile::tempdir().unwrap();
|
| 186 | let path = dir.path().join("test.pos");
|
| 187 | let file = PosFile::new("hello");
|
| 188 | file.save_to_path(&path).unwrap();
|
| 189 | let loaded = PosFile::load_from_path(&path).unwrap();
|
| 190 | assert_eq!(loaded.body, "hello");
|
| 191 | }
|
| 192 | }
|