use crate::error::GeoError;
use lat_long::Coordinate;
use rfham_core::Agency;
use std::{fmt::Display, str::FromStr};
pub trait GridIdentifier:
Clone
+ Display
+ FromStr
+ PartialEq
+ Eq
+ Into<String>
+ AsRef<str>
+ TryFrom<Coordinate, Error = GeoError>
{
fn is_valid(s: &str) -> bool;
fn as_str(&self) -> &str {
self.as_ref()
}
}
pub trait GridPolygon: Clone + PartialEq + Eq + TryFrom<Self::Identifier> {
type Identifier: GridIdentifier;
fn id(&self) -> &Self::Identifier;
fn vertices(&self) -> Vec<Coordinate>;
fn centroid(&self) -> Coordinate;
}
pub trait GridSystem: Default {
type Identifier: GridIdentifier;
type Poly: GridPolygon<Identifier = Self::Identifier>;
fn name(&self) -> &str;
fn defining_agency(&self) -> Agency;
fn lookup_id(&self, id: &Self::Identifier) -> Result<Option<Self::Poly>, GeoError>;
fn lookup_point(&self, point: &Coordinate) -> Result<Option<Self::Poly>, GeoError> {
self.lookup_id(&Self::Identifier::try_from(*point)?)
}
}