From 4ace40319f393aa48b14f33c790f7ceada219f7b Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 9 Oct 2022 19:46:42 +0100 Subject: [PATCH] Try to make the store representation smaller --- Cargo.toml | 1 + src/main.rs | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ba492e2..705ac77 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index 7406560..a3acab2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, - longitude: Option, + latitude: Option, + longitude: Option, } impl SmolPostcode { fn from_postcode(full: Postcode) -> Self { + let to_dec = |n: Option| 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> { 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 { fn main() { build(); - if let Err(err) = read() { - println!("error running example: {}", err); - process::exit(1); - } }