escpos_rust/instruction/
print_data.rs

1use std::collections::HashMap;
2
3/// Contains custom information for each print
4///
5/// Some instructions require custom information in order to get printed. The [PrintData](self::PrintData) structure contains such custom information. The builder pattern is used to construct this structure, see [PrintDataBuilder](self::PrintDataBuilder).
6pub 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    /// Constructs a new print data builder
16    pub fn builder() -> PrintDataBuilder {
17        PrintDataBuilder::new()
18    }
19}
20
21/// Helps build a valid [PrintData](self::PrintData)
22pub 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    /// Creates a new print data builder
44    pub fn new() -> PrintDataBuilder {
45        PrintDataBuilder::default()
46    }
47
48    /// Adds a replacement string
49    ///
50    /// Replacement strings are a simple pattern matching replacement, where all matching instances of `target` get replaces by `replacement`
51    ///
52    /// ```rust
53    /// # use escpos_rs::PrintDataBuilder;
54    /// let print_data = PrintDataBuilder::new()
55    ///     // Instances of "%name%" will get replaced with "Carlos"
56    ///     .replacement("%name%", "Carlos")
57    ///     .build();
58    /// ```
59    ///
60    /// Note that there is no particular syntax for the `target` string. `"%name%"` is used in the example so that the word "name" (in case it appears in the text) is safe from this instruction.
61    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}