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

wkt! macro for compile time checking of static wkt text (like serde_json::json!) #112

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
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
Loading