Skip to content

Commit

Permalink
rename to index find
Browse files Browse the repository at this point in the history
  • Loading branch information
madninja committed Apr 4, 2023
1 parent 3e8cb23 commit f42c903
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
38 changes: 22 additions & 16 deletions generator/src/index.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -23,15 +23,15 @@ impl Cmd {
pub enum IndexCmd {
Generate(Generate),
Export(Export),
Check(Check),
Find(Find),
}

impl IndexCmd {
pub fn run(&self) -> Result<()> {
match self {
Self::Generate(cmd) => cmd.run(),
Self::Export(cmd) => cmd.run(),
Self::Check(cmd) => cmd.run(),
Self::Find(cmd) => cmd.run(),
}
}
}
Expand Down Expand Up @@ -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)
}
}
5 changes: 5 additions & 0 deletions generator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ fn main() -> Result<()> {
let cli = Cli::parse();
cli.run()
}

pub(crate) fn print_json<T: ?Sized + serde::Serialize>(value: &T) -> Result<()> {
println!("{}", serde_json::to_string_pretty(value)?);
Ok(())
}

0 comments on commit f42c903

Please sign in to comment.