Skip to content

Commit

Permalink
Merge #111
Browse files Browse the repository at this point in the history
111: Switch to stable TryFrom trait r=frewsxcv a=urschrei

Rust 1.34 [stabilises the TryFrom trait](https://github.com/rust-lang/rust/blob/master/RELEASES.md#std--core), so we can switch to it (after the 11th of April), removing the previous manual implementation.

Co-authored-by: Stephan Hügel <[email protected]>
  • Loading branch information
bors[bot] and urschrei committed Apr 12, 2019
2 parents 39948dd + 8014709 commit 222160d
Show file tree
Hide file tree
Showing 13 changed files with 45,985 additions and 292 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes

## 0.16.0

* Switch to 2018 edition
* Switch to `std::TryFrom` trait

## 0.15.0

* Bump geo-types to 0.4.0.
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

name = "geojson"
description = "Library for serializing the GeoJSON vector GIS file format"
version = "0.15.0"
version = "0.16.0"
authors = ["Corey Farwell <[email protected]>",
"Blake Grotewold <[email protected]>"]
license = "MIT/Apache-2.0"
repository = "https://github.com/georust/geojson"
readme = "README.md"
documentation = "https://docs.rs/geojson/"
keywords = ["geojson", "gis", "json", "geo"]
edition = "2018"

[dependencies]
serde = "~1.0"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ geojson

Library for serializing the [GeoJSON](http://geojson.org) vector GIS file format

## Minimum Rust Version

This library requires a minimum Rust version of 1.34 (released April 11 2019)

## Examples

### Reading
Expand Down
9 changes: 2 additions & 7 deletions benches/parse.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#![feature(test)]

#[macro_use]
extern crate criterion;

use criterion::Criterion;

extern crate geojson;
use criterion::{criterion_group, criterion_main, Criterion};
use geojson;
extern crate test;

fn parse_benchmark(c: &mut Criterion) {
Expand Down
73 changes: 34 additions & 39 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use geo_types;
use geometry;
use crate::geo_types;
use crate::geometry;
use crate::Error as GJError;
use crate::{LineStringType, PointType, PolygonType};
use num_traits::Float;
use std::convert::From;
use Error;
use {LineStringType, PointType, PolygonType};
use std::convert::TryInto;

fn create_point_type<T>(point: &geo_types::Point<T>) -> PointType
where
Expand Down Expand Up @@ -136,7 +137,7 @@ where
let exterior = polygon_type
.get(0)
.map(|e| create_geo_line_string(e))
.unwrap_or(create_geo_line_string(&vec![]));
.unwrap_or_else(|| create_geo_line_string(&vec![]));

let interiors = if polygon_type.len() < 2 {
vec![]
Expand All @@ -150,7 +151,7 @@ where
geo_types::Polygon::new(exterior, interiors)
}

fn create_geo_multi_polygon<T>(multi_polygon_type: &Vec<PolygonType>) -> geo_types::MultiPolygon<T>
fn create_geo_multi_polygon<T>(multi_polygon_type: &[PolygonType]) -> geo_types::MultiPolygon<T>
where
T: Float,
{
Expand All @@ -162,22 +163,16 @@ where
)
}

/// This trait provides fallible conversions from GeoJSON values to [Geo](https://docs.rs/geo) types
pub trait TryInto<T> {
type Err;
fn try_into(self) -> Result<T, Self::Err>;
}

impl<T> TryInto<geo_types::Point<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::Point<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::Point<T>, Self::Error> {
match self {
geometry::Value::Point(point_type) => Ok(create_geo_point(&point_type)),
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -197,17 +192,17 @@ impl<T> TryInto<geo_types::MultiPoint<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::MultiPoint<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::MultiPoint<T>, Self::Error> {
match self {
geometry::Value::MultiPoint(multi_point_type) => Ok(geo_types::MultiPoint(
multi_point_type
.iter()
.map(|point_type| create_geo_point(&point_type))
.collect(),
)),
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -231,14 +226,14 @@ impl<T> TryInto<geo_types::LineString<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::LineString<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::LineString<T>, Self::Error> {
match self {
geometry::Value::LineString(multi_point_type) => {
Ok(create_geo_line_string(&multi_point_type))
}
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -258,14 +253,14 @@ impl<T> TryInto<geo_types::MultiLineString<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::MultiLineString<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::MultiLineString<T>, Self::Error> {
match self {
geometry::Value::MultiLineString(multi_line_string_type) => {
Ok(create_geo_multi_line_string(&multi_line_string_type))
}
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -285,12 +280,12 @@ impl<T> TryInto<geo_types::Polygon<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::Polygon<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::Polygon<T>, Self::Error> {
match self {
geometry::Value::Polygon(polygon_type) => Ok(create_geo_polygon(&polygon_type)),
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -310,14 +305,14 @@ impl<T> TryInto<geo_types::MultiPolygon<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::MultiPolygon<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::MultiPolygon<T>, Self::Error> {
match self {
geometry::Value::MultiPolygon(multi_polygon_type) => {
Ok(create_geo_multi_polygon(&multi_polygon_type))
}
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -337,9 +332,9 @@ impl<T> TryInto<geo_types::GeometryCollection<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::GeometryCollection<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::GeometryCollection<T>, Self::Error> {
match self {
geometry::Value::GeometryCollection(geometries) => {
let geojson_geometries = geometries
Expand All @@ -349,7 +344,7 @@ where

Ok(geo_types::GeometryCollection(geojson_geometries))
}
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand All @@ -358,9 +353,9 @@ impl<T> TryInto<geo_types::Geometry<T>> for geometry::Value
where
T: Float,
{
type Err = Error;
type Error = GJError;

fn try_into(self) -> Result<geo_types::Geometry<T>, Self::Err> {
fn try_into(self) -> Result<geo_types::Geometry<T>, Self::Error> {
match self {
geometry::Value::Point(ref point_type) => {
Ok(geo_types::Geometry::Point(create_geo_point(point_type)))
Expand All @@ -387,7 +382,7 @@ where
geometry::Value::MultiPolygon(ref multi_polygon_type) => Ok(
geo_types::Geometry::MultiPolygon(create_geo_multi_polygon(multi_polygon_type)),
),
_ => Err(Error::GeometryUnknownType),
_ => Err(GJError::GeometryUnknownType),
}
}
}
Expand Down Expand Up @@ -473,12 +468,12 @@ macro_rules! assert_almost_eq {

#[cfg(test)]
mod tests {
use conversion::TryInto;
use crate::{Geometry, Value};
use geo_types;
use geo_types::{
GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point, Polygon,
};
use {Geometry, Value};
use std::convert::TryInto;

#[test]
fn geo_point_conversion_test() {
Expand Down
26 changes: 12 additions & 14 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use json::{Deserialize, Deserializer, JsonObject, Serialize, Serializer};
use serde_json;
use {util, Error, Feature};
use crate::json::{Deserialize, Deserializer, JsonObject, Serialize, Serializer};
use crate::serde_json::json;
use crate::{util, Error, Feature};

impl<'a> From<&'a Feature> for JsonObject {
fn from(feature: &'a Feature) -> JsonObject {
Expand Down Expand Up @@ -111,19 +111,19 @@ impl Serialize for Id {

#[cfg(test)]
mod tests {
use {feature, Error, Feature, GeoJson, Geometry, Value};
use crate::{feature, Error, Feature, GeoJson, Geometry, Value};

fn feature_json_str() -> &'static str {
"{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"properties\":{},\"type\":\
\"Feature\"}"
}

fn properties() -> Option<::json::JsonObject> {
Some(::json::JsonObject::new())
fn properties() -> Option<crate::json::JsonObject> {
Some(crate::json::JsonObject::new())
}

fn feature() -> Feature {
::Feature {
crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
bbox: None,
Expand All @@ -137,8 +137,6 @@ mod tests {
}

fn encode(feature: &Feature) -> String {
use serde_json;

serde_json::to_string(&feature).unwrap()
}

Expand Down Expand Up @@ -189,7 +187,7 @@ mod tests {
#[test]
fn encode_decode_feature_with_id_number() {
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":0,\"properties\":{},\"type\":\"Feature\"}";
let feature = ::Feature {
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
bbox: None,
Expand All @@ -215,7 +213,7 @@ mod tests {
#[test]
fn encode_decode_feature_with_id_string() {
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":\"foo\",\"properties\":{},\"type\":\"Feature\"}";
let feature = ::Feature {
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
bbox: None,
Expand Down Expand Up @@ -258,15 +256,15 @@ mod tests {

#[test]
fn encode_decode_feature_with_foreign_member() {
use json::JsonObject;
use crate::json::JsonObject;
use serde_json;
let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"other_member\":\"some_value\",\"properties\":{},\"type\":\"Feature\"}";
let mut foreign_members = JsonObject::new();
foreign_members.insert(
String::from("other_member"),
serde_json::to_value("some_value").unwrap(),
);
let feature = ::Feature {
let feature = crate::Feature {
geometry: Some(Geometry {
value: Value::Point(vec![1.1, 2.1]),
bbox: None,
Expand Down
9 changes: 4 additions & 5 deletions src/feature_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use json::{Deserialize, Deserializer, JsonObject, Serialize, Serializer};
use serde_json;

use {util, Bbox, Error, Feature};
use crate::json::{Deserialize, Deserializer, JsonObject, Serialize, Serializer};
use crate::serde_json::json;
use crate::{util, Bbox, Error, Feature};

/// Feature Collection Objects
///
Expand Down
Loading

0 comments on commit 222160d

Please sign in to comment.