Skip to content

Commit c2b00c8

Browse files
authored
Fix extracting point properties from BoltType (#211)
* Fix extracting point properties from BoltType * Remove unused import
1 parent a202b3f commit c2b00c8

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

lib/src/types/serde/point.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,33 @@ impl<'de, P: FromBuilder, E: Error> Visitor<'de> for BoltPointVisitor<P, E> {
140140
write!(formatter, "struct {}", std::any::type_name::<P>())
141141
}
142142

143+
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
144+
where
145+
A: SeqAccess<'de>,
146+
{
147+
let mut point = BoltPointBuilder::default();
148+
149+
point.sr_id(|| {
150+
seq.next_element::<BoltInteger>().map_or_else(Err, |item| {
151+
item.ok_or_else(|| Error::missing_field("sr_id"))
152+
})
153+
})?;
154+
point.x(|| {
155+
seq.next_element::<BoltFloat>()
156+
.map_or_else(Err, |item| item.ok_or_else(|| Error::missing_field("x")))
157+
})?;
158+
point.y(|| {
159+
seq.next_element::<BoltFloat>()
160+
.map_or_else(Err, |item| item.ok_or_else(|| Error::missing_field("y")))
161+
})?;
162+
if let Some(z) = seq.next_element::<BoltFloat>().transpose() {
163+
point.z(|| z)?;
164+
}
165+
166+
let point = point.build()?;
167+
Ok(point)
168+
}
169+
143170
fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
144171
where
145172
A: ::serde::de::MapAccess<'de>,
@@ -281,8 +308,9 @@ impl<'de, E: Error, I: Iterator<Item = Result<Field, &'static str>>> SeqAccess<'
281308
where
282309
T: DeserializeSeed<'de>,
283310
{
284-
self.next_key::<Field>()?;
285-
self.next_value_seed(seed).map(Some)
311+
self.next_key::<Field>()?
312+
.map(|_| self.next_value_seed(seed))
313+
.transpose()
286314
}
287315
}
288316

lib/src/types/serde/typ.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
element::ElementDataDeserializer,
66
node::BoltNodeVisitor,
77
path::BoltPathVisitor,
8-
point::{self, BoltPointDeserializer, BoltPointVisitor},
8+
point::{BoltPointDeserializer, BoltPointVisitor},
99
rel::BoltRelationVisitor,
1010
urel::BoltUnboundedRelationVisitor,
1111
BoltKind,
@@ -231,13 +231,10 @@ impl<'de> Visitor<'de> for BoltTypeVisitor {
231231
.tuple_variant(1, BoltUnboundedRelationVisitor)
232232
.map(BoltType::UnboundedRelation),
233233
BoltKind::Point2D => variant
234-
.struct_variant(
235-
&point::Field::NAMES[..3],
236-
BoltPointVisitor::_2d::<A::Error>(),
237-
)
234+
.tuple_variant(3, BoltPointVisitor::_2d::<A::Error>())
238235
.map(BoltType::Point2D),
239236
BoltKind::Point3D => variant
240-
.struct_variant(point::Field::NAMES, BoltPointVisitor::_3d::<A::Error>())
237+
.tuple_variant(4, BoltPointVisitor::_3d::<A::Error>())
241238
.map(BoltType::Point3D),
242239
BoltKind::Bytes => variant.tuple_variant(1, self),
243240
BoltKind::Path => variant

lib/tests/node_property_parsing.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chrono::{DateTime, FixedOffset};
2-
use neo4rs::{query, Node};
2+
use neo4rs::{query, Node, Point2D, Point3D};
33

44
mod container;
55

@@ -9,15 +9,49 @@ async fn node_property_parsing() {
99
let graph = neo4j.graph();
1010

1111
graph
12-
.run(query("CREATE (:A {p1:DATETIME('2024-12-31T08:10:35')})"))
12+
.run(query(
13+
"CREATE
14+
(:Datetime {p1:DATETIME('2024-12-31T08:10:35')}),
15+
(:Point2D {a:Point ({x:2,y:3})}),
16+
(:Point3D {a:Point ({x:3,y:4,z:5})})
17+
",
18+
))
1319
.await
1420
.unwrap();
1521

16-
let mut result = graph.execute(query("MATCH (p:A) RETURN p")).await.unwrap();
22+
let mut result = graph
23+
.execute(query("MATCH (p:DateTime) RETURN p"))
24+
.await
25+
.unwrap();
1726

1827
while let Ok(Some(row)) = result.next().await {
1928
let node: Node = row.get("p").unwrap();
2029
let p1 = node.get::<DateTime<FixedOffset>>("p1").unwrap();
2130
assert_eq!(p1.timestamp(), 1735632635);
2231
}
32+
33+
let mut result = graph
34+
.execute(query("MATCH (p:Point2D) RETURN p"))
35+
.await
36+
.unwrap();
37+
38+
while let Ok(Some(row)) = result.next().await {
39+
let node: Node = row.get("p").unwrap();
40+
let p1 = node.get::<Point2D>("a").unwrap();
41+
assert_eq!(p1.x(), 2.0);
42+
assert_eq!(p1.y(), 3.0);
43+
}
44+
45+
let mut result = graph
46+
.execute(query("MATCH (p:Point3D) RETURN p"))
47+
.await
48+
.unwrap();
49+
50+
while let Ok(Some(row)) = result.next().await {
51+
let node: Node = row.get("p").unwrap();
52+
let p1 = node.get::<Point3D>("a").unwrap();
53+
assert_eq!(p1.x(), 3.0);
54+
assert_eq!(p1.y(), 4.0);
55+
assert_eq!(p1.z(), 5.0);
56+
}
2357
}

0 commit comments

Comments
 (0)