Debian Codegraph @

src/helpers.rs

raw symbols SCIP
1 //! Matrix helpers functions
2 #![deny(unsafe_code)]
3 #![warn(missing_docs)]
4
5 use craterust-analyzer cargo fast_qr 0.13.1 crate/::modulerust-analyzer cargo fast_qr 0.13.1 module/::Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#;
6 use craterust-analyzer cargo fast_qr 0.13.1 crate/::QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#;
7
8 /// Used to print a ` ` (space)
9 const EMPTYrust-analyzer cargo fast_qr 0.13.1 helpers/EMPTY.: char = ' ';
10 /// Used to print a `█`
11 const BLOCKrust-analyzer cargo fast_qr 0.13.1 helpers/BLOCK.: char = '█';
12 /// Used to print a `▀`
13 const TOPrust-analyzer cargo fast_qr 0.13.1 helpers/TOP.: char = '▀';
14 /// Used to print a `▄`
15 const BOTTOMrust-analyzer cargo fast_qr 0.13.1 helpers/BOTTOM.: char = '▄';
16
17 /// Helper to print two lines at the same time
18 fn print_linerust-analyzer cargo fast_qr 0.13.1 helpers/print_line().(line1: &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#], line2: &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#], size: usize) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
19 let mut line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(size);
20 for i in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#size {
21 match (line1[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().(), line2[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..valuerust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]value().()) {
22 (true, true) => line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(EMPTYrust-analyzer cargo fast_qr 0.13.1 helpers/EMPTY.),
23 (true, false) => line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(BOTTOMrust-analyzer cargo fast_qr 0.13.1 helpers/BOTTOM.),
24 (false, true) => line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(TOPrust-analyzer cargo fast_qr 0.13.1 helpers/TOP.),
25 (false, false) => line.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(BLOCKrust-analyzer cargo fast_qr 0.13.1 helpers/BLOCK.),
26 }
27 }
28 line
29 }
30
31 /// Prints a matrix with margins
32 pub fn print_matrix_with_marginrust-analyzer cargo fast_qr 0.13.1 helpers/print_matrix_with_margin().(qr: &QRCoderust-analyzer cargo fast_qr 0.13.1 qr/QRCode#) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
33 let mut out = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
34
35 let line = print_linerust-analyzer cargo fast_qr 0.13.1 helpers/print_line().(
36 &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#::emptyrust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]empty().(true); 177],
37 &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#::emptyrust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]empty().(false); 177],
38 qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.,
39 );
40
41 out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(BOTTOMrust-analyzer cargo fast_qr 0.13.1 helpers/BOTTOM.);
42 out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&line);
43 out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{BOTTOM}\n"));
44
45 // Black background
46 for i in (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1).step_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#step_by().(2) {
47 let line = print_linerust-analyzer cargo fast_qr 0.13.1 helpers/print_line().(&qr[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &qr[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.);
48 out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(BLOCKrust-analyzer cargo fast_qr 0.13.1 helpers/BLOCK.);
49 out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&line);
50 out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{BLOCK}\n"));
51 }
52
53 let line = print_linerust-analyzer cargo fast_qr 0.13.1 helpers/print_line().(&qr[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &[Modulerust-analyzer cargo fast_qr 0.13.1 module/Module#::emptyrust-analyzer cargo fast_qr 0.13.1 module/impl#[Module]empty().(false); 177], qr.sizerust-analyzer cargo fast_qr 0.13.1 qr/QRCode#size.);
54 out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(BLOCKrust-analyzer cargo fast_qr 0.13.1 helpers/BLOCK.);
55 out.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&line);
56 out.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(BLOCKrust-analyzer cargo fast_qr 0.13.1 helpers/BLOCK.);
57
58 out
59 }
60
61 #[cfg(test)]
62 use craterust-analyzer cargo fast_qr 0.13.1 crate/::{compactrust-analyzer cargo fast_qr 0.13.1 compact/::CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#, Versionrust-analyzer cargo fast_qr 0.13.1 version/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_versionrust-analyzer cargo fast_qr 0.13.1 helpers/binary_to_binarystring_version().(binary: [u8; 5430], version: Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#) -> CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR# {
72 let max = version.max_bytesrust-analyzer cargo fast_qr 0.13.1 version/impl#[Version]max_bytes().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().8;
73 CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#::from_arrayrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]from_array().(&binary, max rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().version.missing_bitsrust-analyzer cargo fast_qr 0.13.1 version/impl#[Version]missing_bits().())
74 }