Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / QRCode / repo / rust / examples / qrcodegen-demo.rs
1 /* 
2  * QR Code generator demo (Rust)
3  * 
4  * Run this command-line program with no arguments. The program computes a bunch of demonstration
5  * QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.
6  * 
7  * Copyright (c) Project Nayuki. (MIT License)
8  * https://www.nayuki.io/page/qr-code-generator-library
9  * 
10  * Permission is hereby granted, free of charge, to any person obtaining a copy of
11  * this software and associated documentation files (the "Software"), to deal in
12  * the Software without restriction, including without limitation the rights to
13  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14  * the Software, and to permit persons to whom the Software is furnished to do so,
15  * subject to the following conditions:
16  * - The above copyright notice and this permission notice shall be included in
17  *   all copies or substantial portions of the Software.
18  * - The Software is provided "as is", without warranty of any kind, express or
19  *   implied, including but not limited to the warranties of merchantability,
20  *   fitness for a particular purpose and noninfringement. In no event shall the
21  *   authors or copyright holders be liable for any claim, damages or other
22  *   liability, whether in an action of contract, tort or otherwise, arising from,
23  *   out of or in connection with the Software or the use or other dealings in the
24  *   Software.
25  */
26
27 extern crate qrcodegen;
28 use qrcodegen::Mask;
29 use qrcodegen::QrCode;
30 use qrcodegen::QrCodeEcc;
31 use qrcodegen::QrSegment;
32 use qrcodegen::Version;
33
34
35 // The main application program.
36 fn main() {
37         do_basic_demo();
38         do_variety_demo();
39         do_segment_demo();
40         do_mask_demo();
41 }
42
43
44
45 /*---- Demo suite ----*/
46
47 // Creates a single QR Code, then prints it to the console.
48 fn do_basic_demo() {
49         let text: &'static str = "Hello, world!";   // User-supplied Unicode text
50         let errcorlvl: QrCodeEcc = QrCodeEcc::Low;  // Error correction level
51         
52         // Make and print the QR Code symbol
53         let qr: QrCode = QrCode::encode_text(text, errcorlvl).unwrap();
54         print_qr(&qr);
55         println!("{}", qr.to_svg_string(4));
56 }
57
58
59 // Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.
60 fn do_variety_demo() {
61         // Numeric mode encoding (3.33 bits per digit)
62         let qr = QrCode::encode_text("314159265358979323846264338327950288419716939937510", QrCodeEcc::Medium).unwrap();
63         print_qr(&qr);
64         
65         // Alphanumeric mode encoding (5.5 bits per character)
66         let qr = QrCode::encode_text("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCodeEcc::High).unwrap();
67         print_qr(&qr);
68         
69         // Unicode text as UTF-8
70         let qr = QrCode::encode_text("こんにちwa、世界! αβγδ", QrCodeEcc::Quartile).unwrap();
71         print_qr(&qr);
72         
73         // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
74         let qr = QrCode::encode_text(concat!(
75                 "Alice was beginning to get very tired of sitting by her sister on the bank, ",
76                 "and of having nothing to do: once or twice she had peeped into the book her sister was reading, ",
77                 "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice ",
78                 "'without pictures or conversations?' So she was considering in her own mind (as well as she could, ",
79                 "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a ",
80                 "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly ",
81                 "a White Rabbit with pink eyes ran close by her."), QrCodeEcc::High).unwrap();
82         print_qr(&qr);
83 }
84
85
86 // Creates QR Codes with manually specified segments for better compactness.
87 fn do_segment_demo() {
88         // Illustration "silver"
89         let silver0 = "THE SQUARE ROOT OF 2 IS 1.";
90         let silver1 = "41421356237309504880168872420969807856967187537694807317667973799";
91         let qr = QrCode::encode_text(&[silver0, silver1].concat(), QrCodeEcc::Low).unwrap();
92         print_qr(&qr);
93         
94         let segs = vec![
95                 QrSegment::make_alphanumeric(&to_chars(silver0)),
96                 QrSegment::make_numeric(&to_chars(silver1)),
97         ];
98         let qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();
99         print_qr(&qr);
100         
101         // Illustration "golden"
102         let golden0 = "Golden ratio φ = 1.";
103         let golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374";
104         let golden2 = "......";
105         let qr = QrCode::encode_text(&[golden0, golden1, golden2].concat(), QrCodeEcc::Low).unwrap();
106         print_qr(&qr);
107         
108         let segs = vec![
109                 QrSegment::make_bytes(golden0.as_bytes()),
110                 QrSegment::make_numeric(&to_chars(golden1)),
111                 QrSegment::make_alphanumeric(&to_chars(golden2)),
112         ];
113         let qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();
114         print_qr(&qr);
115         
116         // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
117         let madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?";
118         let qr = QrCode::encode_text(madoka, QrCodeEcc::Low).unwrap();
119         print_qr(&qr);
120         
121         let kanjichars: Vec<u32> = vec![  // Kanji mode encoding (13 bits per character)
122                 0x0035, 0x1002, 0x0FC0, 0x0AED, 0x0AD7,
123                 0x015C, 0x0147, 0x0129, 0x0059, 0x01BD,
124                 0x018D, 0x018A, 0x0036, 0x0141, 0x0144,
125                 0x0001, 0x0000, 0x0249, 0x0240, 0x0249,
126                 0x0000, 0x0104, 0x0105, 0x0113, 0x0115,
127                 0x0000, 0x0208, 0x01FF, 0x0008,
128         ];
129         let mut bb = qrcodegen::BitBuffer(Vec::new());
130         for &c in &kanjichars {
131                 bb.append_bits(c, 13);
132         }
133         let segs = vec![
134                 QrSegment::new(qrcodegen::QrSegmentMode::Kanji, kanjichars.len(), bb.0),
135         ];
136         let qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();
137         print_qr(&qr);
138 }
139
140
141 // Creates QR Codes with the same size and contents but different mask patterns.
142 fn do_mask_demo() {
143         // Project Nayuki URL
144         let segs = QrSegment::make_segments(&to_chars("https://www.nayuki.io/"));
145         let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, None, true).unwrap();  // Automatic mask
146         print_qr(&qr);
147         let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, Some(Mask::new(3)), true).unwrap();  // Force mask 3
148         print_qr(&qr);
149         
150         // Chinese text as UTF-8
151         let segs = QrSegment::make_segments(&to_chars("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"));
152         let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(0)), true).unwrap();  // Force mask 0
153         print_qr(&qr);
154         let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(1)), true).unwrap();  // Force mask 1
155         print_qr(&qr);
156         let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(5)), true).unwrap();  // Force mask 5
157         print_qr(&qr);
158         let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(7)), true).unwrap();  // Force mask 7
159         print_qr(&qr);
160 }
161
162
163
164 /*---- Utilities ----*/
165
166 // Prints the given QrCode object to the console.
167 fn print_qr(qr: &QrCode) {
168         let border: i32 = 4;
169         for y in -border .. qr.size() + border {
170                 for x in -border .. qr.size() + border {
171                         let c: char = if qr.get_module(x, y) { '█' } else { ' ' };
172                         print!("{0}{0}", c);
173                 }
174                 println!();
175         }
176         println!();
177 }
178
179
180 // Converts the given borrowed string slice to a new character vector.
181 fn to_chars(text: &str) -> Vec<char> {
182         text.chars().collect()
183 }