Skip to content

Commit

Permalink
specific errors and try_into for inner geo-types
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed Jan 30, 2021
1 parent f049273 commit 39d2722
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
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/56/>
* 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/56/>
* Add `geo-types::Geometry::from(wkt)`
* BREAKING: update geo-types, apply new `geo_types::CoordFloat`
* <https://github.com/georust/wkt/pull/53>
Expand Down
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ keywords = ["geo", "geospatial", "wkt"]
[dependencies]
geo-types = {version = "0.7", optional = true}
num-traits = "0.2"
thiserror = "1.0.23"

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

[patch.crates-io]
# This feature depends on an unmerged geo-types feature.
# Once (and if) that geo-types feature is released, we can merge this and bump
# the min version of geo-types required.
# That's a breaking change for wkt, but we already have an unreleased breaking
# change for WKT in the pipeline, so I'd recommend combinging them into one
# breaking WKT release.
geo-types = { git = "https://github.com/georust/geo", branch = "mkirk/pub-error" }
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)),
}
})
}
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

0 comments on commit 39d2722

Please # to comment.