use thiserror::Error;
use crate::LatLng;
#[derive(Debug, Error, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum LatLngError {
#[error("Latitude out of valid range (-90.0, +90.0)")]
InvalidLatitude,
#[error("Longitude out of valid range (-180.0, +180.0)")]
InvalidLongitude,
}
fn validate_latlng(latitude: f64, longitude: f64) -> Result<(), LatLngError> {
if !((-90.0..=90.0).contains(&latitude)) {
Err(LatLngError::InvalidLatitude)
} else if !((-180.0..=180.0).contains(&longitude)) {
Err(LatLngError::InvalidLongitude)
} else {
Ok(())
}
}
impl LatLng {
#[inline]
pub fn new(latitude: f64, longitude: f64) -> Result<Self, LatLngError> {
validate_latlng(latitude, longitude)?;
Ok(Self {
latitude,
longitude,
})
}
#[inline]
pub fn validate(&self) -> Result<(), LatLngError> {
validate_latlng(self.latitude, self.longitude)
}
#[must_use]
#[inline]
pub fn is_valid(&self) -> bool {
self.validate().is_ok()
}
}
impl core::fmt::Display for LatLng {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:.6},{:.6}", self.latitude, self.longitude)
}
}