Skip to content

Commit

Permalink
Merge georust#79
Browse files Browse the repository at this point in the history
79: BREAKING: move `Wkt::from_str` to `FromStr` trait r=urschrei a=nyurik

Most of the user's code will stay the same, but will require `use std::str::FromStr;`

See also discussion in georust#77 

- [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.
---



Co-authored-by: Yuri Astrakhan <[email protected]>
  • Loading branch information
bors[bot] and nyurik authored Feb 24, 2022
2 parents 823e569 + 03c9dda commit 78f1682
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* <https://github.com/georust/wkt/pull/72>
* BREAKING: Reject empty strings instead of parsing them into an empty `Wkt`.
* <https://github.com/georust/wkt/pull/72>
* BREAKING: move `Wkt::from_str` to `FromStr` trait. Add `use std::str::FromStr;` to your code to use it.
* <https://github.com/georust/wkt/pull/79>
* Switch to 2021 edition and add examples
* <https://github.com/georust/wkt/pull/65>

Expand Down
19 changes: 14 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
//! # Examples
//!
//! ```
//! use std::str::FromStr;
//! use wkt::Wkt;
//! let point: Wkt<f64> = Wkt::from_str("POINT(10 20)").unwrap();
//! ```
Expand All @@ -43,6 +44,7 @@
//! If you wish to work directly with one of the WKT [`types`] you can match on the `item` field
//! ```
//! use std::convert::TryInto;
//! use std::str::FromStr;
//! use wkt::Wkt;
//! use wkt::Geometry;
//!
Expand Down Expand Up @@ -187,11 +189,6 @@ impl<T> Wkt<T>
where
T: WktFloat + FromStr + Default,
{
pub fn from_str(wkt_str: &str) -> Result<Self, &'static str> {
let tokens = Tokens::from_str(wkt_str);
Wkt::from_tokens(tokens)
}

fn from_tokens(tokens: Tokens<T>) -> Result<Self, &'static str> {
let mut tokens = tokens.peekable();
let word = match tokens.next() {
Expand All @@ -210,6 +207,17 @@ where
}
}

impl<T> FromStr for Wkt<T>
where
T: WktFloat + FromStr + Default,
{
type Err = &'static str;

fn from_str(wkt_str: &str) -> Result<Self, Self::Err> {
Wkt::from_tokens(Tokens::from_str(wkt_str))
}
}

trait FromTokens<T>: Sized + Default
where
T: WktFloat + FromStr + Default,
Expand Down Expand Up @@ -265,6 +273,7 @@ where
mod tests {
use crate::types::{Coord, MultiPolygon, Point};
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn empty_string() {
Expand Down
1 change: 1 addition & 0 deletions src/types/geometrycollection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ mod tests {
use super::GeometryCollection;
use crate::types::*;
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_geometrycollection() {
Expand Down
1 change: 1 addition & 0 deletions src/types/linestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ where
mod tests {
use super::{Coord, LineString};
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_linestring() {
Expand Down
1 change: 1 addition & 0 deletions src/types/multilinestring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mod tests {
use super::{LineString, MultiLineString};
use crate::types::Coord;
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_multilinestring() {
Expand Down
1 change: 1 addition & 0 deletions src/types/multipoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod tests {
use super::{MultiPoint, Point};
use crate::types::Coord;
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_multipoint() {
Expand Down
1 change: 1 addition & 0 deletions src/types/multipolygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mod tests {
use super::{MultiPolygon, Polygon};
use crate::types::{Coord, LineString};
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_multipolygon() {
Expand Down
1 change: 1 addition & 0 deletions src/types/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ where
mod tests {
use super::{Coord, Point};
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_point() {
Expand Down
1 change: 1 addition & 0 deletions src/types/polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ mod tests {
use super::{LineString, Polygon};
use crate::types::Coord;
use crate::{Geometry, Wkt};
use std::str::FromStr;

#[test]
fn basic_polygon() {
Expand Down

0 comments on commit 78f1682

Please sign in to comment.