use crate::GeometryError;
use crate::ops::CoverageIssue;
use crate::ops::validate::ValidityIssue;
#[derive(Clone, Debug, PartialEq)]
pub struct Unvouched<T> {
value: T,
reason: UnvouchedReason,
}
impl<T> Unvouched<T> {
pub fn new(value: T, reason: UnvouchedReason) -> Self {
Self { value, reason }
}
pub fn reason(&self) -> &UnvouchedReason {
&self.reason
}
pub fn inner(&self) -> &T {
&self.value
}
pub fn into_inner(self) -> T {
self.value
}
pub fn into_parts(self) -> (T, UnvouchedReason) {
(self.value, self.reason)
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum MaybeVouched<T, U = T> {
Vouched(T),
Unvouched(Unvouched<U>),
}
impl<T, U> MaybeVouched<T, U> {
pub fn vouched(self) -> Result<T, GeometryError> {
match self {
Self::Vouched(v) => Ok(v),
Self::Unvouched(u) => Err(GeometryError::Unvouched {
reason: *u.reason(),
inputs: Vec::new(),
}),
}
}
pub fn is_vouched(&self) -> bool {
matches!(self, Self::Vouched(_))
}
}
impl<V> MaybeVouched<V, V> {
pub fn into_parts(self) -> (V, Option<UnvouchedReason>) {
match self {
Self::Vouched(v) => (v, None),
Self::Unvouched(u) => {
let (v, r) = u.into_parts();
(v, Some(r))
}
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UnvouchedReason {
SelfIntersecting {
edges: (usize, usize),
},
DegenerateRing {
ring: usize,
},
RequiresMultiplePolygons,
RequiresHole,
HoleExceedsExterior {
hole: usize,
},
RingCollapsed {
ring: usize,
},
RingSelfIntersecting {
ring: usize,
edges: (usize, usize),
},
RingsOverlap {
first: usize,
second: usize,
},
HoleMisplaced {
ring: usize,
},
PartSelfIntersecting {
part: usize,
ring: usize,
edges: (usize, usize),
},
RunCollapsed {
run: usize,
},
InvalidInput {
issue: ValidityIssue,
},
InvalidResult {
issue: ValidityIssue,
},
NotDelaunay,
NotAHull,
SimplifiedInvalid {
issue: ValidityIssue,
},
NotACoverage {
first: CoverageIssue,
},
}
impl UnvouchedReason {
pub fn is_locally_invalid(self) -> bool {
matches!(
self,
Self::SelfIntersecting { .. }
| Self::DegenerateRing { .. }
| Self::RingCollapsed { .. }
| Self::RingSelfIntersecting { .. }
| Self::PartSelfIntersecting { .. }
| Self::RunCollapsed { .. }
| Self::InvalidInput { .. }
| Self::InvalidResult { .. }
| Self::SimplifiedInvalid { .. }
)
}
pub fn is_structurally_wrong(self) -> bool {
!self.is_locally_invalid()
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(
clippy::float_cmp,
clippy::cast_possible_truncation,
clippy::cast_precision_loss,
clippy::cast_possible_wrap,
clippy::range_plus_one,
clippy::no_effect_underscore_binding
)]
mod tests {
use super::*;
use crate::types::{Coord2d, CoordSeq, LinearRing, Polygon};
fn triangle() -> Polygon {
Polygon::new_simple(
LinearRing::new(CoordSeq::new(vec![
Coord2d::new(0.0, 0.0),
Coord2d::new(1.0, 0.0),
Coord2d::new(1.0, 1.0),
]))
.unwrap(),
)
.unwrap()
}
#[test]
fn unvouched_carries_its_value_and_reason() {
let u = Unvouched::new(
triangle(),
UnvouchedReason::SelfIntersecting { edges: (0, 2) },
);
assert_eq!(
*u.reason(),
UnvouchedReason::SelfIntersecting { edges: (0, 2) }
);
assert_eq!(u.inner(), &triangle());
let (v, r) = u.into_parts();
assert_eq!(v, triangle());
assert!(r.is_locally_invalid());
}
#[test]
fn every_reason_is_in_exactly_one_class() {
let all = [
UnvouchedReason::SelfIntersecting { edges: (0, 1) },
UnvouchedReason::DegenerateRing { ring: 0 },
UnvouchedReason::RequiresMultiplePolygons,
UnvouchedReason::RequiresHole,
UnvouchedReason::HoleExceedsExterior { hole: 0 },
UnvouchedReason::InvalidInput {
issue: ValidityIssue::DegenerateRing { ring: 0 },
},
UnvouchedReason::InvalidResult {
issue: ValidityIssue::DegenerateRing { ring: 0 },
},
UnvouchedReason::SimplifiedInvalid {
issue: ValidityIssue::DegenerateRing { ring: 0 },
},
UnvouchedReason::NotACoverage {
first: crate::ops::CoverageIssue::Overlap { a: 0, b: 1 },
},
];
for r in all {
assert_ne!(
r.is_locally_invalid(),
r.is_structurally_wrong(),
"{r:?} is in both classes or neither"
);
}
}
#[test]
fn the_two_classes_are_populated_as_documented() {
assert!(UnvouchedReason::SelfIntersecting { edges: (0, 1) }.is_locally_invalid());
assert!(UnvouchedReason::DegenerateRing { ring: 0 }.is_locally_invalid());
assert!(UnvouchedReason::RequiresMultiplePolygons.is_structurally_wrong());
assert!(UnvouchedReason::RequiresHole.is_structurally_wrong());
assert!(UnvouchedReason::HoleExceedsExterior { hole: 0 }.is_structurally_wrong());
}
#[test]
fn self_intersecting_carries_edge_indices() {
let r = UnvouchedReason::SelfIntersecting { edges: (3, 7) };
let UnvouchedReason::SelfIntersecting { edges } = r else {
panic!("wrong variant")
};
assert_eq!(edges, (3usize, 7usize));
}
#[test]
fn hole_exceeds_exterior_names_the_hole() {
let r = UnvouchedReason::HoleExceedsExterior { hole: 2 };
let UnvouchedReason::HoleExceedsExterior { hole } = r else {
panic!("wrong variant")
};
assert_eq!(hole, 2);
}
}