use derive_getters::Getters;
use derive_more::Display;
#[derive(Debug, Clone, Display, Getters)]
#[display("Geo operation failed: {}", source)]
pub struct GeoError {
source: String,
}
impl GeoError {
#[track_caller]
pub fn new(source: impl std::fmt::Display) -> Self {
Self {
source: source.to_string(),
}
}
}
impl std::error::Error for GeoError {}
#[derive(Debug, Clone, Display, Getters)]
#[display("JSON parsing failed: {}", source)]
pub struct GeometryJsonError {
source: String,
}
impl GeometryJsonError {
#[track_caller]
pub fn new(source: &serde_json::Error) -> Self {
Self {
source: source.to_string(),
}
}
}
impl std::error::Error for GeometryJsonError {}
#[derive(Debug, Clone, Display)]
pub enum ArcGISGeometryErrorKind {
#[display("Invalid geometry: {}", _0)]
InvalidGeometry(String),
#[display("Missing coordinate at {}", _0)]
MissingCoordinate(String),
#[display("Empty {} in {}", _0, _1)]
EmptyGeometry(String, String),
#[display("Invalid coordinate value: {}", _0)]
InvalidCoordinate(String),
#[display("Expected {} coordinates, got {}", expected, actual)]
InvalidCoordinateLength {
expected: usize,
actual: usize,
},
#[display("{}", _0)]
Geo(GeoError),
#[display("{}", _0)]
Json(GeometryJsonError),
}
#[derive(Debug, Clone, Display, Getters)]
#[display("ESRI Geometry error: {} at {}:{}", kind, file, line)]
pub struct ArcGISGeometryError {
kind: ArcGISGeometryErrorKind,
line: u32,
file: &'static str,
}
impl ArcGISGeometryError {
#[track_caller]
pub fn new(kind: ArcGISGeometryErrorKind) -> Self {
let loc = std::panic::Location::caller();
Self {
kind,
line: loc.line(),
file: loc.file(),
}
}
}
impl std::error::Error for ArcGISGeometryError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.kind {
ArcGISGeometryErrorKind::Geo(e) => Some(e),
ArcGISGeometryErrorKind::Json(e) => Some(e),
_ => None,
}
}
}
impl From<serde_json::Error> for ArcGISGeometryError {
#[track_caller]
fn from(err: serde_json::Error) -> Self {
let kind = ArcGISGeometryErrorKind::Json(GeometryJsonError::new(&err));
Self::new(kind)
}
}