neo4rs/types/
point.rs

1use crate::types::{BoltFloat, BoltInteger};
2use neo4rs_macros::BoltStruct;
3
4#[derive(Debug, PartialEq, Clone, BoltStruct)]
5#[signature(0xB3, 0x58)]
6pub struct BoltPoint2D {
7    pub sr_id: BoltInteger,
8    pub x: BoltFloat,
9    pub y: BoltFloat,
10}
11
12#[derive(Debug, PartialEq, Clone, BoltStruct)]
13#[signature(0xB4, 0x59)]
14pub struct BoltPoint3D {
15    pub sr_id: BoltInteger,
16    pub x: BoltFloat,
17    pub y: BoltFloat,
18    pub z: BoltFloat,
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use crate::{types::BoltWireFormat, version::Version};
25    use bytes::Bytes;
26
27    #[test]
28    fn should_serialize_2d_point() {
29        let sr_id = BoltInteger::new(42);
30        let x = BoltFloat::new(1.0);
31        let y = BoltFloat::new(2.0);
32
33        let point = BoltPoint2D { sr_id, x, y };
34
35        let bytes: Bytes = point.into_bytes(Version::V4_1).unwrap();
36
37        assert_eq!(
38            &bytes[..],
39            Bytes::from_static(&[
40                0xB3, 0x58, 0x2A, 0xC1, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x40,
41                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42            ])
43        );
44    }
45
46    #[test]
47    fn should_deserialize_2d_point() {
48        let mut input = Bytes::from_static(&[
49            0xB3, 0x58, 0x2A, 0xC1, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x40,
50            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51        ]);
52
53        let point: BoltPoint2D = BoltPoint2D::parse(Version::V4_1, &mut input).unwrap();
54
55        assert_eq!(point.sr_id, BoltInteger::new(42));
56        assert_eq!(point.x, BoltFloat::new(1.0));
57        assert_eq!(point.y, BoltFloat::new(2.0));
58    }
59
60    #[test]
61    fn should_serialize_3d_point() {
62        let sr_id = BoltInteger::new(42);
63        let x = BoltFloat::new(1.0);
64        let y = BoltFloat::new(2.0);
65        let z = BoltFloat::new(3.0);
66
67        let point = BoltPoint3D { sr_id, x, y, z };
68
69        let bytes: Bytes = point.into_bytes(Version::V4_1).unwrap();
70
71        assert_eq!(
72            &bytes[..],
73            Bytes::from_static(&[
74                0xB4, 0x59, 0x2A, 0xC1, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x40,
75                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00,
76                0x00, 0x00,
77            ])
78        );
79    }
80
81    #[test]
82    fn should_deserialize_3d_point() {
83        let mut input = Bytes::from_static(&[
84            0xB4, 0x59, 0x2A, 0xC1, 0x3F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x40,
85            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00,
86            0x00, 0x00,
87        ]);
88
89        let point: BoltPoint3D = BoltPoint3D::parse(Version::V4_1, &mut input).unwrap();
90
91        assert_eq!(point.sr_id, BoltInteger::new(42));
92        assert_eq!(point.x, BoltFloat::new(1.0));
93        assert_eq!(point.y, BoltFloat::new(2.0));
94        assert_eq!(point.z, BoltFloat::new(3.0));
95    }
96}