use crate::{PathKind, complexity::ComplexityResource};
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum Error {
InvalidPath {
kind: PathKind,
minimum_vertices: usize,
actual_vertices: usize,
},
NonFiniteCoordinate {
point_index: usize,
},
ArithmeticOverflow,
NonIntegralResult,
TopologyFailure,
InvalidOffset,
TriangulationFailure,
IntersectingPaths,
LimitExceeded {
resource: ComplexityResource,
limit: usize,
required: usize,
},
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidPath { kind, minimum_vertices, actual_vertices } => write!(
formatter,
"{kind:?} path has {actual_vertices} vertices; at least {minimum_vertices} are required"
),
Self::NonFiniteCoordinate { point_index } => {
write!(formatter, "point {point_index} contains a non-finite coordinate")
}
Self::ArithmeticOverflow => {
formatter.write_str("checked geometry arithmetic overflowed")
}
Self::NonIntegralResult => {
formatter.write_str("the exact result contains a non-integral coordinate")
}
Self::TopologyFailure => formatter.write_str("the polygon arrangement did not close"),
Self::InvalidOffset => formatter.write_str("the offset parameters are invalid"),
Self::TriangulationFailure => {
formatter.write_str("the polygon could not be triangulated")
}
Self::IntersectingPaths => formatter.write_str("the input paths intersect"),
Self::LimitExceeded { resource, limit, required } => {
write!(formatter, "topology requires {required} {resource:?}; limit is {limit}")
}
}
}
}
impl std::error::Error for Error {}
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PathValidationError {
path_index: usize,
point_index: Option<usize>,
error: Error,
}
impl PathValidationError {
pub(crate) fn new(path_index: usize, error: Error) -> Self {
let point_index = match &error {
Error::NonFiniteCoordinate { point_index } => Some(*point_index),
_ => None,
};
Self { path_index, point_index, error }
}
#[must_use]
pub const fn path_index(&self) -> usize {
self.path_index
}
#[must_use]
pub const fn point_index(&self) -> Option<usize> {
self.point_index
}
#[must_use]
pub const fn error(&self) -> &Error {
&self.error
}
#[must_use]
pub fn into_error(self) -> Error {
self.error
}
}
impl fmt::Display for PathValidationError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(point_index) = self.point_index {
write!(formatter, "path {}, point {point_index}: {}", self.path_index, self.error)
} else {
write!(formatter, "path {}: {}", self.path_index, self.error)
}
}
}
impl std::error::Error for PathValidationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.error)
}
}