BlueGit

timfa posprint

Files

posprint / src / preview.rs

preview.rs

preview.rs 4.1 KiB Download Raw
1use crate::dsl::{Align, Block, Document, Inline};
2use crate::io::PosMetadata;
3
4#[derive(Debug, Clone, PartialEq)]
5pub struct PreviewModel {
6 pub lines: Vec<PreviewLine>,
7 pub width_chars: u32,
8 pub width_mm: f32,
9}
10
11#[derive(Debug, Clone, PartialEq)]
12pub enum PreviewLine {
13 Text {
14 content: String,
15 align: Align,
16 style: TextStyle,
17 },
18 Rule,
19 Barcode {
20 symbology: String,
21 data: String,
22 align: Align,
23 },
24 Qr {
25 data: String,
26 align: Align,
27 },
28 Image {
29 label: String,
30 align: Align,
31 },
32 Spacer(u8),
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
36pub enum TextStyle {
37 #[default]
38 Normal,
39 Heading1,
40 Heading2,
41 Bold,
42 Italic,
43 Code,
44}
45
46pub fn render_preview(document: &Document, metadata: &PosMetadata) -> PreviewModel {
47 let width_chars = metadata.chars_per_line();
48 let mut lines = Vec::new();
49
50 for block in &document.blocks {
51 match block {
52 Block::Paragraph { align, inlines } => {
53 let text = inlines_to_string(inlines);
54 for line in text.split('\n') {
55 lines.push(PreviewLine::Text {
56 content: line.to_string(),
57 align: *align,
58 style: TextStyle::Normal,
59 });
60 }
61 }
62 Block::Heading { level, inlines } => {
63 let style = if *level == 1 {
64 TextStyle::Heading1
65 } else {
66 TextStyle::Heading2
67 };
68 lines.push(PreviewLine::Text {
69 content: inlines_to_string(inlines),
70 align: Align::Center,
71 style,
72 });
73 }
74 Block::Rule => lines.push(PreviewLine::Rule),
75 Block::Feed(n) => lines.push(PreviewLine::Spacer(*n)),
76 Block::Cut => lines.push(PreviewLine::Text {
77 content: "--- cut ---".into(),
78 align: Align::Center,
79 style: TextStyle::Code,
80 }),
81 Block::Image { path, align, .. } => lines.push(PreviewLine::Image {
82 label: format!("[image: {path}]"),
83 align: *align,
84 }),
85 Block::Barcode {
86 symbology,
87 data,
88 align,
89 ..
90 } => lines.push(PreviewLine::Barcode {
91 symbology: format!("{symbology:?}"),
92 data: data.clone(),
93 align: *align,
94 }),
95 Block::Qr { data, align, .. } => lines.push(PreviewLine::Qr {
96 data: data.clone(),
97 align: *align,
98 }),
99 }
100 }
101
102 PreviewModel {
103 lines,
104 width_chars,
105 width_mm: metadata.page_width_mm,
106 }
107}
108
109fn inlines_to_string(inlines: &[Inline]) -> String {
110 let mut out = String::new();
111 for inline in inlines {
112 match inline {
113 Inline::Text(t) => out.push_str(t),
114 Inline::Code(t) => out.push_str(t),
115 Inline::Param(t) => out.push_str(&format!("${{{t}}}")),
116 Inline::Bold(inner) => out.push_str(&inlines_to_string(inner)),
117 Inline::Italic(inner) => out.push_str(&inlines_to_string(inner)),
118 }
119 }
120 out
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126 use crate::dsl::parse;
127
128 #[test]
129 fn renders_paragraph_lines() {
130 let doc = parse("Line1\nLine2").unwrap();
131 let model = render_preview(&doc, &PosMetadata::default());
132 assert!(model.lines.len() >= 2);
133 }
134
135 #[test]
136 fn shows_unresolved_params_in_preview() {
137 let doc = parse("${X}").unwrap();
138 let model = render_preview(&doc, &PosMetadata::default());
139 let PreviewLine::Text { content, .. } = &model.lines[0] else {
140 panic!();
141 };
142 assert_eq!(content, "${X}");
143 }
144}