Skip to content

Commit

Permalink
Replace Wkt with its inner Geometry.
Browse files Browse the repository at this point in the history
This is a large breaking change to simplify the Wkt data structure.

Wkt had a single field - `item` of type `Geometry`. So everything about
a Wkt was determined by its item. In effect, Wkt was serving no purpose.
So I replaced Wkt with it's inner Geometry.
  • Loading branch information
michaelkirk committed Sep 16, 2023
1 parent a5759db commit d0201d9
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 255 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Bump min version of geo-types, and update geo_types::Coordinate to non-deprecated geo_types::Coord
* BREAKING: WktNum must implement PartialEq
* Implement PartialEq for Wkt
* BREAKING: Simplify Wkt data structure by replacing it with it's only field (formerly known as `item: Geometry`)
* BREAKING: Replace geometry_variant.as_item() with Wkt::from(geometry_variant)

## 0.10.3 - 2022-06-22

Expand Down
5 changes: 2 additions & 3 deletions src/deserialize/geo_types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Geometry, Wkt, WktNum};
use crate::{Wkt, WktNum};
use serde::de::{Deserialize, Deserializer, Error};
use std::{default::Default, str::FromStr};

Expand All @@ -9,8 +9,7 @@ where
D: Deserializer<'de>,
T: FromStr + Default + WktNum,
{
Geometry::deserialize(deserializer)
.and_then(|g: Geometry<T>| g.try_into().map_err(D::Error::custom))
Wkt::deserialize(deserializer).and_then(|g: Wkt<T>| g.try_into().map_err(D::Error::custom))
}

/// Deserializes from WKT format into an `Option<geo_types::Point>`.
Expand Down
30 changes: 9 additions & 21 deletions src/deserialize/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! This module deserialises to WKT using [`serde`].
//!
//! You can deserialise to [`geo_types`] or any other implementor of [`TryFromWkt`], using
//! [`deserialize_wkt`]. Or you can store this crates internal primitives [`Wkt`]
//! or [`Geometry`] in your struct fields.
//! [`deserialize_wkt`]. Or you can store this crates internal primitives [`wkt`]
//! or [`Wkt`] in your struct fields.

use crate::{Geometry, TryFromWkt, Wkt, WktNum};
use crate::{TryFromWkt, Wkt, WktNum};
use serde::de::{Deserializer, Error, Visitor};
use std::{
default::Default,
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'de, T> Visitor<'de> for GeometryVisitor<T>
where
T: FromStr + Default + WktNum,
{
type Value = Geometry<T>;
type Value = Wkt<T>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a valid WKT format")
}
Expand All @@ -169,19 +169,7 @@ where
E: Error,
{
let wkt = Wkt::from_str(s).map_err(|e| serde::de::Error::custom(e))?;
Ok(wkt.item)
}
}

impl<'de, T> serde::Deserialize<'de> for Geometry<T>
where
T: FromStr + Default + WktNum,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(GeometryVisitor::default())
Ok(wkt)
}
}

Expand All @@ -190,7 +178,7 @@ mod tests {
use super::*;
use crate::{
types::{Coord, Point},
Geometry,
Wkt,
};
use serde::de::{
value::{Error, StrDeserializer},
Expand All @@ -207,8 +195,8 @@ mod tests {
.deserialize_any(WktVisitor::<f64>::default())
.unwrap();
assert!(matches!(
wkt.item,
Geometry::Point(Point(Some(Coord {
wkt,
Wkt::Point(Point(Some(Coord {
x: _, // floating-point types cannot be used in patterns
y: _, // floating-point types cannot be used in patterns
z: None,
Expand Down Expand Up @@ -239,7 +227,7 @@ mod tests {
.unwrap();
assert!(matches!(
geometry,
Geometry::Point(Point(Some(Coord {
Wkt::Point(Point(Some(Coord {
x: _, // floating-point types cannot be used in patterns
y: _, // floating-point types cannot be used in patterns
z: None,
Expand Down
Loading

0 comments on commit d0201d9

Please sign in to comment.