Debian Codegraph @

src/helpers.rs

raw symbols SCIP
1 //! Matrix helpers functions
2 #![deny(unsafe_code)]
3 #![warn(missing_docs)]
4
5 use crate::module::Module;
6 use crate::QRCode;
7
8 /// Used to print a ` ` (space)
9 const EMPTY: char = ' ';
10 /// Used to print a `█`
11 const BLOCK: char = '█';
12 /// Used to print a `▀`
13 const TOP: char = '▀';
14 /// Used to print a `▄`
15 const BOTTOM: char = '▄';
16
17 /// Helper to print two lines at the same time
18 fn print_line(line1: &[Module], line2: &[Module], size: usize) -> String {
19 let mut line = String::with_capacity(size);
20 for i in 0..size {
21 match (line1[i].value(), line2[i].value()) {
22 (true, true) => line.push(EMPTY),
23 (true, false) => line.push(BOTTOM),
24 (false, true) => line.push(TOP),
25 (false, false) => line.push(BLOCK),
26 }
27 }
28 line
29 }
30
31 /// Prints a matrix with margins
32 pub fn print_matrix_with_margin(qr: &QRCode) -> String {
33 let mut out = String::new();
34
35 let line = print_line(
36 &[Module::empty(true); 177],
37 &[Module::empty(false); 177],
38 qr.size,
39 );
40
41 out.push(BOTTOM);
42 out.push_str(&line);
43 out.push_str(&format!("{BOTTOM}\n"));
44
45 // Black background
46 for i in (0..qr.size - 1).step_by(2) {
47 let line = print_line(&qr[i], &qr[i + 1], qr.size);
48 out.push(BLOCK);
49 out.push_str(&line);
50 out.push_str(&format!("{BLOCK}\n"));
51 }
52
53 let line = print_line(&qr[qr.size - 1], &[Module::empty(false); 177], qr.size);
54 out.push(BLOCK);
55 out.push_str(&line);
56 out.push(BLOCK);
57
58 out
59 }
60
61 #[cfg(test)]
62 use crate::{compact::CompactQR, Version};
63
64 /// Convert a vector of u8 to it's representation in bits
65 ///
66 /// If bits are required by the QR code (referring to 8.6 of the spec), they are added to the end of the vector.
67 ///
68 /// ## Example
69 /// { 101 } => "01100101"
70 #[cfg(test)]
71 pub fn binary_to_binarystring_version(binary: [u8; 5430], version: Version) -> CompactQR {
72 let max = version.max_bytes() * 8;
73 CompactQR::from_array(&binary, max + version.missing_bits())
74 }