-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraw_json_to_struct.rs
82 lines (72 loc) · 2.65 KB
/
raw_json_to_struct.rs
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
use cdg_api::response_models::{
parse_response, serialize_response, BillsResponse, GenericResponse,
};
use cdg_api::unwrap_option_string;
const RAW_BILL_DATA: &str = r#"{
"bills": [
{
"congress": 117,
"latestAction": {
"actionDate": "2022-04-06",
"text": "Became Public Law No: 117-108."
},
"number": "3076",
"originChamber": "House",
"originChamberCode": "H",
"title": "Postal Service Reform Act of 2022",
"type": "HR",
"updateDate": "2022-09-29",
"updateDateIncludingText": "2022-09-29T03:27:05Z",
"url": "https://api.congress.gov/v3/bill/117/hr/3076?format=json"
},
{
"congress": 117,
"latestAction": {
"actionDate": "2022-04-06",
"text": "Read twice. Placed on Senate Legislative Calendar under General Orders. Calendar No. 343."
},
"number": "3599",
"originChamber": "House",
"originChamberCode": "H",
"title": "Federal Rotational Cyber Workforce Program Act of 2021",
"type": "HR",
"updateDate": "2022-09-29",
"updateDateIncludingText": "2022-09-29",
"url": "https://api.congress.gov/v3/bill/117/hr/3599?format=json"
}
]
}"#;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let bills: BillsResponse = serde_json::from_str(RAW_BILL_DATA)?;
for bill in bills.bills[..].iter() {
println!(
"{}, {}, {} -- {}\n",
unwrap_option_string(bill.title.clone()),
unwrap_option_string(bill.bill_type.clone()),
unwrap_option_string(bill.number.clone()),
unwrap_option_string(bill.url.clone())
);
}
println!("Total bills: {}", bills.bills.len());
println!("===============================");
let bills: GenericResponse = serde_json::from_str(RAW_BILL_DATA)?;
let bills: BillsResponse = match parse_response(&bills) {
Ok(bills) => bills,
Err(e) => {
eprintln!("Error parsing generic response: {}", e);
println!("{}", serialize_response(&bills, true)?);
return Ok(());
}
};
for bill in bills.bills[..].iter() {
println!(
"{}, {}, {} -- {}\n",
unwrap_option_string(bill.title.clone()),
unwrap_option_string(bill.bill_type.clone()),
unwrap_option_string(bill.number.clone()),
unwrap_option_string(bill.url.clone())
);
}
println!("Total bills: {}", bills.bills.len());
Ok(())
}