elasticsearch_dsl/search/params/
coordinate.rs1use serde::Serialize;
2use std::{fmt::Display, str::FromStr};
3
4#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
6pub struct Coordinate {
7 x: f32,
8 y: f32,
9}
10
11impl Coordinate {
12 pub fn new(x: f32, y: f32) -> Self {
14 Self { x, y }
15 }
16}
17
18impl Display for Coordinate {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 format!("[{}, {}]", &self.x, &self.y).fmt(f)
21 }
22}
23
24impl Serialize for Coordinate {
25 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
26 where
27 S: serde::Serializer,
28 {
29 [self.x, self.y].serialize(serializer)
30 }
31}
32
33impl From<[f32; 2]> for Coordinate {
34 fn from(value: [f32; 2]) -> Self {
35 Self {
36 x: value[0],
37 y: value[1],
38 }
39 }
40}
41
42impl From<(f32, f32)> for Coordinate {
43 fn from(value: (f32, f32)) -> Self {
44 Self {
45 x: value.0,
46 y: value.1,
47 }
48 }
49}
50
51impl FromStr for Coordinate {
52 type Err = String;
53
54 fn from_str(s: &str) -> Result<Self, Self::Err> {
55 let mut values = s.split(',');
56
57 let x = values.next().and_then(|x| x.trim().parse().ok());
58 let y = values.next().and_then(|x| x.trim().parse().ok());
59
60 match (x, y, values.next()) {
61 (Some(x), Some(y), None) => Ok(Self { x, y }),
62 _ => Err(format!("Couldn't parse '{s}' as coordinate")),
63 }
64 }
65}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use crate::util::*;
71
72 #[test]
73 fn serialization() {
74 assert_serialize(Coordinate::new(1.1, 2.2), json!([1.1, 2.2]));
75 assert_serialize(Coordinate::from([1.1, 2.2]), json!([1.1, 2.2]));
76 assert_serialize(Coordinate::from((1.1, 2.2)), json!([1.1, 2.2]));
77 }
78
79 #[test]
80 fn from_str() {
81 assert_eq!(
82 Coordinate::from_str("1.1, 2.2").unwrap(),
83 Coordinate::new(1.1, 2.2)
84 );
85 assert_eq!(
86 Coordinate::from_str("1,2").unwrap(),
87 Coordinate::new(1., 2.)
88 );
89
90 assert!(Coordinate::from_str("1.1").is_err());
91 assert!(Coordinate::from_str("1.1,2.2,3").is_err());
92 assert!(Coordinate::from_str("abc").is_err());
93 }
94}