#![forbid(unsafe_code)]
use g_math::fixed_point::imperative::FixedVector;
include!(concat!(env!("OUT_DIR"), "/format.rs"));
pub mod core;
pub mod mixing;
pub mod mode;
pub mod eval;
pub mod compat;
pub use crate::core::config::{CoordinateConfig, MAX_DIMENSION};
pub use crate::core::manifold::{geodesic_distance, ManifoldType};
pub use crate::mixing::geodesic::{GeodesicWalk, LabelOrder, WalkLimits};
pub use mode::domain::DomainHasher;
pub use mode::speed::SpeedHasher;
pub use mode::hierarchical::HierarchicalHasher;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ToponError {
InvalidDomain,
MathOverflow,
InvalidConfig,
DegenerateVector,
DepthExceeded { max_depth: usize },
}
impl std::fmt::Display for ToponError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ToponError::InvalidDomain => write!(f, "invalid input"),
ToponError::MathOverflow => write!(f, "arithmetic overflow"),
ToponError::InvalidConfig => write!(f, "invalid configuration"),
ToponError::DegenerateVector => write!(f, "degenerate zero vector"),
ToponError::DepthExceeded { max_depth } => {
write!(f, "hierarchy deeper than max_depth = {}", max_depth)
}
}
}
}
impl std::error::Error for ToponError {}
impl From<g_math::fixed_point::imperative::OverflowDetected> for ToponError {
fn from(_: g_math::fixed_point::imperative::OverflowDetected) -> Self {
ToponError::MathOverflow
}
}
pub trait GeometricHash {
fn name(&self) -> &str;
fn hash(&self, input: &[u8]) -> Result<FixedVector<FixedPoint>, ToponError>;
fn output_dimension(&self) -> usize;
fn manifold_type(&self) -> ManifoldType;
}
pub trait DomainHash: GeometricHash {
fn hash_domain(&self, domain: &str) -> Result<FixedVector<FixedPoint>, ToponError> {
let normalized = crate::core::normalize::normalize_domain(domain)?;
self.hash(normalized.as_bytes())
}
}
impl<T: GeometricHash> DomainHash for T {}
pub fn domain_hasher() -> DomainHasher {
DomainHasher::new(CoordinateConfig::default())
.expect("the default CoordinateConfig is always valid")
}
pub fn speed_hasher(config: CoordinateConfig) -> Result<SpeedHasher, ToponError> {
SpeedHasher::new(config)
}
pub fn hierarchical_hasher(config: CoordinateConfig) -> Result<HierarchicalHasher, ToponError> {
HierarchicalHasher::new(config)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_config_is_valid() {
CoordinateConfig::default()
.validate()
.expect("default config must stay valid");
let _ = domain_hasher();
}
}