postcodes/src/main.rs

89 lines
2.4 KiB
Rust
Raw Normal View History

2022-09-18 14:41:07 +00:00
use csv::Reader;
use csv::ReaderBuilder;
use std::error::Error;
use std::io;
use std::process;
use serde::{Deserialize, Serialize};
use serde;
use serde_repr::*;
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
enum Status {
Live,
Terminated,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
enum UserType {
Small,
Large,
}
#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
enum PositionalQuality {
MatchedAddressPostcodeMean = 1,
LandlineMapsInspection = 2,
ApproximateWithin50Meters = 3,
PostcodeUnitMean = 4,
ImputedByONS = 5,
PostcodeSectorMean = 6,
TerminatedPriorToGridlink = 8,
NoGridReferenceAvailable = 9,
// 1 Within the building of the matched address closest to the postcode mean.
// 2 As for status value 1, except by visual inspection of Landline maps (Scotland only).
// 3 Approximate to within 50 metres.
// 4 Postcode unit mean (mean of matched addresses with the same postcode, but not snapped to a building).
// 5 Imputed by ONS, by reference to surrounding postcode grid references.
// 6 Postcode sector mean, (mainly PO Boxes).
// 7 Not used.
// 8 Postcode terminated prior to Gridlink® initiative, last known ONS postcode grid reference.
// 9 No grid reference available.
}
#[derive(Debug, Deserialize)]
struct Postcode {
postcode: String,
status: Status,
usertype: UserType,
#[serde(deserialize_with = "csv::invalid_option")]
easting: Option<u32>,
#[serde(deserialize_with = "csv::invalid_option")]
northing: Option<u32>,
positional_quality_indicator: PositionalQuality,
country: String,
latitude: Option<String>,
longitude: Option<String>,
postcode_no_space: String,
postcode_fixed_width_seven: String,
postcode_fixed_width_eight: String,
postcode_area: String,
postcode_district: String,
postcode_sector: String,
outcode: String,
incode: String,
}
fn read() -> Result<(), Box<dyn Error>> {
let mut rdr = ReaderBuilder::new()
.has_headers(false)
.from_reader(io::stdin());
let mut i: u32 = 0;
for result in rdr.deserialize() {
let record: Postcode = result?;
// println!("{}:: {:?}", i, record);
i += 1;
}
println!("Read {:?} postcodes", i);
Ok(())
}
fn main() {
if let Err(err) = read() {
println!("error running example: {}", err);
process::exit(1);
}
}