elasticsearch_dsl/search/params/
geo_location.rs

1use serde::Serialize;
2
3/// Represents a point in two dimensional space
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub struct GeoLocation {
6    latitude: f32,
7    longitude: f32,
8}
9
10impl GeoLocation {
11    /// Creates an instance of [GeoLocation]
12    pub fn new(latitude: f32, longitude: f32) -> Self {
13        Self {
14            latitude,
15            longitude,
16        }
17    }
18}
19
20impl Serialize for GeoLocation {
21    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
22    where
23        S: serde::Serializer,
24    {
25        [self.longitude, self.latitude].serialize(serializer)
26    }
27}
28
29impl From<[f32; 2]> for GeoLocation {
30    fn from(value: [f32; 2]) -> Self {
31        Self {
32            latitude: value[1],
33            longitude: value[0],
34        }
35    }
36}
37
38impl From<(f32, f32)> for GeoLocation {
39    fn from(value: (f32, f32)) -> Self {
40        Self {
41            latitude: value.1,
42            longitude: value.0,
43        }
44    }
45}
46
47impl IntoIterator for GeoLocation {
48    type Item = Self;
49
50    type IntoIter = std::option::IntoIter<Self::Item>;
51
52    fn into_iter(self) -> Self::IntoIter {
53        Some(self).into_iter()
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60    use crate::util::*;
61
62    #[test]
63    fn serialization() {
64        assert_serialize(GeoLocation::new(1.1, 2.2), json!([2.2, 1.1]));
65        assert_serialize(GeoLocation::from([2.2, 1.1]), json!([2.2, 1.1]));
66        assert_serialize(GeoLocation::from((2.2, 1.1)), json!([2.2, 1.1]));
67    }
68}