Refactor and other improvements
This commit is contained in:
parent
04a78e7bfe
commit
6c90cf3c0f
47 changed files with 2705 additions and 1568 deletions
52
server-rs/src/parsing/h3.rs
Normal file
52
server-rs/src/parsing/h3.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use axum::http::StatusCode;
|
||||
use tracing::warn;
|
||||
|
||||
use crate::consts::{H3_PRECOMPUTE_MAX, H3_REQUEST_MAX, H3_REQUEST_MIN};
|
||||
|
||||
/// Validate that an H3 resolution is within the allowed range and convert to h3o::Resolution.
|
||||
pub fn validate_h3_resolution(resolution: u8) -> Result<h3o::Resolution, (StatusCode, String)> {
|
||||
if !(H3_REQUEST_MIN..=H3_REQUEST_MAX).contains(&resolution) {
|
||||
warn!(
|
||||
resolution,
|
||||
"Resolution out of range [{}, {}]", H3_REQUEST_MIN, H3_REQUEST_MAX
|
||||
);
|
||||
return Err((
|
||||
StatusCode::BAD_REQUEST,
|
||||
format!(
|
||||
"resolution must be between {} and {}",
|
||||
H3_REQUEST_MIN, H3_REQUEST_MAX
|
||||
),
|
||||
));
|
||||
}
|
||||
h3o::Resolution::try_from(resolution).map_err(|error| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Invalid H3 resolution {}: {}", resolution, error),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve a row's H3 cell at the requested resolution, using precomputed max-resolution cells.
|
||||
#[inline]
|
||||
pub fn cell_for_row(
|
||||
row: usize,
|
||||
precomputed: &[u64],
|
||||
h3_res: h3o::Resolution,
|
||||
need_parent: bool,
|
||||
) -> u64 {
|
||||
let max_cell = precomputed[row];
|
||||
if !need_parent || max_cell == 0 {
|
||||
return max_cell;
|
||||
}
|
||||
h3o::CellIndex::try_from(max_cell)
|
||||
.ok()
|
||||
.and_then(|ci| ci.parent(h3_res))
|
||||
.map(u64::from)
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Whether the given resolution requires computing a parent from precomputed cells.
|
||||
#[inline]
|
||||
pub fn needs_parent(resolution: u8) -> bool {
|
||||
resolution < H3_PRECOMPUTE_MAX
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue