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

pub(crate) const MARKER: u8 = 0xB4;
pub(crate) const SIGNATURE: u8 = 0x59;

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

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

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

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

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

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

#[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() -> Point3D {
        Point3D::new(7, 1234.56543, 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,
                0x07,
                FLOAT_MARKER,
                0x40,
                0x93,
                0x4A,
                0x43,
                0x00,
                0x14,
                0xF8,
                0xB6,
                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 = &[
            0x07,
            FLOAT_MARKER,
            0x40,
            0x93,
            0x4A,
            0x43,
            0x00,
            0x14,
            0xF8,
            0xB6,
            FLOAT_MARKER,
            0x41,
            0x54,
            0xAE,
            0x54,
            0xA4,
            0x6F,
            0xD2,
            0x8B,
            FLOAT_MARKER,
            0x3F,
            0xFE,
            0xDB,
            0xF4,
            0x87,
            0xFC,
            0xB9,
            0x24,
        ];
        assert_eq!(
            Point3D::try_from(Arc::new(Mutex::new(Bytes::from_static(point_bytes)))).unwrap(),
            point
        );
    }
}