#![warn(missing_docs)]
use crate::{CRS, MyError};
use core::fmt;
use std::num::NonZero;
pub const EPSG_4326: SRID = SRID(4326);
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::upper_case_acronyms)]
pub struct SRID(i32);
impl TryFrom<i32> for SRID {
type Error = MyError;
fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
x if x == -1 || x == 0 => Ok(Self(x)),
x => {
let code = usize::try_from(value)?;
let _ = CRS::from_epsg(
NonZero::new(code)
.ok_or(MyError::Runtime("Expected a non-zero EPSG code".into()))?,
)?;
Ok(Self(x))
}
}
}
}
impl TryFrom<usize> for SRID {
type Error = MyError;
fn try_from(value: usize) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self(0)),
x => {
let _ = CRS::from_epsg(
NonZero::new(value)
.ok_or(MyError::Runtime("Expected a non-zero EPSG code".into()))?,
)?;
Ok(Self(x.try_into()?))
}
}
}
}
impl fmt::Display for SRID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0 {
-1 => write!(f, "Undefined (Cartesian)"),
0 => write!(f, "Undefined (geographic)"),
x => write!(f, "EPSG:{x}"),
}
}
}
impl SRID {
pub(crate) fn into_inner(self) -> i32 {
self.0
}
}