1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::borrow::Borrow;
use std::marker::PhantomData;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use geojson::Geometry;
use super::mapping::{GeoShapeFieldType, GeoShapeMapping};

/**
Geo shape type with a given mapping.

Defining a `geo_shape` with a mapping:

```
# extern crate elastic_types;
extern crate geojson;
use geojson::{ Geometry, Value };

# use elastic_types::prelude::*;
# fn main() {
let point: GeoShape<DefaultGeoShapeMapping> = GeoShape::new(
    Geometry::new(
        Value::Point(vec![ 1.0, 1.0 ])
    )
);
# }
```
*/
#[derive(Debug, Clone, PartialEq)]
pub struct GeoShape<TMapping>
where
    TMapping: GeoShapeMapping,
{
    value: Geometry,
    _m: PhantomData<TMapping>,
}

impl<TMapping> GeoShape<TMapping>
where
    TMapping: GeoShapeMapping,
{
    /**
    Creates a new `GeoShape` from the given `Geometry`.
    
    This function will consume the provided `Geometry`.
    
    # Examples
    
    ```
    # extern crate elastic_types;
    # extern crate geojson;
    use geojson::{ Geometry, Value };
    
    # use elastic_types::prelude::*;
    # fn main() {
    let point: GeoShape<DefaultGeoShapeMapping> = GeoShape::new(
        Geometry::new(
            Value::Point(vec![ 1.0, 1.0 ])
        )
    );
    # }
    ```
    */
    pub fn new<I>(geo: I) -> GeoShape<TMapping>
    where
        I: Into<Geometry>,
    {
        GeoShape {
            value: geo.into(),
            _m: PhantomData,
        }
    }

    /** Change the mapping of this geo shape. */
    pub fn remap<TNewMapping>(shape: GeoShape<TMapping>) -> GeoShape<TNewMapping>
    where
        TNewMapping: GeoShapeMapping,
    {
        GeoShape::new(shape.value)
    }
}

impl<TMapping> GeoShapeFieldType<TMapping> for GeoShape<TMapping>
where
    TMapping: GeoShapeMapping,
{
}

impl_mapping_type!(Geometry, GeoShape, GeoShapeMapping);

impl<TMapping> Serialize for GeoShape<TMapping>
where
    TMapping: GeoShapeMapping,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.value.serialize(serializer)
    }
}

impl<'de, TMapping> Deserialize<'de> for GeoShape<TMapping>
where
    TMapping: GeoShapeMapping,
{
    fn deserialize<D>(deserializer: D) -> Result<GeoShape<TMapping>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let t = try!(Geometry::deserialize(deserializer));

        Ok(GeoShape::new(t))
    }
}

#[cfg(test)]
mod tests {
    use serde_json;

    use geojson::{Geometry, Value};
    use prelude::*;

    #[derive(Default)]
    struct MyGeoShapeMapping;
    impl GeoShapeMapping for MyGeoShapeMapping {}

    #[test]
    fn can_change_geo_shape_mapping() {
        fn takes_custom_mapping(_: GeoShape<MyGeoShapeMapping>) -> bool {
            true
        }

        let point: GeoShape<DefaultGeoShapeMapping> = GeoShape::new(Geometry::new(Value::Point(vec![1.0, 1.0])));

        assert!(takes_custom_mapping(GeoShape::remap(point)));
    }

    #[test]
    fn serialise_elastic_geo_shape() {
        let shape = GeoShape::<DefaultGeoShapeMapping>::new(Geometry::new(Value::Point(vec![1.0, 1.0])));

        let ser = serde_json::to_string(&shape).unwrap();

        assert_eq!(
            json_str!({
            "coordinates": [ 1.0, 1.0 ],
            "type": "Point"
        }),
            ser
        );
    }

    #[test]
    fn deserialise_elastic_geo_shape() {
        let shape: GeoShape<DefaultGeoShapeMapping> = serde_json::from_str(&json_str!({
            "coordinates": [ 1, 1 ],
            "type": "Point"
        })).unwrap();

        assert_eq!(Geometry::new(Value::Point(vec![1.0, 1.0])), *shape);
    }

}