escpos_rust/instruction/
print_data.rs1use std::collections::HashMap;
2
3pub struct PrintData {
7 pub(crate) replacements: HashMap<String, String>,
8 pub(crate) duo_tables: Option<HashMap<String, Vec<(String, String)>>>,
9 pub(crate) trio_tables: Option<HashMap<String, Vec<(String, String, String)>>>,
10 pub(crate) quad_tables: Option<HashMap<String, Vec<(String, String, String, String)>>>,
11 pub(crate) qr_contents: Option<HashMap<String, String>>
12}
13
14impl PrintData {
15 pub fn builder() -> PrintDataBuilder {
17 PrintDataBuilder::new()
18 }
19}
20
21pub struct PrintDataBuilder {
23 replacements: HashMap<String, String>,
24 duo_tables: Option<HashMap<String, Vec<(String, String)>>>,
25 trio_tables: Option<HashMap<String, Vec<(String, String, String)>>>,
26 quad_tables: Option<HashMap<String, Vec<(String, String, String, String)>>>,
27 qr_contents: Option<HashMap<String, String>>
28}
29
30impl Default for PrintDataBuilder {
31 fn default() -> Self {
32 PrintDataBuilder {
33 replacements: HashMap::new(),
34 duo_tables: None,
35 trio_tables: None,
36 quad_tables: None,
37 qr_contents: None
38 }
39 }
40}
41
42impl PrintDataBuilder {
43 pub fn new() -> PrintDataBuilder {
45 PrintDataBuilder::default()
46 }
47
48 pub fn replacement<A: Into<String>, B: Into<String>>(mut self, target: A, replacement: B) -> Self {
62 self.replacements.insert(target.into(), replacement.into());
63 self
64 }
65
66 pub fn add_duo_table<A: Into<String>>(mut self, name: A, rows: Vec<(String, String)>) -> Self {
67 if let Some(duo_tables) = &mut self.duo_tables {
68 duo_tables.insert(name.into(), rows);
69 } else {
70 self.duo_tables = Some(vec![(name.into(), rows)].into_iter().collect());
71 }
72 self
73 }
74
75 pub fn add_trio_table<A: Into<String>>(mut self, name: A, rows: Vec<(String, String, String)>) -> Self {
76 if let Some(trio_tables) = &mut self.trio_tables {
77 trio_tables.insert(name.into(), rows);
78 } else {
79 self.trio_tables = Some(vec![(name.into(), rows)].into_iter().collect());
80 }
81 self
82 }
83
84 pub fn add_quad_table<A: Into<String>>(mut self, name: A, rows: Vec<(String, String, String, String)>) -> Self {
85 if let Some(quad_tables) = &mut self.quad_tables {
86 quad_tables.insert(name.into(), rows);
87 } else {
88 self.quad_tables = Some(vec![(name.into(), rows)].into_iter().collect());
89 }
90 self
91 }
92
93 pub fn add_qr_code<A: Into<String>, B: Into<String>>(mut self, name: A, content: B) -> Self {
94 if let Some(qr_contents) = &mut self.qr_contents {
95 qr_contents.insert(name.into(), content.into());
96 } else {
97 self.qr_contents = Some(vec![(name.into(), content.into())].into_iter().collect());
98 }
99 self
100 }
101
102 pub fn build(self) -> PrintData {
103 PrintData {
104 replacements: self.replacements,
105 duo_tables: self.duo_tables,
106 trio_tables: self.trio_tables,
107 quad_tables: self.quad_tables,
108 qr_contents: self.qr_contents
109 }
110 }
111}