From f42c9037b198a27057e6a56ea40f493e54d12a3a Mon Sep 17 00:00:00 2001 From: Marc Nijdam Date: Mon, 3 Apr 2023 21:05:05 -0400 Subject: [PATCH] rename to index find --- generator/src/index.rs | 38 ++++++++++++++++++++++---------------- generator/src/main.rs | 5 +++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/generator/src/index.rs b/generator/src/index.rs index 463fb3c..bef4492 100644 --- a/generator/src/index.rs +++ b/generator/src/index.rs @@ -1,4 +1,4 @@ -use crate::polyfill; +use crate::{polyfill, print_json}; use anyhow::Result; use byteorder::ReadBytesExt; use flate2::{read::GzDecoder, write::GzEncoder, Compression}; @@ -23,7 +23,7 @@ impl Cmd { pub enum IndexCmd { Generate(Generate), Export(Export), - Check(Check), + Find(Find), } impl IndexCmd { @@ -31,7 +31,7 @@ impl IndexCmd { match self { Self::Generate(cmd) => cmd.run(), Self::Export(cmd) => cmd.run(), - Self::Check(cmd) => cmd.run(), + Self::Find(cmd) => cmd.run(), } } } @@ -163,25 +163,31 @@ impl Export { } } -/// Check membership of a given h3 index in the given binary file +/// Check membership of a given h3 index in all h3idz files in a given folder #[derive(Debug, clap::Args)] -pub struct Check { +pub struct Find { input: path::PathBuf, cell: h3ron::H3Cell, } -impl Check { +impl Find { pub fn run(&self) -> Result<()> { - let hex_set = read_hexset(&self.input)?; - if hex_set.contains(hextree::Cell::from_raw(*self.cell)?) { - println!("Cell {} in {}", self.cell.to_string(), self.input.display()) - } else { - anyhow::bail!( - "Cell {} not in {}", - self.cell.to_string(), - self.input.display() - ) + let paths = std::fs::read_dir(&self.input)?; + let mut matches = vec![]; + let needle = hextree::Cell::from_raw(*self.cell)?; + for path in paths { + let entry = path?.path(); + if entry.extension().map(|ext| ext == "h3idz").unwrap_or(false) { + let hex_set = read_hexset(&entry)?; + if hex_set.contains(needle) { + matches.push(entry); + } + } } - Ok(()) + let json = serde_json::json!({ + "location": self.cell.to_string(), + "matches": matches, + }); + print_json(&json) } } diff --git a/generator/src/main.rs b/generator/src/main.rs index dded67c..376db2c 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -39,3 +39,8 @@ fn main() -> Result<()> { let cli = Cli::parse(); cli.run() } + +pub(crate) fn print_json(value: &T) -> Result<()> { + println!("{}", serde_json::to_string_pretty(value)?); + Ok(()) +}