2 * QR Code generator test worker (Rust)
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.
9 * Copyright (c) Project Nayuki. (MIT License)
10 * https://www.nayuki.io/page/qr-code-generator-library
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
29 extern crate qrcodegen;
31 use qrcodegen::QrCode;
32 use qrcodegen::QrCodeEcc;
33 use qrcodegen::QrSegment;
34 use qrcodegen::Version;
40 // Read data length or exit
41 let length: i16 = read_int();
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");
53 let isascii: bool = data.iter().all(|b| *b < 128);
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);
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)
73 vec![QrSegment::make_bytes(&data)]
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) {
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);
90 Err(_) => println!("-1"),
93 std::io::stdout().flush().unwrap();
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")
108 static ECC_LEVELS: [QrCodeEcc; 4] = [