From 17d7140393cb1bc8ae41ca315fd70244df289e92 Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Mon, 10 Oct 2022 00:58:53 +0100 Subject: [PATCH] Split in too, added a lookup cli --- Cargo.toml | 9 +++++++++ src/{main.rs => index.rs} | 4 ++-- src/lookup.rs | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) rename src/{main.rs => index.rs} (99%) create mode 100644 src/lookup.rs diff --git a/Cargo.toml b/Cargo.toml index 94f8876..a3102f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,12 @@ serde = { version = "1.0.145", features = ["derive"] } serde_repr = "0.1.9" extindex = "0.5.0" rust_decimal = { version = "1.26.1", features = ["serde-str"] } +pico-args = "0.5.0" + +[[bin]] +name = "index" +path = "src/index.rs" + +[[bin]] +name = "lookup" +path = "src/lookup.rs" diff --git a/src/main.rs b/src/index.rs similarity index 99% rename from src/main.rs rename to src/index.rs index 246ac62..e4a0a8e 100644 --- a/src/main.rs +++ b/src/index.rs @@ -82,7 +82,7 @@ struct Postcode { } #[derive(Serialize, Deserialize, Debug)] -struct SmolPostcode { +pub struct SmolPostcode { postcode: String, usertype: UserType, positional_quality_indicator: PositionalQuality, @@ -117,7 +117,7 @@ fn main() { .filter(|full| match full.status { Status::Live => true, _ => false }) .map(|full: Postcode| SmolPostcode::from_postcode(full)) .map(|smol| Entry::new(smol.postcode.to_owned(), SerdeWrapper(smol))); - + let index_file_path = Path::new("./postcodes.db"); let builder: Builder> = Builder::new(index_file_path); builder.build(entries.into_iter()).unwrap(); diff --git a/src/lookup.rs b/src/lookup.rs new file mode 100644 index 0000000..1e0364c --- /dev/null +++ b/src/lookup.rs @@ -0,0 +1,16 @@ +use extindex::{Builder, Entry, SerdeWrapper, Reader as ExtReader}; +use std::path::Path; + +mod index; +use crate::index::SmolPostcode; + +fn main() { + let mut pargs = pico_args::Arguments::from_env(); + let lookup: String = pargs.free_from_str().unwrap(); + println!("Searching for {}", &lookup); + + let index_file_path = Path::new("./postcodes.db"); + let reader = ExtReader::>::open(index_file_path).unwrap(); + let here = reader.find(&lookup).unwrap().expect("Not found"); + println!("Here: {:?}", here.value().0); +}