Parse a JSON string from graph_traverse into `SubGraph`.
(json_text: &str)
| 43 | |
| 44 | /// Parse a JSON string from graph_traverse into `SubGraph`. |
| 45 | pub(super) fn parse_graph_traverse_json(json_text: &str) -> NodeDbResult<SubGraph> { |
| 46 | let parsed: serde_json::Value = sonic_rs::from_str(json_text) |
| 47 | .map_err(|e| NodeDbError::serialization("json", e.to_string()))?; |
| 48 | |
| 49 | let mut nodes = Vec::new(); |
| 50 | let mut edges = Vec::new(); |
| 51 | |
| 52 | if let Some(n) = parsed.get("nodes").and_then(|v| v.as_array()) { |
| 53 | for item in n { |
| 54 | let id = item |
| 55 | .get("id") |
| 56 | .and_then(|v| v.as_str()) |
| 57 | .unwrap_or("") |
| 58 | .to_string(); |
| 59 | let depth = item.get("depth").and_then(|v| v.as_u64()).unwrap_or(0) as u8; |
| 60 | nodes.push(SubGraphNode { |
| 61 | id: NodeId::from_validated(id), |
| 62 | depth, |
| 63 | properties: HashMap::new(), |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if let Some(e) = parsed.get("edges").and_then(|v| v.as_array()) { |
| 69 | for item in e { |
| 70 | let src = item.get("from").and_then(|v| v.as_str()).unwrap_or(""); |
| 71 | let dst = item.get("to").and_then(|v| v.as_str()).unwrap_or(""); |
| 72 | let label = item.get("label").and_then(|v| v.as_str()).unwrap_or(""); |
| 73 | edges.push(SubGraphEdge { |
| 74 | id: EdgeId::try_first( |
| 75 | NodeId::from_validated(src.to_owned()), |
| 76 | NodeId::from_validated(dst.to_owned()), |
| 77 | label, |
| 78 | ) |
| 79 | .expect("server wire label already validated"), |
| 80 | from: NodeId::from_validated(src.to_owned()), |
| 81 | to: NodeId::from_validated(dst.to_owned()), |
| 82 | label: label.to_string(), |
| 83 | properties: HashMap::new(), |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | Ok(SubGraph { nodes, edges }) |
| 89 | } |
| 90 | |
| 91 | #[cfg(test)] |
| 92 | mod tests { |