Try to make the store representation smaller

This commit is contained in:
Alex Wright 2022-10-09 19:46:42 +01:00
parent 68bedf2c36
commit 4ace40319f
2 changed files with 14 additions and 9 deletions

View File

@ -10,3 +10,4 @@ csv = "1.1.6"
serde = { version = "1.0.145", features = ["derive"] }
serde_repr = "0.1.9"
extindex = "0.5.0"
rust_decimal = "1.26.1"

View File

@ -1,9 +1,11 @@
use csv::ReaderBuilder;
use extindex::{Builder, Entry, SerdeWrapper, Reader as ExtReader};
use rust_decimal::Decimal;
use std::error::Error;
use std::io;
use std::path::Path;
use std::process;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use serde;
use serde_repr::*;
@ -74,20 +76,24 @@ struct SmolPostcode {
usertype: UserType,
positional_quality_indicator: PositionalQuality,
country: String,
latitude: Option<String>,
longitude: Option<String>,
latitude: Option<Decimal>,
longitude: Option<Decimal>,
}
impl SmolPostcode {
fn from_postcode(full: Postcode) -> Self {
let to_dec = |n: Option<String>| match n {
Some(str) => Decimal::from_str(&str).ok(),
None => None,
};
SmolPostcode {
postcode: full.postcode.to_owned(),
status: full.status,
usertype: full.usertype,
positional_quality_indicator: full.positional_quality_indicator.to_owned(),
country: full.country.to_owned(),
latitude: full.latitude.to_owned(),
longitude: full.longitude.to_owned(),
latitude: to_dec(full.latitude),
longitude: to_dec(full.longitude),
}
}
}
@ -99,8 +105,10 @@ fn read() -> Result<(), Box<dyn Error>> {
let mut i: u32 = 0;
for result in rdr.deserialize() {
let record: Postcode = result?;
// println!("{}:: {:?}", i, record);
println!("{}:: {:?}", i, &record);
i += 1;
let smol = SmolPostcode::from_postcode(record);
println!("As smol: {:?}", smol);
}
println!("Read {:?} postcodes", i);
Ok(())
@ -127,10 +135,6 @@ fn csv_iter(file: impl io::Read + 'static) -> impl Iterator<Item=String> {
fn main() {
build();
if let Err(err) = read() {
println!("error running example: {}", err);
process::exit(1);
}
}