1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use serde::{Deserialize, Serialize};
/// Represents a barcode on a pass.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Barcode {
/// (Required) The message or payload to display as a barcode.
pub message: String,
/// (Required) The format of the barcode.
///
/// The barcode format `PKBarcodeFormatCode128` isn’t supported for watchOS.
pub format: BarcodeFormat,
/// The text to display near the barcode.
///
/// For example, a human-readable version of the barcode data in case the barcode doesn’t scan.
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub alt_text: Option<String>,
/// (Required) The IANA character set name of the text encoding to use to convert message from a string representation to a data representation that the system renders as a barcode.
pub message_encoding: String,
}
impl Default for Barcode {
/// Creates an empty `Barcode` with QR.
fn default() -> Self {
Self {
message: String::new(),
format: BarcodeFormat::QR,
alt_text: None,
message_encoding: String::from("iso-8859-1"),
}
}
}
/// Barcode format
#[derive(Serialize, Deserialize, Debug)]
pub enum BarcodeFormat {
/// QR - <https://en.wikipedia.org/wiki/QR_code>
#[serde(rename = "PKBarcodeFormatQR")]
QR,
/// PDF417 - <https://en.wikipedia.org/wiki/PDF417>
#[serde(rename = "PKBarcodeFormatPDF417")]
PDF417,
/// Aztec - <https://en.wikipedia.org/wiki/Aztec_Code>
#[serde(rename = "PKBarcodeFormatAztec")]
Aztec,
/// Code128 - <https://en.wikipedia.org/wiki/Code_128>
#[serde(rename = "PKBarcodeFormatCode128")]
Code128,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn make_barcode() {
// Serialization test
let barcode = Barcode {
message: String::from("Hello world!"),
format: BarcodeFormat::PDF417,
..Default::default()
};
let json = serde_json::to_string_pretty(&barcode).unwrap();
println!("{json}");
let json_expected = r#"{
"message": "Hello world!",
"format": "PKBarcodeFormatPDF417",
"messageEncoding": "iso-8859-1"
}"#;
assert_eq!(json_expected, json);
// Deserialization test
let barcode: Barcode = serde_json::from_str(json_expected).unwrap();
let json = serde_json::to_string_pretty(&barcode).unwrap();
assert_eq!(json_expected, json);
}
}