-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
614: Specify the details of conversion failures in an exported error enum r=michaelkirk a=michaelkirk - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. --- I came across this while trying to improve ergonomics of the WKT crate, which relies on this conversion logic. (see georust/wkt#57) I *think* this is not a breaking change (see comment inline), but would appreciate confirmation. If I'm wrong, and it indeed is a breaking change, I'd prefer to hold off on merging it for now. Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
- Loading branch information
Showing
5 changed files
with
106 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use std::fmt; | ||
|
||
#[derive(Debug)] | ||
pub enum Error { | ||
MismatchedGeometry { | ||
expected: &'static str, | ||
found: &'static str, | ||
}, | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
Error::MismatchedGeometry { expected, found } => { | ||
write!(f, "Expected a {}, but found a {}", expected, found) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use crate::{Geometry, Point, Rect}; | ||
use std::convert::TryFrom; | ||
|
||
#[test] | ||
fn error_output() { | ||
let point = Point::new(1.0, 2.0); | ||
let point_geometry = Geometry::from(point); | ||
|
||
let rect = Rect::new(Point::new(1.0, 2.0), Point::new(3.0, 4.0)); | ||
let rect_geometry = Geometry::from(rect); | ||
|
||
Point::try_from(point_geometry).expect("failed to unwrap inner enum Point"); | ||
|
||
let failure = Point::try_from(rect_geometry).unwrap_err(); | ||
assert_eq!( | ||
format!("{}", failure), | ||
"Expected a geo_types::point::Point<f64>, but found a geo_types::rect::Rect<f64>" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters