#![doc = include_str!("lib.md")]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![doc(test(attr(deny(warnings))))]
mod boolean;
mod dispatch;
mod error;
mod fast;
mod fast_dispatch;
#[cfg(feature = "geo-types")]
pub mod geo_types;
mod geometry;
mod offset;
mod request;
mod standard_dispatch;
mod triangulation;
pub use error::{Error, PathValidationError};
pub use geometry::{
Orientation, Path64, PathD, Paths64, PathsD, Point64, PointD, PointLocation, Rect64, RectD,
clip_to_rect_d, clip_to_rect64, normalize_path_d, normalize_path64, orientation,
point_in_polygon, reverse_path_d, reverse_path64, signed_area2, simplify_paths_d,
simplify_paths64, translate_path_d, translate_path64, trim_collinear_d, trim_collinear64,
validate_path_d, validate_path64, validate_paths_d, validate_paths_d_located, validate_paths64,
validate_paths64_located,
};
pub use offset::{
EndType, JoinType, OffsetOptions, offset_path_d, offset_path64, offset_paths_d, offset_paths64,
};
pub use request::{
BooleanRequest, BooleanRequestD, ClipType, FillRule, boolean_op, boolean_op_d, difference,
difference_d, difference_path, difference_path_d, intersection, intersection_d,
intersection_path, intersection_path_d, union, union_d, union_path, union_path_d,
validate_request, validate_request_d, xor, xor_d, xor_path, xor_path_d,
};
pub use triangulation::{
Triangle64, TriangleD, TriangulationError, TriangulationLimits, TriangulationResource,
triangulate_d, triangulate_d_with_limits, triangulate_path_d, triangulate_path64,
triangulate64, triangulate64_with_limits,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
pub enum PathKind {
Closed,
Open,
}
pub const API_VERSION: &str = env!("CARGO_PKG_VERSION");
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn public_contract_is_constructible() {
assert_eq!(API_VERSION, env!("CARGO_PKG_VERSION"));
assert_eq!(PathKind::Closed, PathKind::Closed);
let request = BooleanRequest {
subjects: &[],
clips: &[],
clip_type: ClipType::Xor,
fill_rule: FillRule::Positive,
};
assert!(validate_request(&request).is_ok());
}
#[test]
fn errors_are_displayable() {
let errors = [
Error::InvalidPath { kind: PathKind::Closed, minimum_vertices: 3, actual_vertices: 2 },
Error::NonFiniteCoordinate { point_index: 4 },
Error::ArithmeticOverflow,
Error::NonIntegralResult,
Error::TopologyFailure,
Error::InvalidOffset,
Error::TriangulationFailure,
Error::IntersectingPaths,
];
for error in errors {
assert!(!error.to_string().is_empty());
}
}
}
#[cfg(all(test, feature = "serde"))]
mod serde_tests {
use super::*;
use serde::{Serialize, de::DeserializeOwned};
fn round_trip<T>(value: &T) -> T
where
T: Serialize + DeserializeOwned,
{
serde_json::from_str(&serde_json::to_string(value).unwrap()).unwrap()
}
#[test]
fn public_value_types_round_trip_through_serde() {
let path = vec![PointD::new(1.25, -2.5), PointD::new(3.75, 4.5)];
assert_eq!(round_trip(&path), path);
assert_eq!(round_trip(&Point64::new(i64::MIN, i64::MAX)), Point64::new(i64::MIN, i64::MAX));
assert_eq!(round_trip(&Rect64::new(5, 6, 1, 2)), Rect64::new(5, 6, 1, 2));
assert_eq!(round_trip(&RectD::new(5.0, 6.0, 1.0, 2.0)), RectD::new(5.0, 6.0, 1.0, 2.0));
assert_eq!(round_trip(&Orientation::CounterClockwise), Orientation::CounterClockwise);
assert_eq!(round_trip(&PointLocation::Boundary), PointLocation::Boundary);
assert_eq!(round_trip(&PathKind::Open), PathKind::Open);
assert_eq!(round_trip(&ClipType::Difference), ClipType::Difference);
assert_eq!(round_trip(&FillRule::Negative), FillRule::Negative);
assert_eq!(round_trip(&JoinType::Miter), JoinType::Miter);
assert_eq!(round_trip(&EndType::Round), EndType::Round);
assert_eq!(round_trip(&OffsetOptions::default()), OffsetOptions::default());
assert_eq!(round_trip(&TriangulationLimits::DEFAULT), TriangulationLimits::DEFAULT);
let limit_error = TriangulationError::LimitExceeded {
resource: TriangulationResource::Vertices,
limit: 4,
required: 5,
};
assert_eq!(round_trip(&limit_error), limit_error);
assert_eq!(round_trip(&Error::TopologyFailure), Error::TopologyFailure);
let located = validate_paths_d_located(
&[vec![PointD::new(0.0, 0.0), PointD::new(f64::NAN, 0.0)]],
PathKind::Open,
)
.unwrap_err();
assert_eq!(round_trip(&located), located);
}
#[test]
fn public_enum_wire_names_are_snake_case() {
assert_eq!(
serde_json::to_string(&Orientation::CounterClockwise).unwrap(),
"\"counter_clockwise\""
);
assert_eq!(serde_json::to_string(&FillRule::EvenOdd).unwrap(), "\"even_odd\"");
assert_eq!(
serde_json::to_string(&TriangulationResource::EdgePairs).unwrap(),
"\"edge_pairs\""
);
}
}