Skip to content
New issue

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

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

Already on GitHub? # to your account

specific errors and try_into for inner geo-types #57

Merged
merged 1 commit into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

* WKT errors impl `std::error::Error`
* <https://github.com/georust/wkt/pull/57>
* Add TryFrom for converting directly to geo-types::Geometry enum members, such
as `geo_types::LineString::try_from(wkt)`
* <https://github.com/georust/wkt/pull/57>
* Add `geo-types::Geometry::from(wkt)`
* BREAKING: update geo-types, apply new `geo_types::CoordFloat`
* <https://github.com/georust/wkt/pull/53>
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ readme = "README.md"
keywords = ["geo", "geospatial", "wkt"]

[dependencies]
geo-types = {version = "0.7", optional = true}
geo-types = {version = "0.7.1", optional = true}
num-traits = "0.2"
thiserror = "1.0.23"

[dev-dependencies]
criterion = { version = "0.2" }
Expand All @@ -22,3 +23,4 @@ default = ["geo-types"]
[[bench]]
name = "parse"
harness = false

68 changes: 56 additions & 12 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ use Geometry;
use Wkt;

use std::convert::{TryFrom, TryInto};
use std::fmt;

use geo_types::CoordFloat;
use thiserror::Error;

#[derive(Debug)]
#[derive(Error, Debug)]
pub enum Error {
#[error("The WKT Point was empty, but geo_type::Points cannot be empty")]
PointConversionError,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::PointConversionError => {
f.write_str("impossible to convert empty point to geo_type point")
}
}
}
#[error("Mismatched geometry (expected {expected:?}, found {found:?})")]
MismatchedGeometry {
expected: &'static str,
found: &'static str,
},
#[error("Wrong number of Geometries: {0}")]
WrongNumberOfGeometries(usize),
#[error("External error: {0}")]
External(Box<dyn std::error::Error>),
}

impl<T> TryFrom<Wkt<T>> for geo_types::Geometry<T>
Expand All @@ -51,6 +51,50 @@ where
}
}

#[macro_use]
macro_rules! try_from_wkt_impl {
($($type: ident),+) => {
$(
/// Convert a Wkt enum into a specific geo-type
impl<T: CoordFloat> TryFrom<Wkt<T>> for geo_types::$type<T> {
type Error = Error;

fn try_from(mut wkt: Wkt<T>) -> Result<Self, Self::Error> {
match wkt.items.len() {
1 => {
let item = wkt.items.pop().unwrap();
let geometry = geo_types::Geometry::try_from(item)?;
Self::try_from(geometry).map_err(|e| {
match e {
geo_types::Error::MismatchedGeometry { expected, found } => {
Error::MismatchedGeometry { expected, found }
}
// currently only one error type in geo-types error enum, but that seems likely to change
#[allow(unreachable_patterns)]
other => Error::External(Box::new(other)),
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 thought it was worth future-proofing, so we don't have to bump WKT's minimum version of geo-types again if we add to the error enum.

}
})
}
other => Err(Error::WrongNumberOfGeometries(other)),
}
}
}
)+
}
}

try_from_wkt_impl!(
Point,
Line,
LineString,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
Rect,
Triangle
);

impl<T> From<Coord<T>> for geo_types::Coordinate<T>
where
T: CoordFloat,
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub mod types;
#[cfg(feature = "geo-types")]
extern crate geo_types;

extern crate thiserror;

#[cfg(feature = "geo-types")]
pub use towkt::ToWkt;

Expand Down