geo_types/
error.rs

1use core::fmt;
2
3#[derive(Debug)]
4pub enum Error {
5    MismatchedGeometry {
6        expected: &'static str,
7        found: &'static str,
8    },
9}
10
11#[cfg(feature = "std")]
12impl std::error::Error for Error {}
13
14impl fmt::Display for Error {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        match self {
17            Error::MismatchedGeometry { expected, found } => {
18                write!(f, "Expected a {expected}, but found a {found}")
19            }
20        }
21    }
22}
23
24#[cfg(test)]
25mod test {
26    use crate::{Geometry, Point, Rect};
27    use alloc::string::ToString;
28    use core::convert::TryFrom;
29
30    #[test]
31    fn error_output() {
32        let point = Point::new(1.0, 2.0);
33        let point_geometry = Geometry::from(point);
34
35        let rect = Rect::new(Point::new(1.0, 2.0), Point::new(3.0, 4.0));
36        let rect_geometry = Geometry::from(rect);
37
38        Point::try_from(point_geometry).expect("failed to unwrap inner enum Point");
39
40        let failure = Point::try_from(rect_geometry).unwrap_err();
41        assert_eq!(
42            failure.to_string(),
43            "Expected a geo_types::geometry::point::Point, but found a geo_types::geometry::rect::Rect"
44        );
45    }
46}