Jakub Kotur | c72d720 | 2020-12-21 17:28:15 +0100 | [diff] [blame^] | 1 | use std::error::Error; |
| 2 | use std::io; |
| 3 | use std::process; |
| 4 | |
| 5 | use serde::Serialize; |
| 6 | |
| 7 | // Note that structs can derive both Serialize and Deserialize! |
| 8 | #[derive(Debug, Serialize)] |
| 9 | #[serde(rename_all = "PascalCase")] |
| 10 | struct Record<'a> { |
| 11 | city: &'a str, |
| 12 | state: &'a str, |
| 13 | population: Option<u64>, |
| 14 | latitude: f64, |
| 15 | longitude: f64, |
| 16 | } |
| 17 | |
| 18 | fn run() -> Result<(), Box<dyn Error>> { |
| 19 | let mut wtr = csv::Writer::from_writer(io::stdout()); |
| 20 | |
| 21 | wtr.serialize(Record { |
| 22 | city: "Davidsons Landing", |
| 23 | state: "AK", |
| 24 | population: None, |
| 25 | latitude: 65.2419444, |
| 26 | longitude: -165.2716667, |
| 27 | })?; |
| 28 | wtr.serialize(Record { |
| 29 | city: "Kenai", |
| 30 | state: "AK", |
| 31 | population: Some(7610), |
| 32 | latitude: 60.5544444, |
| 33 | longitude: -151.2583333, |
| 34 | })?; |
| 35 | wtr.serialize(Record { |
| 36 | city: "Oakman", |
| 37 | state: "AL", |
| 38 | population: None, |
| 39 | latitude: 33.7133333, |
| 40 | longitude: -87.3886111, |
| 41 | })?; |
| 42 | |
| 43 | wtr.flush()?; |
| 44 | Ok(()) |
| 45 | } |
| 46 | |
| 47 | fn main() { |
| 48 | if let Err(err) = run() { |
| 49 | println!("{}", err); |
| 50 | process::exit(1); |
| 51 | } |
| 52 | } |