1use serde::{Deserialize, Serialize};
2
3#[derive(Serialize, Deserialize, Debug)]
5#[serde(rename_all = "camelCase")]
6pub struct Barcode {
7 pub message: String,
9
10 pub format: BarcodeFormat,
14
15 #[serde(default)]
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub alt_text: Option<String>,
21
22 pub message_encoding: String,
24}
25
26impl Default for Barcode {
27 fn default() -> Self {
29 Self {
30 message: String::new(),
31 format: BarcodeFormat::QR,
32 alt_text: None,
33 message_encoding: String::from("iso-8859-1"),
34 }
35 }
36}
37
38#[derive(Serialize, Deserialize, Debug)]
40pub enum BarcodeFormat {
41 #[serde(rename = "PKBarcodeFormatQR")]
43 QR,
44
45 #[serde(rename = "PKBarcodeFormatPDF417")]
47 PDF417,
48
49 #[serde(rename = "PKBarcodeFormatAztec")]
51 Aztec,
52
53 #[serde(rename = "PKBarcodeFormatCode128")]
55 Code128,
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn make_barcode() {
64 let barcode = Barcode {
66 message: String::from("Hello world!"),
67 format: BarcodeFormat::PDF417,
68 ..Default::default()
69 };
70
71 let json = serde_json::to_string_pretty(&barcode).unwrap();
72
73 println!("{}", json);
74
75 let json_expected = r#"{
76 "message": "Hello world!",
77 "format": "PKBarcodeFormatPDF417",
78 "messageEncoding": "iso-8859-1"
79}"#;
80
81 assert_eq!(json_expected, json);
82
83 let barcode: Barcode = serde_json::from_str(json_expected).unwrap();
85 let json = serde_json::to_string_pretty(&barcode).unwrap();
86 assert_eq!(json_expected, json);
87 }
88}