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

Renamed geojson::GeoJson to geojson::Object to improve clarity #169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
{
Expand All @@ -29,13 +29,13 @@ let geojson_str = r#"
}
"#;

let geojson = geojson_str.parse::<GeoJson>().unwrap();
let geojson = geojson_str.parse::<Object>().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(
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions benches/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ fn parse_benchmark(c: &mut Criterion) {
let geojson_str = include_str!("../tests/fixtures/countries.geojson");

b.iter(|| {
let _ = test::black_box(geojson_str.parse::<geojson::GeoJson>());
let _ = test::black_box(geojson_str.parse::<geojson::Object>());
});
});

c.bench_function("parse (geometry_collection.geojson)", |b| {
let geojson_str = include_str!("../tests/fixtures/geometry_collection.geojson");

b.iter(|| {
let _ = test::black_box(geojson_str.parse::<geojson::GeoJson>());
let _ = test::black_box(geojson_str.parse::<geojson::Object>());
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion benches/to_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<geojson::GeoJson>().unwrap();
let geojson = geojson_str.parse::<geojson::Object>().unwrap();

c.bench_function("quick_collection", move |b| {
b.iter(|| {
Expand Down
8 changes: 4 additions & 4 deletions src/conversion/from_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -562,8 +562,8 @@ mod tests {

use std::iter::FromIterator;

let actual = GeoJson::from_iter(points.iter());
let actual2 = points.iter().collect::<GeoJson>();
let actual = Object::from_iter(points.iter());
let actual2 = points.iter().collect::<Object>();
assert_eq!(actual, actual2);

let expected: serde_json::Value = serde_json::json!({
Expand Down
14 changes: 7 additions & 7 deletions src/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T>(gj: &GeoJson) -> Result<geo_types::GeometryCollection<T>, GJError>
fn process_geojson<T>(gj: &Object) -> Result<geo_types::GeometryCollection<T>, GJError>
where
T: CoordFloat,
{
Expand All @@ -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<T>(geometry: &GjGeometry) -> Result<geo_types::Geometry<T>, GJError>
where
T: CoordFloat,
Expand Down Expand Up @@ -138,7 +138,7 @@ where
///
/// ```
/// use geo_types::GeometryCollection;
/// use geojson::{quick_collection, GeoJson};
/// use geojson::{quick_collection, Object};
///
/// let geojson_str = r#"
/// {
Expand All @@ -158,12 +158,12 @@ where
/// ]
/// }
/// "#;
/// let geojson = geojson_str.parse::<GeoJson>().unwrap();
/// let geojson = geojson_str.parse::<Object>().unwrap();
/// // Turn the GeoJSON string into a geo_types GeometryCollection
/// let mut collection: GeometryCollection<f64> = quick_collection(&geojson).unwrap();
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
pub fn quick_collection<T>(gj: &GeoJson) -> Result<geo_types::GeometryCollection<T>, GJError>
pub fn quick_collection<T>(gj: &Object) -> Result<geo_types::GeometryCollection<T>, GJError>
where
T: CoordFloat,
{
Expand Down
14 changes: 7 additions & 7 deletions src/conversion/to_geo_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -207,23 +207,23 @@ where

fn try_from(val: FeatureCollection) -> Result<geo_types::Geometry<T>, Self::Error> {
Ok(geo_types::Geometry::GeometryCollection(quick_collection(
&GeoJson::FeatureCollection(val),
&Object::FeatureCollection(val),
)?))
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo-types")))]
impl<T> TryFrom<GeoJson> for geo_types::Geometry<T>
impl<T> TryFrom<Object> for geo_types::Geometry<T>
where
T: CoordFloat,
{
type Error = GJError;

fn try_from(val: GeoJson) -> Result<geo_types::Geometry<T>, Self::Error> {
fn try_from(val: Object) -> Result<geo_types::Geometry<T>, 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(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl TryFrom<JsonValue> for Feature {
if let JsonValue::Object(obj) = value {
Self::try_from(obj)
} else {
Err(Error::GeoJsonExpectedObject(value))
Err(Error::ExpectedObject(value))
}
}
}
Expand Down Expand Up @@ -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\":\
Expand Down Expand Up @@ -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()
}

Expand All @@ -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);
Expand Down Expand Up @@ -311,9 +311,9 @@ mod tests {
"properties":{},
"type":"Feature"
}"#;
let geojson = geojson_str.parse::<GeoJson>().unwrap();
let geojson = geojson_str.parse::<Object>().unwrap();
let feature = match geojson {
GeoJson::Feature(feature) => feature,
Object::Feature(feature) => feature,
_ => unimplemented!(),
};
assert!(feature.geometry.is_none());
Expand All @@ -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::<GeoJson>().unwrap_err() {
match geojson_str.parse::<Object>().unwrap_err() {
Error::FeatureInvalidGeometryValue(_) => (),
_ => unreachable!(),
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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::<GeoJson>() {
let result = match feature_json_str.parse::<Object>() {
Err(Error::FeatureInvalidIdentifierType(_)) => true,
Ok(_) => false,
_ => false,
Expand All @@ -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::<GeoJson>() {
let result = match feature_json_str.parse::<Object>() {
Err(Error::FeatureInvalidIdentifierType(_)) => true,
Ok(_) => false,
_ => false,
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/feature_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ 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,
/// features: vec![],
/// foreign_members: None,
/// };
///
/// let serialized = GeoJson::from(feature_collection).to_string();
/// let serialized = Object::from(feature_collection).to_string();
///
/// assert_eq!(
/// serialized,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl TryFrom<JsonValue> for FeatureCollection {
if let JsonValue::Object(obj) = value {
Self::try_from(obj)
} else {
Err(Error::GeoJsonExpectedObject(value))
Err(Error::ExpectedObject(value))
}
}
}
Expand Down
Loading