file_example/
file_example.rs1#[cfg(all(feature = "serde_json", feature = "std"))]
10use serde::{Deserialize, Serialize};
11use std::collections::HashMap;
12use std::path::PathBuf;
13use zcstring::{serde_json_from_zcstring, ZCString};
14
15#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
16pub enum Status {
17 #[serde(rename = "active")]
18 Active,
19 #[serde(rename = "inactive")]
20 Inactive,
21 #[serde(rename = "maintenance")]
22 Maintenance,
23 #[serde(rename = "warning")]
24 Warning,
25}
26
27#[derive(Clone, Debug, Deserialize, Serialize)]
28struct State {
29 population: u64,
30 state_bird: ZCString,
31 capital: ZCString,
32 sensor_id: ZCString,
33 temp_c: f64,
34 status: Status,
35}
36
37#[derive(Debug, Deserialize, Serialize)]
38struct Country {
39 states: HashMap<ZCString, State>,
40}
41
42fn main() -> Result<(), Box<dyn std::error::Error>> {
43 let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
45 path.push("examples");
46 path.push("file_example.json");
48
49 let data = ZCString::from_file(path)?;
50 let country: Country = serde_json_from_zcstring(data)?;
51
52 let active: HashMap<_, _> = country
53 .states
54 .iter()
55 .filter(|(_, data)| data.status == Status::Active)
56 .map(|(k, v)| (k.clone(), v.clone()))
59 .collect();
60
61 println!("{}", serde_json::to_string_pretty(&active)?);
62 Ok(())
63}