Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / QRCode / repo / rust / examples / qrcodegen-worker.rs
1 /* 
2  * QR Code generator test worker (Rust)
3  * 
4  * This program reads data and encoding parameters from standard input and writes
5  * QR Code bitmaps to standard output. The I/O format is one integer per line.
6  * Run with no command line arguments. The program is intended for automated
7  * batch testing of end-to-end functionality of this QR Code generator library.
8  * 
9  * Copyright (c) Project Nayuki. (MIT License)
10  * https://www.nayuki.io/page/qr-code-generator-library
11  * 
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of
13  * this software and associated documentation files (the "Software"), to deal in
14  * the Software without restriction, including without limitation the rights to
15  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16  * the Software, and to permit persons to whom the Software is furnished to do so,
17  * subject to the following conditions:
18  * - The above copyright notice and this permission notice shall be included in
19  *   all copies or substantial portions of the Software.
20  * - The Software is provided "as is", without warranty of any kind, express or
21  *   implied, including but not limited to the warranties of merchantability,
22  *   fitness for a particular purpose and noninfringement. In no event shall the
23  *   authors or copyright holders be liable for any claim, damages or other
24  *   liability, whether in an action of contract, tort or otherwise, arising from,
25  *   out of or in connection with the Software or the use or other dealings in the
26  *   Software.
27  */
28
29 extern crate qrcodegen;
30 use qrcodegen::Mask;
31 use qrcodegen::QrCode;
32 use qrcodegen::QrCodeEcc;
33 use qrcodegen::QrSegment;
34 use qrcodegen::Version;
35
36
37 fn main() {
38         loop {
39                 
40                 // Read data length or exit
41                 let length: i16 = read_int();
42                 if length == -1 {
43                         break;
44                 }
45                 
46                 // Read data bytes
47                 let mut data = Vec::<u8>::with_capacity(length as usize);
48                 for _ in 0 .. length {
49                         let b: i16 = read_int();
50                         assert_eq!(i16::from(b as u8), b, "Byte value out of range");
51                         data.push(b as u8);
52                 }
53                 let isascii: bool = data.iter().all(|b| *b < 128);
54                 
55                 // Read encoding parameters
56                 let errcorlvl  = read_int();
57                 let minversion = read_int();
58                 let maxversion = read_int();
59                 let mask       = read_int();
60                 let boostecl   = read_int();
61                 assert!(0 <= errcorlvl && errcorlvl <= 3);
62                 assert!(i16::from(Version::MIN.value()) <= minversion
63                         && minversion <= maxversion
64                         && maxversion <= i16::from(Version::MAX.value()));
65                 assert!(-1 <= mask && mask <= 7);
66                 assert!(boostecl >> 1 == 0);
67                 
68                 // Make segments for encoding
69                 let segs: Vec<QrSegment> = if isascii {
70                         let chrs: Vec<char> = std::str::from_utf8(&data).unwrap().chars().collect();
71                         QrSegment::make_segments(&chrs)
72                 } else {
73                         vec![QrSegment::make_bytes(&data)]
74                 };
75                 
76                 // Try to make QR Code symbol
77                 let msk = if mask == -1 { None } else { Some(Mask::new(mask as u8)) };
78                 match QrCode::encode_segments_advanced(&segs, ECC_LEVELS[errcorlvl as usize],
79                                 Version::new(minversion as u8), Version::new(maxversion as u8), msk, boostecl != 0) {
80                 
81                         Ok(qr) => {
82                                 // Print grid of modules
83                                 println!("{}", qr.version().value());
84                                 for y in 0 .. qr.size() {
85                                         for x in 0 .. qr.size() {
86                                                 println!("{}", qr.get_module(x, y) as i8);
87                                         }
88                                 }
89                         },
90                         Err(_) => println!("-1"),
91                 }
92                 use std::io::Write;
93                 std::io::stdout().flush().unwrap();
94         }
95 }
96
97
98 fn read_int() -> i16 {
99         let mut line = String::new();
100         std::io::stdin().read_line(&mut line).unwrap();
101         let mut chrs: Vec<char> = line.chars().collect();
102         assert_eq!(chrs.pop().unwrap(), '\n');
103         let line: String = chrs.iter().cloned().collect();
104         line.parse::<i16>().expect("Invalid number")
105 }
106
107
108 static ECC_LEVELS: [QrCodeEcc; 4] = [
109         QrCodeEcc::Low,
110         QrCodeEcc::Medium,
111         QrCodeEcc::Quartile,
112         QrCodeEcc::High,
113 ];