1use crate::error::GraphError;
6use crate::query::{GraphNode, GraphRelationship, GraphResult};
7use serde::de::DeserializeOwned;
8
9pub struct ResultMapper;
11
12impl ResultMapper {
13 pub fn map_to<T: DeserializeOwned>(results: &[GraphResult]) -> Result<Vec<T>, GraphError> {
15 let values: Vec<serde_json::Value> = results.iter().map(Self::result_to_json).collect();
16 serde_json::from_value(serde_json::Value::Array(values))
17 .map_err(|e| GraphError::MappingError(format!("deserialization failed: {}", e)))
18 }
19
20 fn result_to_json(result: &GraphResult) -> serde_json::Value {
25 match result {
26 GraphResult::Node { node } => node.properties.clone(),
27 GraphResult::Relationship { relationship } => relationship.properties.clone(),
28 GraphResult::Path { path } => {
29 serde_json::to_value(path).unwrap_or(serde_json::Value::Null)
30 }
31 GraphResult::Scalar { value } => value.clone(),
32 }
33 }
34}
35
36pub struct NodeMapper;
38
39impl NodeMapper {
40 pub fn extract_nodes(results: &[GraphResult]) -> Vec<&GraphNode> {
42 results.iter().filter_map(|r| r.as_node()).collect()
43 }
44
45 pub fn map_node<T: DeserializeOwned>(node: &GraphNode) -> Result<T, GraphError> {
47 serde_json::from_value(node.properties.clone()).map_err(|e| {
48 GraphError::MappingError(format!(
49 "node mapping failed: {} (missing field or type mismatch)",
50 e
51 ))
52 })
53 }
54}
55
56pub struct RelationMapper;
58
59impl RelationMapper {
60 pub fn extract_relationships(results: &[GraphResult]) -> Vec<&GraphRelationship> {
62 results.iter().filter_map(|r| r.as_relationship()).collect()
63 }
64
65 pub fn map_relationship<T: DeserializeOwned>(rel: &GraphRelationship) -> Result<T, GraphError> {
67 serde_json::from_value(rel.properties.clone()).map_err(|e| {
68 GraphError::MappingError(format!(
69 "relationship mapping failed: {} (missing field or type mismatch)",
70 e
71 ))
72 })
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use serde::Deserialize;
80
81 #[derive(Debug, Deserialize)]
82 struct Person {
83 name: String,
84 age: i64,
85 }
86
87 #[test]
88 fn test_node_mapping() {
89 let node = GraphNode {
90 id: "1".into(),
91 labels: vec!["Person".into()],
92 properties: serde_json::json!({"name": "Alice", "age": 30}),
93 };
94 let result = GraphResult::Node { node };
95 let mapped: Vec<Person> = ResultMapper::map_to(&[result]).unwrap();
96 assert_eq!(mapped.len(), 1);
97 assert_eq!(mapped[0].name, "Alice");
98 assert_eq!(mapped[0].age, 30);
99 }
100
101 #[test]
102 fn test_mapping_error_on_missing_field() {
103 let node = GraphNode {
104 id: "1".into(),
105 labels: vec!["Person".into()],
106 properties: serde_json::json!({"name": "Alice"}),
107 };
108 let result = GraphResult::Node { node };
109 let mapped: Result<Vec<Person>, _> = ResultMapper::map_to(&[result]);
110 assert!(mapped.is_err());
111 let err = mapped.unwrap_err();
112 assert!(err.to_string().contains("mapping"));
113 }
114}