From cbe8f50ed1bd2ae3c3b858b2cd039a2fa4382c31 Mon Sep 17 00:00:00 2001 From: andres Date: Tue, 22 Oct 2024 11:11:34 -0500 Subject: [PATCH] feat: Implements serialize for NodeVec --- Cargo.toml | 3 ++- src/utils/node_set.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 973df5f..f7c224d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "orbweaver" -version = "0.15.0" +version = "0.16.0" edition = "2021" authors = ["ixpantia ", "Andrés F. Quintero "] description = "Crate designed for effortless construction and analysis of graph data structures." @@ -11,6 +11,7 @@ exclude = ["assets/"] [dev-dependencies] criterion = "0.5" +serde_json = "1.0.132" ureq = "2.9.7" [dependencies] diff --git a/src/utils/node_set.rs b/src/utils/node_set.rs index 6734401..dcd26a1 100644 --- a/src/utils/node_set.rs +++ b/src/utils/node_set.rs @@ -7,6 +7,16 @@ pub struct NodeVec { pub(crate) arena: Rc<[u8]>, } +#[cfg(feature = "serde")] +impl serde::Serialize for NodeVec { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.values.serialize(serializer) + } +} + impl PartialEq for NodeVec { fn eq(&self, other: &Self) -> bool { self.values.eq(&other.values) @@ -110,3 +120,21 @@ impl<'a> Iterator for NodeVecIter<'a> { self.node_set.values.get(i).copied() } } + +#[cfg(test)] +mod tests { + use crate::directed::builder::DirectedGraphBuilder; + + #[test] + fn test_serialize_nodevec() { + let mut builder = DirectedGraphBuilder::new(); + builder.add_path(["0", "1", "2", "3", "4"]); + builder.add_path(["0", "4"]); + let graph = builder.build_acyclic().unwrap(); + + let paths = graph.find_all_paths("0", "4").unwrap(); + let result = serde_json::to_string(&paths).expect("Unable to serialize Vec"); + + assert_eq!(result, r#"[["0","1","2","3","4"],["0","4"]]"#); + } +}