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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::borrow::Borrow;
use std::marker::PhantomData;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use georust::{Geometry as GeoEnum, ToGeo};
use super::mapping::{GeoPointFieldType, GeoPointMapping};
use super::{Coordinate, GeoPointFormat, Geometry, Point};

/**
An Elasticsearch `geo_point` type with a format.

The [format](format/index.html) is provided as a generic parameter.
This struct wraps up a `geo::Point` struct, which have an `x` and `y` floating point value.

# Examples

Defining a geo point using the default format:

```
# use elastic_types::prelude::*;
let point: GeoPoint<DefaultGeoPointMapping> = GeoPoint::build(1.0, 1.0);
```

Defining a geo point using a named format:

```
# use elastic_types::prelude::*;
let point: GeoPoint<DefaultGeoPointMapping<GeoPointString>> = GeoPoint::build(1.0, 1.0);
```

Accessing the values of a geo point:

```
# use elastic_types::prelude::*;
let point: GeoPoint<DefaultGeoPointMapping> = GeoPoint::build(1.0, 1.0);

//eg: (1.0,1.0)
println!("({},{})",
    point.x(),
    point.y()
);
```

# Links
- [Elasticsearch Doc](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html)
*/
#[derive(Debug, Clone, PartialEq)]
pub struct GeoPoint<TMapping>
where
    TMapping: GeoPointMapping,
{
    value: Point,
    _m: PhantomData<TMapping>,
}

impl<TMapping> GeoPoint<TMapping>
where
    TMapping: GeoPointMapping,
{
    /**
    Creates a new `GeoPoint` from the given coordinate.
    
    This function will consume the provided `Coordinate`.
    
    # Examples
    
    ```
    # extern crate elastic_types;
    # extern crate geo;
    # fn main() {
    use geo::{ Point, Coordinate };
    use elastic_types::prelude::*;
    
    //Create a geo Coordinate struct
    let coord = Coordinate { x: 1.0, y: 1.0 };
    
    //Give it to the GeoPoint struct
    let point: GeoPoint<DefaultGeoPointMapping> = GeoPoint::new(Point(coord));
    # }
    ```
    */
    pub fn new<I>(point: I) -> Self
    where
        I: Into<Point>,
    {
        GeoPoint {
            value: point.into(),
            _m: PhantomData,
        }
    }

    /**
    Creates an `GeoPoint` from the given `x` and `y` primitives:
    
    ```
    # use elastic_types::prelude::*;
    let point: GeoPoint<DefaultGeoPointMapping> = GeoPoint::build(1.0, 1.0);
    ```
    */
    pub fn build(x: f64, y: f64) -> Self {
        GeoPoint::new(Point::new(x, y))
    }

    /**
    Change the format/mapping of this geo point.
    
    # Examples
    
    ```
    # use elastic_types::prelude::*;
    //Get a point formatted as a string
    let point: GeoPoint<DefaultGeoPointMapping<GeoPointString>> = GeoPoint::build(1.0, 1.0);
    
    //Change the format to an object
    let otherpoint: GeoPoint<DefaultGeoPointMapping<GeoPointObject>> = GeoPoint::remap(point);
    ```
    */
    pub fn remap<TNewMapping>(point: GeoPoint<TMapping>) -> GeoPoint<TNewMapping>
    where
        TNewMapping: GeoPointMapping,
    {
        GeoPoint::new(point.value)
    }
}

impl<TMapping> GeoPointFieldType<TMapping> for GeoPoint<TMapping>
where
    TMapping: GeoPointMapping,
{
}

impl_mapping_type!(Point, GeoPoint, GeoPointMapping);

impl<TMapping> From<Coordinate> for GeoPoint<TMapping>
where
    TMapping: GeoPointMapping,
{
    fn from(point: Coordinate) -> GeoPoint<TMapping> {
        GeoPoint::new(Point::new(point.x, point.y))
    }
}

impl<TMapping> ToGeo<f64> for GeoPoint<TMapping>
where
    TMapping: GeoPointMapping,
{
    fn to_geo(&self) -> Geometry {
        GeoEnum::Point(self.value.clone())
    }
}

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

impl<'de, TMapping> Deserialize<'de> for GeoPoint<TMapping>
where
    TMapping: GeoPointMapping,
{
    fn deserialize<D>(deserializer: D) -> Result<GeoPoint<TMapping>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let point = TMapping::Format::parse(deserializer)?;

        Ok(point.into())
    }
}

#[cfg(test)]
mod tests {
    use georust::{Coordinate, Geometry, Point, ToGeo};

    use prelude::*;

    #[test]
    fn can_change_point_mapping() {
        fn takes_custom_mapping(_: GeoPoint<DefaultGeoPointMapping<GeoPointObject>>) -> bool {
            true
        }

        let point: GeoPoint<DefaultGeoPointMapping<GeoPointString>> = GeoPoint::new(Point(Coordinate { x: 1.0, y: 1.0 }));

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

    #[test]
    fn can_build_point_from_geo() {
        let coord = Coordinate { x: 1.0, y: 1.0 };

        let point = GeoPoint::<DefaultGeoPointMapping<DefaultGeoPointFormat>>::new(Point(coord.clone()));

        assert_eq!((coord.x, coord.y), (point.x(), point.y()));
    }

    #[test]
    fn can_convert_point_to_geo() {
        let point = GeoPoint::<DefaultGeoPointMapping<DefaultGeoPointFormat>>::new(Point(Coordinate { x: 1.0, y: 1.0 }));
        let geo = point.to_geo();

        match geo {
            Geometry::Point(point) => assert_eq!((1.0, 1.0), (point.x(), point.y())),
            _ => panic!("expected point"),
        }
    }
}