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
use bolt_proto_derive::*;

pub(crate) const MARKER: u8 = 0xB3;
pub(crate) const SIGNATURE: u8 = 0x58;

#[derive(Debug, Clone, PartialEq, Signature, Marker, Serialize, Deserialize)]
pub struct Point2D {
    pub(crate) srid: i32,
    pub(crate) x: f64,
    pub(crate) y: f64,
}

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

    pub fn srid(&self) -> i32 {
        self.srid
    }

    pub fn x(&self) -> f64 {
        self.x
    }

    pub fn y(&self) -> f64 {
        self.y
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryFrom;
    use std::sync::{Arc, Mutex};

    use bytes::Bytes;

    use crate::serialization::*;
    use crate::value::float::MARKER as FLOAT_MARKER;

    use super::*;

    fn get_point() -> Point2D {
        Point2D::new(120, 5_421_394.569_325_1, 1.9287)
    }

    #[test]
    fn get_marker() {
        let point = get_point();
        assert_eq!(point.get_marker().unwrap(), MARKER);
    }

    #[test]
    fn try_into_bytes() {
        let point = get_point();
        assert_eq!(
            point.try_into_bytes().unwrap(),
            Bytes::from_static(&[
                MARKER,
                SIGNATURE,
                0x78,
                FLOAT_MARKER,
                0x41,
                0x54,
                0xAE,
                0x54,
                0xA4,
                0x6F,
                0xD2,
                0x8B,
                FLOAT_MARKER,
                0x3F,
                0xFE,
                0xDB,
                0xF4,
                0x87,
                0xFC,
                0xB9,
                0x24
            ])
        );
    }

    #[test]
    fn try_from_bytes() {
        let point = get_point();
        let point_bytes = &[
            0x78,
            FLOAT_MARKER,
            0x41,
            0x54,
            0xAE,
            0x54,
            0xA4,
            0x6F,
            0xD2,
            0x8B,
            FLOAT_MARKER,
            0x3F,
            0xFE,
            0xDB,
            0xF4,
            0x87,
            0xFC,
            0xB9,
            0x24,
        ];
        assert_eq!(
            Point2D::try_from(Arc::new(Mutex::new(Bytes::from_static(point_bytes)))).unwrap(),
            point
        );
    }
}