use crate::GeometryError;
use crate::types::Coord2d;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Point(Coord2d);
impl Point {
pub(crate) fn from_finite(coord: Coord2d) -> Self {
Self(coord)
}
#[inline]
pub fn new(coord: Coord2d) -> Result<Self, GeometryError> {
if !coord.x.is_finite() {
return Err(GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 0,
});
}
if !coord.y.is_finite() {
return Err(GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 1,
});
}
Ok(Self(coord))
}
#[inline]
pub fn x(&self) -> f64 {
self.0.x
}
#[inline]
pub fn y(&self) -> f64 {
self.0.y
}
#[inline]
pub fn coord(&self) -> Coord2d {
self.0
}
}
impl TryFrom<Coord2d> for Point {
type Error = GeometryError;
fn try_from(coord: Coord2d) -> Result<Self, Self::Error> {
Self::new(coord)
}
}
impl From<Point> for Coord2d {
fn from(p: Point) -> Self {
p.0
}
}
#[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::*;
fn c(x: f64, y: f64) -> Coord2d {
Coord2d::new(x, y)
}
#[test]
fn finite_coordinates_are_accepted_and_read_back_exactly() {
let p = Point::new(c(1.5, -2.25)).unwrap();
assert_eq!(p.x(), 1.5);
assert_eq!(p.y(), -2.25);
assert_eq!(p.coord(), c(1.5, -2.25));
assert_eq!(Coord2d::from(p), c(1.5, -2.25));
}
#[test]
fn extreme_but_finite_coordinates_are_accepted() {
assert!(Point::new(c(f64::MAX, f64::MIN)).is_ok());
assert!(Point::new(c(f64::MIN_POSITIVE / 2.0, -0.0)).is_ok());
}
#[test]
fn every_non_finite_component_is_rejected_with_its_axis() {
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert_eq!(
Point::new(c(bad, 0.0)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 0
}
);
assert_eq!(
Point::new(c(0.0, bad)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 1
}
);
}
}
#[test]
fn x_is_reported_when_both_components_are_non_finite() {
assert_eq!(
Point::new(c(f64::NAN, f64::INFINITY)).unwrap_err(),
GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis: 0
}
);
}
#[test]
fn try_from_matches_new() {
assert_eq!(Point::try_from(c(1.0, 2.0)), Point::new(c(1.0, 2.0)));
assert!(Point::try_from(c(f64::NAN, 2.0)).is_err());
}
mod decode_validation {
use super::*;
use crate::codec::{CodecError, decode, encode};
fn point_bytes(x: f64, y: f64) -> Vec<u8> {
let mut v = vec![crate::codec::FORMAT_VERSION, crate::codec::tag::POINT];
v.extend_from_slice(&x.to_le_bytes());
v.extend_from_slice(&y.to_le_bytes());
v
}
#[test]
fn finite_coordinates_decode() {
let p = Point::new(c(1.0, 2.0)).unwrap();
assert_eq!(decode::<Point>(&encode(&p)).unwrap(), p);
}
#[test]
fn non_finite_coordinates_fail_to_decode() {
for (x, y, axis) in [
(f64::NAN, 0.0, 0),
(0.0, f64::INFINITY, 1),
(f64::NEG_INFINITY, f64::NAN, 0),
] {
let err = decode::<Point>(&point_bytes(x, y)).unwrap_err();
assert_eq!(
err,
CodecError::Geometry(GeometryError::NonFiniteCoordinate {
coord_index: 0,
axis
}),
"({x}, {y}) must be refused, not decoded"
);
}
}
#[test]
fn a_refused_stream_yields_no_point_at_all() {
assert!(decode::<Point>(&point_bytes(f64::NAN, 0.0)).is_err());
}
}
}