Skip to content

Instantly share code, notes, and snippets.

@JayKickliter
Created February 23, 2024 17:55
Show Gist options
  • Save JayKickliter/7e70e38a6f8e9e9342b8204c7b692471 to your computer and use it in GitHub Desktop.
Save JayKickliter/7e70e38a6f8e9e9342b8204c7b692471 to your computer and use it in GitHub Desktop.
Parsing coordinate or h3 hex from cli in rust
use h3o::LatLng;
use h3o::Resolution;
use hextree::Cell;
use std::str::FromStr;
#[derive(Debug, Clone, Copy)]
pub struct CoordOrHex(Cell);
impl FromStr for CoordOrHex {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<CoordOrHex, anyhow::Error> {
let raw = if let Some(pos) = s.find(',') {
let (l, r) = s.split_at(pos);
let lat_lon = LatLng::new(f64::from_str(l)?, f64::from_str(r)?)?;
let ci = lat_lon.to_cell(Resolution::Fifteen);
u64::from(ci)
} else {
u64::from_str(s)?
};
let cell = Cell::try_from(raw)?;
Ok(CoordOrHex(cell))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment