use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Barcode {
pub message: String,
pub format: BarcodeFormat,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub alt_text: Option<String>,
pub message_encoding: String,
}
impl Default for Barcode {
fn default() -> Self {
Self {
message: String::new(),
format: BarcodeFormat::QR,
alt_text: None,
message_encoding: String::from("iso-8859-1"),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub enum BarcodeFormat {
#[serde(rename = "PKBarcodeFormatQR")]
QR,
#[serde(rename = "PKBarcodeFormatPDF417")]
PDF417,
#[serde(rename = "PKBarcodeFormatAztec")]
Aztec,
#[serde(rename = "PKBarcodeFormatCode128")]
Code128,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn make_barcode() {
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);
let barcode: Barcode = serde_json::from_str(json_expected).unwrap();
let json = serde_json::to_string_pretty(&barcode).unwrap();
assert_eq!(json_expected, json);
}
}