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
use {
    super::ValueError,
    crate::result::{Error, Result},
    regex::Regex,
    serde::{Deserialize, Serialize},
    std::fmt,
};

#[derive(Copy, Debug, Clone, Serialize, Deserialize)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

impl Point {
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }

    pub fn from_wkt(v: &str) -> Result<Self> {
        let re = Regex::new(r"POINT\s*\(\s*(-?\d*\.?\d+)\s+(-?\d*\.?\d+)\s*\)").unwrap();

        if let Some(captures) = re.captures(v) {
            let x = captures[1]
                .parse::<f64>()
                .map_err(|_| Error::Value(ValueError::FailedToParsePoint(v.to_owned())))?;
            let y = captures[2]
                .parse::<f64>()
                .map_err(|_| Error::Value(ValueError::FailedToParsePoint(v.to_owned())))?;
            Ok(Self { x, y })
        } else {
            Err(Error::Value(ValueError::FailedToParsePoint(v.to_owned())))
        }
    }

    pub fn calc_distance(&self, other: &Point) -> f64 {
        let dx = self.x - other.x;
        let dy = self.y - other.y;
        f64::sqrt(dx * dx + dy * dy)
    }
}

impl PartialEq for Point {
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }
}

impl Eq for Point {}

impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "POINT({} {})", self.x, self.y)
    }
}