Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the details of conversion failures in an exported error enum #614

Merged
merged 2 commits into from
Feb 6, 2021

Conversation

michaelkirk
Copy link
Member

@michaelkirk michaelkirk commented Jan 29, 2021

  • I agree to follow the project's code of conduct.
  • 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.

impl std::error::Error for InvalidRectCoordinatesError {}

#[allow(deprecated)]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deprecation stuff is sort of orthogonal. We deprecated the only method that produces this error, so we should also deprecate the error itself - in hopes that we can delete them both one day.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@@ -182,94 +181,56 @@ impl<T: CoordNum> Geometry<T> {
}
}

#[derive(Debug)]
pub struct FailedToConvertError;
Copy link
Member Author

@michaelkirk michaelkirk Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 I deleted this FailedToConvertError.

My understanding is that this is not a breaking change, because, though it was public, it was never exported - so no external crate could have typed in geo_types::FailedToConvertError ...so non-breaking, right?

}
}

impl<T: CoordNum> TryFrom<Geometry<T>> for Line<T> {
type Error = FailedToConvertError;
try_from_geometry_impl!(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to leverage more macros to DRY up our code. I'm pretty new to using macros, so if this feels sophomoric or something, let me know - I'm not actually that bothered by the mostly copy-pasted implementations that I replaced.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am completely clueless when it comes to writing macros but this one is readable and I understand what it's doing, so I think that's a good sign?

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>"
Copy link
Member Author

@michaelkirk michaelkirk Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here's what the error output actually looks like -

before:

Could not convert from enum member to concrete type

after:

Expected a geo_types::point::Point<f64>, but found a geo_types::rect::Rect<f64>

Copy link
Member Author

@michaelkirk michaelkirk Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of where I'd like to take advantage of these more specific error messages, is in code like this georust/wkt#57

let wkt = wkt::Wkt::from_str(include_str!("../resources/test_fixtures/louisiana.wkt")).expect("invalid test data");
let ls = geo::LineString::<f32>::try_from(wkt).expect("unexpected geometry in wkt");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great!

MultiPolygon,
Rect,
Triangle
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should GeometryCollection be in here? Nvm nvm nvm

impl std::error::Error for InvalidRectCoordinatesError {}

#[allow(deprecated)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor

@rmanoka rmanoka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool macros! Changes look good to me.

@michaelkirk
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 6, 2021

Merge conflict.

@michaelkirk
Copy link
Member Author

bors retry

(rebased and fixed conflict in CHANGES.md)

@bors
Copy link
Contributor

bors bot commented Feb 6, 2021

Build succeeded:

@bors bors bot merged commit 7e265bd into master Feb 6, 2021
@michaelkirk michaelkirk deleted the mkirk/pub-error branch February 10, 2021 18:03
bors bot added a commit to georust/wkt that referenced this pull request Feb 10, 2021
57: specific errors and `try_into` for inner geo-types r=urschrei 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.
---

**DRAFT**: depends on georust/geo#614 being released first.

Add TryFrom for converting directly to `geo_types::Geometry` enum members. 

```
let ls: geo_types::LineString = geo_types::LineString::try_from(wkt).unwrap();
```

Previously it was:
```
let geom: geo_types::Geometry = geo_types::Geometry::from(wkt).unwrap();
let ls: geo_types::LineString = geo_types::LineString::try_from(geom).unwrap();
```


This introduced two new error cases, so I've also introduced `thiserror` which solves #49

FIXES #49

Co-authored-by: Michael Kirk <michael.code@endoftheworl.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants