Skip to content

Commit

Permalink
wip: introduce serde_test token matching
Browse files Browse the repository at this point in the history
Co-authored-by: Carlo Federico Vescovo <cfvescovo@users.noreply.github.com>
  • Loading branch information
LoZack19 and cfvescovo committed Aug 29, 2024
1 parent be6bbea commit f094f01
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ serde = { version = "1.0.209", optional = true }
[dev-dependencies]
serde = "1.0.209"
serde_json = "1.0.127"
serde_test = "1.0.177"
10 changes: 3 additions & 7 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<'a, T: Serialize> Serialize for SerNode<'a, T> {
where
S: serde::Serializer,
{
let mut state = serializer.serialize_struct("SerNode", 2)?;
let mut state = serializer.serialize_struct("Node", 2)?;
state.serialize_field("value", &self.value)?;
state.serialize_field("children", &self.children)?;
state.end()
Expand Down Expand Up @@ -93,7 +93,7 @@ where
type Value = DeserNode<T>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("struct DeserNode")
formatter.write_str("struct Node")
}

fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
Expand Down Expand Up @@ -138,11 +138,7 @@ where
where
D: Deserializer<'de>,
{
deserializer.deserialize_struct(
"DeserNode",
&["value", "children"],
DeserNodeVisitor::new(),
)
deserializer.deserialize_struct("Node", &["value", "children"], DeserNodeVisitor::new())
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(feature = "serde")]

use ego_tree::{tree, Tree};
use serde_test::{assert_tokens, Token};

#[test]
fn test_serde_round_trip() {
Expand All @@ -11,3 +12,24 @@ fn test_serde_round_trip() {
println!("{re_tree}");
assert_eq!(tree, re_tree);
}

#[test]
fn test_internal_serde_repr() {
let tree = tree!("a");

assert_tokens(
&tree,
&[
Token::Struct {
name: "Node",
len: 2,
},
Token::Str("value"),
Token::Str("a"),
Token::Str("children"),
Token::Seq { len: Some(0) },
Token::SeqEnd,
Token::StructEnd,
],
);
}

0 comments on commit f094f01

Please sign in to comment.