diff --git a/CHANGES.md b/CHANGES.md index b3d0b4b6..de770bab 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Changes +## 0.23.0 +* Renamed `geojson::GeoJson` to `geojson::Object` +* moved `src/geojson.rs` to `src/object.rs` + ## 0.22.2 * Added convenience methods to convert from geo_types::Geometry directly to GeoJson diff --git a/README.md b/README.md index cf089e77..8cc409b2 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ This library requires a minimum Rust version of 1.34 (released April 11 2019) ### Reading ```rust -use geojson::GeoJson; +use geojson::Object; let geojson_str = r#" { @@ -29,13 +29,13 @@ let geojson_str = r#" } "#; -let geojson = geojson_str.parse::().unwrap(); +let geojson = geojson_str.parse::().unwrap(); ``` ### Writing ```rust -use geojson::{Feature, GeoJson, Geometry, Value}; +use geojson::{Feature, Object, Geometry, Value}; use serde_json::{Map, to_value}; let geometry = Geometry::new( @@ -48,7 +48,7 @@ properties.insert( to_value("Firestone Grill").unwrap(), ); -let geojson = GeoJson::Feature(Feature { +let geojson = Object::Feature(Feature { bbox: None, geometry: Some(geometry), id: None, diff --git a/benches/parse.rs b/benches/parse.rs index 763695cf..a2921fdf 100644 --- a/benches/parse.rs +++ b/benches/parse.rs @@ -8,7 +8,7 @@ fn parse_benchmark(c: &mut Criterion) { let geojson_str = include_str!("../tests/fixtures/countries.geojson"); b.iter(|| { - let _ = test::black_box(geojson_str.parse::()); + let _ = test::black_box(geojson_str.parse::()); }); }); @@ -16,7 +16,7 @@ fn parse_benchmark(c: &mut Criterion) { let geojson_str = include_str!("../tests/fixtures/geometry_collection.geojson"); b.iter(|| { - let _ = test::black_box(geojson_str.parse::()); + let _ = test::black_box(geojson_str.parse::()); }); }); } diff --git a/benches/to_geo_types.rs b/benches/to_geo_types.rs index 7d741455..192edb6c 100644 --- a/benches/to_geo_types.rs +++ b/benches/to_geo_types.rs @@ -5,7 +5,7 @@ extern crate test; fn parse_benchmark(c: &mut Criterion) { let geojson_str = include_str!("../tests/fixtures/countries.geojson"); - let geojson = geojson_str.parse::().unwrap(); + let geojson = geojson_str.parse::().unwrap(); c.bench_function("quick_collection", move |b| { b.iter(|| { diff --git a/src/conversion/from_geo_types.rs b/src/conversion/from_geo_types.rs index 4213789e..5dba1c32 100644 --- a/src/conversion/from_geo_types.rs +++ b/src/conversion/from_geo_types.rs @@ -269,7 +269,7 @@ where #[cfg(test)] mod tests { - use crate::{GeoJson, Geometry, Value}; + use crate::{Object, Geometry, Value}; use geo_types; use geo_types::{ Coordinate, GeometryCollection, Line, LineString, MultiLineString, MultiPoint, @@ -548,7 +548,7 @@ mod tests { #[test] fn test_from_geo_type_to_geojson() { let p1 = geo_types::Point::new(100.0f64, 0.0f64); - let actual = serde_json::Value::from(GeoJson::from(&p1)); + let actual = serde_json::Value::from(Object::from(&p1)); let expected: serde_json::Value = serde_json::json!({"coordinates": [100.0, 0.0], "type": "Point"}); assert_eq!(expected, actual); @@ -562,8 +562,8 @@ mod tests { use std::iter::FromIterator; - let actual = GeoJson::from_iter(points.iter()); - let actual2 = points.iter().collect::(); + let actual = Object::from_iter(points.iter()); + let actual2 = points.iter().collect::(); assert_eq!(actual, actual2); let expected: serde_json::Value = serde_json::json!({ diff --git a/src/conversion/mod.rs b/src/conversion/mod.rs index 293ce82f..002c19a2 100644 --- a/src/conversion/mod.rs +++ b/src/conversion/mod.rs @@ -18,8 +18,8 @@ use crate::geo_types::{ MultiLineString as GtMultiLineString, MultiPoint as GtMultiPoint, MultiPolygon as GtMultiPolygon, Point as GtPoint, Polygon as GtPolygon, }; -use crate::geojson::GeoJson; -use crate::geojson::GeoJson::{Feature, FeatureCollection, Geometry}; +use crate::geojson::Object; +use crate::geojson::Object::{Feature, FeatureCollection, Geometry}; use crate::geometry::Geometry as GjGeometry; use crate::Error as GJError; @@ -73,7 +73,7 @@ pub(crate) mod from_geo_types; pub(crate) mod to_geo_types; // Process top-level `GeoJSON` items, returning a geo_types::GeometryCollection or an Error -fn process_geojson(gj: &GeoJson) -> Result, GJError> +fn process_geojson(gj: &Object) -> Result, GJError> where T: CoordFloat, { @@ -98,7 +98,7 @@ where } } -// Process GeoJson Geometry objects, returning their geo_types equivalents, or an error +// Process Object Geometry objects, returning their geo_types equivalents, or an error fn process_geometry(geometry: &GjGeometry) -> Result, GJError> where T: CoordFloat, @@ -138,7 +138,7 @@ where /// /// ``` /// use geo_types::GeometryCollection; -/// use geojson::{quick_collection, GeoJson}; +/// use geojson::{quick_collection, Object}; /// /// let geojson_str = r#" /// { @@ -158,12 +158,12 @@ where /// ] /// } /// "#; -/// let geojson = geojson_str.parse::().unwrap(); +/// let geojson = geojson_str.parse::().unwrap(); /// // Turn the GeoJSON string into a geo_types GeometryCollection /// let mut collection: GeometryCollection = quick_collection(&geojson).unwrap(); /// ``` #[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))] -pub fn quick_collection(gj: &GeoJson) -> Result, GJError> +pub fn quick_collection(gj: &Object) -> Result, GJError> where T: CoordFloat, { diff --git a/src/conversion/to_geo_types.rs b/src/conversion/to_geo_types.rs index cafb1286..b1224de1 100644 --- a/src/conversion/to_geo_types.rs +++ b/src/conversion/to_geo_types.rs @@ -4,7 +4,7 @@ use crate::geometry; use crate::Error as GJError; use crate::{ - quick_collection, Feature, FeatureCollection, GeoJson, Geometry, LineStringType, PointType, + quick_collection, Feature, FeatureCollection, Object, Geometry, LineStringType, PointType, PolygonType, }; use std::convert::{TryFrom, TryInto}; @@ -207,23 +207,23 @@ where fn try_from(val: FeatureCollection) -> Result, Self::Error> { Ok(geo_types::Geometry::GeometryCollection(quick_collection( - &GeoJson::FeatureCollection(val), + &Object::FeatureCollection(val), )?)) } } #[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))] -impl TryFrom for geo_types::Geometry +impl TryFrom for geo_types::Geometry where T: CoordFloat, { type Error = GJError; - fn try_from(val: GeoJson) -> Result, Self::Error> { + fn try_from(val: Object) -> Result, Self::Error> { match val { - GeoJson::Geometry(geom) => geom.try_into(), - GeoJson::Feature(feat) => feat.try_into(), - GeoJson::FeatureCollection(fc) => fc.try_into(), + Object::Geometry(geom) => geom.try_into(), + Object::Feature(feat) => feat.try_into(), + Object::FeatureCollection(fc) => fc.try_into(), } } } diff --git a/src/errors.rs b/src/errors.rs index 3dc4bc23..b6df8bc9 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -12,7 +12,7 @@ pub enum Error { #[error("Encountered non-numeric value within 'bbox' array")] BboxExpectedNumericValues(Value), #[error("Encountered a non-object type for GeoJSON: `{0}`")] - GeoJsonExpectedObject(Value), + ExpectedObject(Value), /// This was previously `GeoJsonUnknownType`, but has been split for clarity #[error("Expected a Feature, FeatureCollection, or Geometry, but got an empty type")] EmptyType, diff --git a/src/feature.rs b/src/feature.rs index a2342ef6..6f334d2d 100644 --- a/src/feature.rs +++ b/src/feature.rs @@ -162,7 +162,7 @@ impl TryFrom for Feature { if let JsonValue::Object(obj) = value { Self::try_from(obj) } else { - Err(Error::GeoJsonExpectedObject(value)) + Err(Error::ExpectedObject(value)) } } } @@ -212,7 +212,7 @@ impl Serialize for Id { #[cfg(test)] mod tests { - use crate::{feature, Error, Feature, GeoJson, Geometry, Value}; + use crate::{feature, Error, Feature, Geometry, Object, Value}; fn feature_json_str() -> &'static str { "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"properties\":{},\"type\":\ @@ -249,7 +249,7 @@ mod tests { serde_json::to_string(&feature).unwrap() } - fn decode(json_string: String) -> GeoJson { + fn decode(json_string: String) -> Object { json_string.parse().unwrap() } @@ -263,7 +263,7 @@ mod tests { // Test decoding let decoded_feature = match decode(json_string) { - GeoJson::Feature(f) => f, + Object::Feature(f) => f, _ => unreachable!(), }; assert_eq!(decoded_feature, feature); @@ -311,9 +311,9 @@ mod tests { "properties":{}, "type":"Feature" }"#; - let geojson = geojson_str.parse::().unwrap(); + let geojson = geojson_str.parse::().unwrap(); let feature = match geojson { - GeoJson::Feature(feature) => feature, + Object::Feature(feature) => feature, _ => unimplemented!(), }; assert!(feature.geometry.is_none()); @@ -322,7 +322,7 @@ mod tests { #[test] fn feature_json_invalid_geometry() { let geojson_str = r#"{"geometry":3.14,"properties":{},"type":"Feature"}"#; - match geojson_str.parse::().unwrap_err() { + match geojson_str.parse::().unwrap_err() { Error::FeatureInvalidGeometryValue(_) => (), _ => unreachable!(), } @@ -348,7 +348,7 @@ mod tests { // Test decode let decoded_feature = match decode(feature_json_str.into()) { - GeoJson::Feature(f) => f, + Object::Feature(f) => f, _ => unreachable!(), }; assert_eq!(decoded_feature, feature); @@ -374,7 +374,7 @@ mod tests { // Test decode let decoded_feature = match decode(feature_json_str.into()) { - GeoJson::Feature(f) => f, + Object::Feature(f) => f, _ => unreachable!(), }; assert_eq!(decoded_feature, feature); @@ -383,7 +383,7 @@ mod tests { #[test] fn decode_feature_with_invalid_id_type_object() { let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":{},\"properties\":{},\"type\":\"Feature\"}"; - let result = match feature_json_str.parse::() { + let result = match feature_json_str.parse::() { Err(Error::FeatureInvalidIdentifierType(_)) => true, Ok(_) => false, _ => false, @@ -394,7 +394,7 @@ mod tests { #[test] fn decode_feature_with_invalid_id_type_null() { let feature_json_str = "{\"geometry\":{\"coordinates\":[1.1,2.1],\"type\":\"Point\"},\"id\":null,\"properties\":{},\"type\":\"Feature\"}"; - let result = match feature_json_str.parse::() { + let result = match feature_json_str.parse::() { Err(Error::FeatureInvalidIdentifierType(_)) => true, Ok(_) => false, _ => false, @@ -429,7 +429,7 @@ mod tests { // Test decode let decoded_feature = match decode(feature_json_str.into()) { - GeoJson::Feature(f) => f, + Object::Feature(f) => f, _ => unreachable!(), }; assert_eq!(decoded_feature, feature); diff --git a/src/feature_collection.rs b/src/feature_collection.rs index 5867d970..e7b345f5 100644 --- a/src/feature_collection.rs +++ b/src/feature_collection.rs @@ -31,7 +31,7 @@ use crate::{util, Bbox, Feature}; /// # extern crate geojson; /// # fn main() { /// use geojson::FeatureCollection; -/// use geojson::GeoJson; +/// use geojson::Object; /// /// let feature_collection = FeatureCollection { /// bbox: None, @@ -39,7 +39,7 @@ use crate::{util, Bbox, Feature}; /// foreign_members: None, /// }; /// -/// let serialized = GeoJson::from(feature_collection).to_string(); +/// let serialized = Object::from(feature_collection).to_string(); /// /// assert_eq!( /// serialized, @@ -118,7 +118,7 @@ impl TryFrom for FeatureCollection { if let JsonValue::Object(obj) = value { Self::try_from(obj) } else { - Err(Error::GeoJsonExpectedObject(value)) + Err(Error::ExpectedObject(value)) } } } diff --git a/src/geometry.rs b/src/geometry.rs index 3a9894ad..4b5b6b5f 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -133,7 +133,7 @@ impl TryFrom for Value { if let JsonValue::Object(obj) = value { Self::try_from(obj) } else { - Err(Error::GeoJsonExpectedObject(value)) + Err(Error::ExpectedObject(value)) } } } @@ -193,7 +193,7 @@ impl Serialize for Value { /// Serializing a `Geometry` to a GeoJSON string: /// /// ``` -/// use geojson::{GeoJson, Geometry, Value}; +/// use geojson::{Object, Geometry, Value}; /// use serde_json; /// /// let geometry = Geometry::new(Value::Point(vec![7.428959, 1.513394])); @@ -209,12 +209,12 @@ impl Serialize for Value { /// Deserializing a GeoJSON string into a `Geometry`: /// /// ``` -/// use geojson::{GeoJson, Geometry, Value}; +/// use geojson::{Object, Geometry, Value}; /// /// let geojson_str = "{\"coordinates\":[7.428959,1.513394],\"type\":\"Point\"}"; /// -/// let geometry = match geojson_str.parse::() { -/// Ok(GeoJson::Geometry(g)) => g, +/// let geometry = match geojson_str.parse::() { +/// Ok(Object::Geometry(g)) => g, /// _ => return, /// }; /// @@ -296,7 +296,7 @@ impl TryFrom for Geometry { if let JsonValue::Object(obj) = value { Self::try_from(obj) } else { - Err(Error::GeoJsonExpectedObject(value)) + Err(Error::ExpectedObject(value)) } } } @@ -336,12 +336,12 @@ where mod tests { use crate::json::JsonObject; - use crate::{GeoJson, Geometry, Value}; + use crate::{Geometry, Object, Value}; fn encode(geometry: &Geometry) -> String { serde_json::to_string(&geometry).unwrap() } - fn decode(json_string: String) -> GeoJson { + fn decode(json_string: String) -> Object { json_string.parse().unwrap() } @@ -360,7 +360,7 @@ mod tests { // Test decode let decoded_geometry = match decode(json_string) { - GeoJson::Geometry(g) => g, + Object::Geometry(g) => g, _ => unreachable!(), }; assert_eq!(decoded_geometry, geometry); @@ -430,7 +430,7 @@ mod tests { // Test decode let decoded_geometry = match decode(geometry_json_str.into()) { - GeoJson::Geometry(g) => g, + Object::Geometry(g) => g, _ => unreachable!(), }; assert_eq!(decoded_geometry, geometry); @@ -462,7 +462,7 @@ mod tests { // Test decode let decoded_geometry = match decode(geometry_collection_string.into()) { - GeoJson::Geometry(g) => g, + Object::Geometry(g) => g, _ => unreachable!(), }; assert_eq!(decoded_geometry, geometry_collection); diff --git a/src/lib.rs b/src/lib.rs index 44f877d1..c93c228e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ //! optionally using `serde` for serialisation. Crate users are encouraged to familiarise themselves with the spec, //! as the crate is structured around it. //! # Structure of the Crate -//! GeoJSON can contain one of three top-level objects, reflected in the top-level `geojson::GeoJson` +//! GeoJSON can contain one of three top-level objects, reflected in the top-level `geojson::Object` //! enum members of the same name: //! //! - [`Feature`](struct.Feature.html) @@ -36,7 +36,7 @@ //! If your primary use case for this crate is ingesting `GeoJSON` strings in order to process geometries //! using the algorithms in the [`geo`](https://docs.rs/geo) crate, you can do so by enabling the `geo-types` feature in //! your `Cargo.toml`, and using the [`quick_collection`](fn.quick_collection.html) function to -//! parse [`GeoJson`](enum.GeoJson.html) objects into +//! parse [`Object`](enum.Object.html) objects into //! a [`geo_types::GeometryCollection`](../geo_types/struct.GeometryCollection.html). //! See [here](#conversion-to-geo-objects) for details. //! @@ -78,7 +78,7 @@ //! ## Reading //! //! ``` -//! use geojson::GeoJson; +//! use geojson::Object; //! //! let geojson_str = r#" //! { @@ -99,7 +99,7 @@ //! } //! "#; //! -//! let geojson = geojson_str.parse::().unwrap(); +//! let geojson = geojson_str.parse::().unwrap(); //! ``` //! //! ## Writing @@ -116,10 +116,10 @@ //! properties.insert(String::from("name"), to_value("Firestone Grill").unwrap()); //! ``` //! -//! `GeoJson` can then be serialized by calling `to_string`: +//! `Object` can then be serialized by calling `to_string`: //! //! ```rust -//! use geojson::{Feature, GeoJson, Geometry, Value}; +//! use geojson::{Feature, Geometry, Object, Value}; //! # fn properties() -> ::serde_json::Map { //! # let mut properties = ::serde_json::Map::new(); //! # properties.insert( @@ -133,7 +133,7 @@ //! //! let geometry = Geometry::new(Value::Point(vec![-120.66029, 35.2812])); //! -//! let geojson = GeoJson::Feature(Feature { +//! let geojson = Object::Feature(Feature { //! bbox: None, //! geometry: Some(geometry), //! id: None, @@ -157,24 +157,24 @@ //! ownership of it. //! //! ```rust -//! use geojson::{GeoJson, Geometry, Value}; +//! use geojson::{Geometry, Object, Value}; //! //! /// Process top-level GeoJSON items -//! fn process_geojson(gj: &GeoJson) { +//! fn process_geojson(gj: &Object) { //! match *gj { -//! GeoJson::FeatureCollection(ref ctn) => { +//! Object::FeatureCollection(ref ctn) => { //! for feature in &ctn.features { //! if let Some(ref geom) = feature.geometry { //! match_geometry(geom) //! } //! } //! } -//! GeoJson::Feature(ref feature) => { +//! Object::Feature(ref feature) => { //! if let Some(ref geom) = feature.geometry { //! match_geometry(geom) //! } //! } -//! GeoJson::Geometry(ref geometry) => match_geometry(geometry), +//! Object::Geometry(ref geometry) => match_geometry(geometry), //! } //! } //! @@ -228,7 +228,7 @@ //! ] //! } //! "#; -//! let geojson = geojson_str.parse::().unwrap(); +//! let geojson = geojson_str.parse::().unwrap(); //! process_geojson(&geojson); //! } //! ``` @@ -248,14 +248,14 @@ //! coordinate, point, linestring, polygon, or its multi- equivalent, **and each of these has //! an equivalent `geo` primitive type**, which you can convert to using the `std::convert::TryFrom` trait. //! -//! Unifying these features, the [`quick_collection`](fn.quick_collection.html) function accepts a [`GeoJson`](enum.GeoJson.html) enum +//! Unifying these features, the [`quick_collection`](fn.quick_collection.html) function accepts a [`Object`](enum.Object.html) enum //! and processes it, producing a [`GeometryCollection`](../geo_types/struct.GeometryCollection.html) //! whose members can be transformed, measured, rotated, etc using the algorithms and functions in //! the [`geo`](https://docs.rs/geo) crate: //! //! ``` //! # #[cfg(feature = "geo-types")] -//! use geojson::{quick_collection, GeoJson}; +//! use geojson::{quick_collection, Object}; //! # #[cfg(feature = "geo-types")] //! use geo_types::GeometryCollection; //! # #[cfg(feature = "geo-types")] @@ -278,7 +278,7 @@ //! } //! "#; //! # #[cfg(feature = "geo-types")] -//! let geojson = geojson_str.parse::().unwrap(); +//! let geojson = geojson_str.parse::().unwrap(); //! // Turn the GeoJSON string into a geo_types GeometryCollection //! # #[cfg(feature = "geo-types")] //! let mut collection: GeometryCollection = quick_collection(&geojson).unwrap(); @@ -288,7 +288,7 @@ //! //! ``` //! # #[cfg(feature = "geo-types")] -//! use geojson::GeoJson; +//! use geojson::Object; //! # #[cfg(feature = "geo-types")] //! use geo_types::Geometry; //! use std::convert::TryInto; @@ -308,7 +308,7 @@ //! } //! "#; //! # #[cfg(feature = "geo-types")] -//! let geojson = GeoJson::from_str(geojson_str).unwrap(); +//! let geojson = Object::from_str(geojson_str).unwrap(); //! // Turn the GeoJSON string into a geo_types Geometry //! # #[cfg(feature = "geo-types")] //! let geom: geo_types::Geometry = geojson.try_into().unwrap(); @@ -347,8 +347,11 @@ pub type PolygonType = Vec>; mod util; -mod geojson; -pub use crate::geojson::GeoJson; +mod object; + +pub use crate::object::Object; +// preserve backwards compatibility for old name +pub use crate::object::Object as GeoJson; mod geometry; pub use crate::geometry::{Geometry, Value}; diff --git a/src/geojson.rs b/src/object.rs similarity index 73% rename from src/geojson.rs rename to src/object.rs index b6c4feae..c0ab4013 100644 --- a/src/geojson.rs +++ b/src/object.rs @@ -25,7 +25,7 @@ use std::str::FromStr; /// /// ``` /// use std::convert::TryInto; -/// use geojson::{Feature, GeoJson, Geometry, Value}; +/// use geojson::{Feature, Object, Geometry, Value}; /// use serde_json::json; /// let json_value = json!({ /// "type": "Feature", @@ -37,76 +37,76 @@ use std::str::FromStr; /// }); /// let feature: Feature = json_value.try_into().unwrap(); /// -/// // Easily convert a feature to a GeoJson -/// let geojson: GeoJson = feature.into(); +/// // Easily convert a feature to a Object +/// let geojson: Object = feature.into(); /// // and back again /// let feature2: Feature = geojson.try_into().unwrap(); /// ``` /// [GeoJSON Format Specification ยง 3](https://tools.ietf.org/html/rfc7946#section-3) #[derive(Clone, Debug, PartialEq)] -pub enum GeoJson { +pub enum Object { Geometry(Geometry), Feature(Feature), FeatureCollection(FeatureCollection), } -impl<'a> From<&'a GeoJson> for JsonObject { - fn from(geojson: &'a GeoJson) -> JsonObject { +impl<'a> From<&'a Object> for JsonObject { + fn from(geojson: &'a Object) -> JsonObject { match *geojson { - GeoJson::Geometry(ref geometry) => geometry.into(), - GeoJson::Feature(ref feature) => feature.into(), - GeoJson::FeatureCollection(ref fc) => fc.into(), + Object::Geometry(ref geometry) => geometry.into(), + Object::Feature(ref feature) => feature.into(), + Object::FeatureCollection(ref fc) => fc.into(), } } } -impl From for JsonValue { - fn from(geojson: GeoJson) -> JsonValue { +impl From for JsonValue { + fn from(geojson: Object) -> JsonValue { match geojson { - GeoJson::Geometry(geometry) => JsonValue::Object(JsonObject::from(&geometry)), - GeoJson::Feature(feature) => JsonValue::Object(JsonObject::from(&feature)), - GeoJson::FeatureCollection(fc) => JsonValue::Object(JsonObject::from(&fc)), + Object::Geometry(geometry) => JsonValue::Object(JsonObject::from(&geometry)), + Object::Feature(feature) => JsonValue::Object(JsonObject::from(&feature)), + Object::FeatureCollection(fc) => JsonValue::Object(JsonObject::from(&fc)), } } } -impl> From for GeoJson { +impl> From for Object { fn from(geometry: G) -> Self { - GeoJson::Geometry(geometry.into()) + Object::Geometry(geometry.into()) } } -impl> FromIterator for GeoJson { +impl> FromIterator for Object { fn from_iter>(iter: I) -> Self { use crate::Value; let geometries = iter.into_iter().map(|g| g.into()).collect(); let collection = Value::GeometryCollection(geometries); - GeoJson::Geometry(Geometry::new(collection)) + Object::Geometry(Geometry::new(collection)) } } -impl From for GeoJson { +impl From for Object { fn from(feature: Feature) -> Self { - GeoJson::Feature(feature) + Object::Feature(feature) } } -impl From for GeoJson { - fn from(feature_collection: FeatureCollection) -> GeoJson { - GeoJson::FeatureCollection(feature_collection) +impl From for Object { + fn from(feature_collection: FeatureCollection) -> Object { + Object::FeatureCollection(feature_collection) } } -impl TryFrom for Geometry { +impl TryFrom for Geometry { type Error = Error; - fn try_from(value: GeoJson) -> Result { + fn try_from(value: Object) -> Result { match value { - GeoJson::Geometry(g) => Ok(g), - GeoJson::Feature(_) => Err(Error::ExpectedType { + Object::Geometry(g) => Ok(g), + Object::Feature(_) => Err(Error::ExpectedType { expected: "Geometry".to_string(), actual: "Feature".to_string(), }), - GeoJson::FeatureCollection(_) => Err(Error::ExpectedType { + Object::FeatureCollection(_) => Err(Error::ExpectedType { expected: "Geometry".to_string(), actual: "FeatureCollection".to_string(), }), @@ -114,16 +114,16 @@ impl TryFrom for Geometry { } } -impl TryFrom for Feature { +impl TryFrom for Feature { type Error = Error; - fn try_from(value: GeoJson) -> Result { + fn try_from(value: Object) -> Result { match value { - GeoJson::Geometry(_) => Err(Error::ExpectedType { + Object::Geometry(_) => Err(Error::ExpectedType { expected: "Feature".to_string(), actual: "Geometry".to_string(), }), - GeoJson::Feature(f) => Ok(f), - GeoJson::FeatureCollection(_) => Err(Error::ExpectedType { + Object::Feature(f) => Ok(f), + Object::FeatureCollection(_) => Err(Error::ExpectedType { expected: "Feature".to_string(), actual: "FeatureCollection".to_string(), }), @@ -131,34 +131,34 @@ impl TryFrom for Feature { } } -impl TryFrom for FeatureCollection { +impl TryFrom for FeatureCollection { type Error = Error; - fn try_from(value: GeoJson) -> Result { + fn try_from(value: Object) -> Result { match value { - GeoJson::Geometry(_) => Err(Error::ExpectedType { + Object::Geometry(_) => Err(Error::ExpectedType { expected: "FeatureCollection".to_string(), actual: "Geometry".to_string(), }), - GeoJson::Feature(_) => Err(Error::ExpectedType { + Object::Feature(_) => Err(Error::ExpectedType { expected: "FeatureCollection".to_string(), actual: "Feature".to_string(), }), - GeoJson::FeatureCollection(f) => Ok(f), + Object::FeatureCollection(f) => Ok(f), } } } -impl GeoJson { +impl Object { pub fn from_json_object(object: JsonObject) -> Result { Self::try_from(object) } - /// Converts a JSON Value into a GeoJson object. + /// Converts a JSON Value into a Object object. /// /// # Example /// ``` /// use std::convert::TryInto; - /// use geojson::{Feature, GeoJson, Geometry, Value}; + /// use geojson::{Feature, Object, Geometry, Value}; /// use serde_json::json; /// /// let json_value = json!({ @@ -172,11 +172,11 @@ impl GeoJson { /// /// assert!(json_value.is_object()); /// - /// let geojson: GeoJson = json_value.try_into().unwrap(); + /// let geojson: Object = json_value.try_into().unwrap(); /// /// assert_eq!( /// geojson, - /// GeoJson::Feature(Feature { + /// Object::Feature(Feature { /// bbox: None, /// geometry: Some(Geometry::new(Value::Point(vec![102.0, 0.5]))), /// id: None, @@ -192,10 +192,10 @@ impl GeoJson { /// Convience method to convert to a JSON Value. Uses `From`. /// ``` /// use std::convert::TryFrom; - /// use geojson::GeoJson; + /// use geojson::Object; /// use serde_json::json; /// - /// let geojson = GeoJson::try_from( json!({ + /// let geojson = Object::try_from( json!({ /// "type": "Feature", /// "geometry": { /// "type": "Point", @@ -220,7 +220,7 @@ impl GeoJson { JsonValue::from(self) } - // Deserialize a GeoJson object from an IO stream of JSON + // Deserialize a Object object from an IO stream of JSON pub fn from_reader(rdr: R) -> Result where R: std::io::Read, @@ -229,7 +229,7 @@ impl GeoJson { } } -impl TryFrom for GeoJson { +impl TryFrom for Object { type Error = Error; fn try_from(object: JsonObject) -> Result { @@ -239,23 +239,23 @@ impl TryFrom for GeoJson { }; let type_ = type_.ok_or(Error::EmptyType)?; match type_ { - Type::Feature => Feature::try_from(object).map(GeoJson::Feature), + Type::Feature => Feature::try_from(object).map(Object::Feature), Type::FeatureCollection => { - FeatureCollection::try_from(object).map(GeoJson::FeatureCollection) + FeatureCollection::try_from(object).map(Object::FeatureCollection) } - _ => Geometry::try_from(object).map(GeoJson::Geometry), + _ => Geometry::try_from(object).map(Object::Geometry), } } } -impl TryFrom for GeoJson { +impl TryFrom for Object { type Error = Error; fn try_from(value: JsonValue) -> Result { if let JsonValue::Object(obj) = value { Self::try_from(obj) } else { - Err(Error::GeoJsonExpectedObject(value)) + Err(Error::ExpectedObject(value)) } } } @@ -290,7 +290,7 @@ impl Type { } } -impl Serialize for GeoJson { +impl Serialize for Object { fn serialize(&self, serializer: S) -> Result where S: Serializer, @@ -299,8 +299,8 @@ impl Serialize for GeoJson { } } -impl<'de> Deserialize<'de> for GeoJson { - fn deserialize(deserializer: D) -> Result +impl<'de> Deserialize<'de> for Object { + fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { @@ -308,13 +308,13 @@ impl<'de> Deserialize<'de> for GeoJson { let val = JsonObject::deserialize(deserializer)?; - GeoJson::from_json_object(val).map_err(|e| D::Error::custom(e.to_string())) + Object::from_json_object(val).map_err(|e| D::Error::custom(e.to_string())) } } /// # Example ///``` -/// use geojson::GeoJson; +/// use geojson::Object; /// use std::str::FromStr; /// /// let geojson_str = r#"{ @@ -334,20 +334,20 @@ impl<'de> Deserialize<'de> for GeoJson { /// ] /// } /// "#; -/// let geo_json = GeoJson::from_str(&geojson_str).unwrap(); -/// if let GeoJson::FeatureCollection(collection) = geo_json { +/// let geo_json = Object::from_str(&geojson_str).unwrap(); +/// if let Object::FeatureCollection(collection) = geo_json { /// assert_eq!(1, collection.features.len()); /// } else { /// panic!("expected feature collection"); /// } /// ``` -impl FromStr for GeoJson { +impl FromStr for Object { type Err = Error; fn from_str(s: &str) -> Result { let object = get_object(s)?; - GeoJson::from_json_object(object) + Object::from_json_object(object) } } @@ -359,7 +359,7 @@ fn get_object(s: &str) -> Result { } } -impl fmt::Display for GeoJson { +impl fmt::Display for Object { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ::serde_json::to_string(self) .map_err(|_| fmt::Error) @@ -393,7 +393,7 @@ impl fmt::Display for FeatureCollection { #[cfg(test)] mod tests { - use crate::{Error, Feature, GeoJson, Geometry, Value}; + use crate::{Error, Feature, Geometry, Object, Value}; use serde_json::json; use std::convert::TryInto; use std::str::FromStr; @@ -409,7 +409,7 @@ mod tests { "properties": null }"#; - let g1 = GeoJson::from_reader(json_str.as_bytes()).unwrap(); + let g1 = Object::from_reader(json_str.as_bytes()).unwrap(); let json_value = json!({ "type": "Feature", @@ -420,7 +420,7 @@ mod tests { "properties": null, }); - let g2: GeoJson = json_value.try_into().unwrap(); + let g2: Object = json_value.try_into().unwrap(); assert_eq!(g1, g2); } @@ -438,11 +438,11 @@ mod tests { assert!(json_value.is_object()); - let geojson: GeoJson = json_value.try_into().unwrap(); + let geojson: Object = json_value.try_into().unwrap(); assert_eq!( geojson, - GeoJson::Feature(Feature { + Object::Feature(Feature { bbox: None, geometry: Some(Geometry::new(Value::Point(vec![102.0, 0.5]))), id: None, @@ -471,7 +471,7 @@ mod tests { ] }"#; assert!(matches!( - GeoJson::from_str(geojson_str), + Object::from_str(geojson_str), Err(Error::MalformedJson(_)) )) } diff --git a/tests/roundtrip.rs b/tests/roundtrip.rs index 32c9f1f0..2cae3b20 100644 --- a/tests/roundtrip.rs +++ b/tests/roundtrip.rs @@ -1,6 +1,6 @@ #[cfg(test)] mod roundtrip_tests { - use geojson::GeoJson; + use geojson::Object; use serde_json; use std::fs::File; use std::io::prelude::*; @@ -53,7 +53,7 @@ mod roundtrip_tests { let _ = file.read_to_string(&mut file_contents); // Read and parse the geojson from the file's contents - let geojson = file_contents.parse::().expect("unable to parse"); + let geojson = file_contents.parse::().expect("unable to parse"); // Now that we've successfully decoded the geojson, re-encode it and compare to the // original to make sure nothing was lost.