-
Notifications
You must be signed in to change notification settings - Fork 22
Implement serde traits for ego-tree #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
05edbe3
implement serde traits for ego-tree
LoZack19 2f7167b
fix clippy warnings for serde feature
LoZack19 ea8c556
Update Cargo.toml
LoZack19 82514a5
updating to rust 2021: remove macro_use and extern crate from all tests
LoZack19 7fb9415
remove useless line beacause already in prelude
LoZack19 b5c1029
implement serialize through intermediate representation
LoZack19 9f3d03d
implement deserialize through intermediate representation
LoZack19 143afa6
correct to_* into into_* for semantic correctness
LoZack19 1ff4288
improve idiomacity of serialization and deserialization for structs
LoZack19 b85b188
into_tree_node now takes parent as NodeMut
LoZack19 be6bbea
add documentation for serde module
LoZack19 f094f01
wip: introduce serde_test token matching
LoZack19 08ae4d9
Ignore JetBrains' .idea folder
cfvescovo e975c79
Fix tests for serde feature
cfvescovo d082fec
Update src/serde.rs
cfvescovo 644828b
move serde module comments to serde.rs
LoZack19 2520ba0
remove unused return value in into_tree_node
LoZack19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
target | ||
Cargo.lock | ||
.vscode | ||
.vscode | ||
.idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
//! Implement `serde::Serialize` and `serde::Deserialize` traits for Tree | ||
//! | ||
//! # Warning | ||
//! Serialize and Deserialize implementations are recursive. They require an amount of stack memory | ||
//! proportional to the depth of the tree. | ||
|
||
use std::{fmt, marker::PhantomData}; | ||
|
||
use serde::{ | ||
de::{self, MapAccess, Visitor}, | ||
ser::{Serialize, SerializeStruct}, | ||
Deserialize, Deserializer, | ||
}; | ||
|
||
use crate::{NodeMut, NodeRef, Tree}; | ||
|
||
#[derive(Debug)] | ||
struct SerNode<'a, T> { | ||
value: &'a T, | ||
children: Vec<SerNode<'a, T>>, | ||
} | ||
|
||
impl<'a, T> From<NodeRef<'a, T>> for SerNode<'a, T> { | ||
fn from(node: NodeRef<'a, T>) -> Self { | ||
let value = node.value(); | ||
let children = node.children().map(SerNode::from).collect(); | ||
Self { value, children } | ||
} | ||
} | ||
|
||
impl<'a, T: Serialize> Serialize for SerNode<'a, T> { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
let mut state = serializer.serialize_struct("Node", 2)?; | ||
state.serialize_field("value", &self.value)?; | ||
state.serialize_field("children", &self.children)?; | ||
state.end() | ||
} | ||
} | ||
|
||
impl<T: Serialize> Serialize for Tree<T> { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
SerNode::from(self.root()).serialize(serializer) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct DeserNode<T> { | ||
value: T, | ||
children: Vec<DeserNode<T>>, | ||
} | ||
|
||
impl<T> DeserNode<T> { | ||
fn into_tree_node(self, parent: &mut NodeMut<T>) { | ||
let mut node = parent.append(self.value); | ||
|
||
for child in self.children { | ||
child.into_tree_node(&mut node); | ||
} | ||
} | ||
} | ||
|
||
impl<T> From<DeserNode<T>> for Tree<T> { | ||
fn from(root: DeserNode<T>) -> Self { | ||
let mut tree: Tree<T> = Tree::new(root.value); | ||
let mut tree_root = tree.root_mut(); | ||
|
||
for child in root.children { | ||
child.into_tree_node(&mut tree_root); | ||
} | ||
|
||
tree | ||
} | ||
} | ||
|
||
struct DeserNodeVisitor<T> { | ||
marker: PhantomData<fn() -> DeserNode<T>>, | ||
} | ||
|
||
impl<T> DeserNodeVisitor<T> { | ||
fn new() -> Self { | ||
DeserNodeVisitor { | ||
marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<'de, T> Visitor<'de> for DeserNodeVisitor<T> | ||
where | ||
T: Deserialize<'de>, | ||
{ | ||
type Value = DeserNode<T>; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("struct Node") | ||
} | ||
|
||
fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> | ||
where | ||
M: MapAccess<'de>, | ||
{ | ||
let mut value = None; | ||
let mut children = None; | ||
|
||
while let Some(key) = map.next_key()? { | ||
match key { | ||
"value" => { | ||
if value.is_some() { | ||
return Err(de::Error::duplicate_field("value")); | ||
} | ||
value = Some(map.next_value()?); | ||
} | ||
"children" => { | ||
if children.is_some() { | ||
return Err(de::Error::duplicate_field("children")); | ||
} | ||
children = Some(map.next_value()?); | ||
} | ||
_ => { | ||
return Err(de::Error::unknown_field(key, &["value", "children"])); | ||
} | ||
} | ||
} | ||
|
||
let value = value.ok_or_else(|| de::Error::missing_field("value"))?; | ||
let children = children.ok_or_else(|| de::Error::missing_field("children"))?; | ||
|
||
Ok(DeserNode { value, children }) | ||
} | ||
} | ||
|
||
impl<'de, T> Deserialize<'de> for DeserNode<T> | ||
where | ||
T: Deserialize<'de>, | ||
{ | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_struct("Node", &["value", "children"], DeserNodeVisitor::new()) | ||
} | ||
} | ||
|
||
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Tree<T> { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let deser = DeserNode::<T>::deserialize(deserializer)?; | ||
Ok(deser.into()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#[macro_use] | ||
extern crate ego_tree; | ||
use ego_tree::tree; | ||
|
||
#[test] | ||
fn into_iter() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
#[macro_use] | ||
extern crate ego_tree; | ||
|
||
use ego_tree::Tree; | ||
use ego_tree::{tree, Tree}; | ||
|
||
#[test] | ||
fn root() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
#[macro_use] | ||
extern crate ego_tree; | ||
|
||
use ego_tree::NodeRef; | ||
use ego_tree::{tree, NodeRef}; | ||
|
||
#[test] | ||
fn value() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#[macro_use] | ||
extern crate ego_tree; | ||
use ego_tree::tree; | ||
|
||
#[test] | ||
fn value() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#![cfg(feature = "serde")] | ||
|
||
use ego_tree::tree; | ||
use serde_test::{assert_tokens, Token}; | ||
|
||
#[test] | ||
fn test_internal_serde_repr_trivial() { | ||
let tree = tree!("a"); | ||
|
||
assert_tokens( | ||
&tree, | ||
&[ | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("a"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(0) }, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
], | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_internal_serde_repr() { | ||
let tree = tree!("a" => {"b", "c" => {"d", "e"}, "f"}); | ||
|
||
assert_tokens( | ||
&tree, | ||
&[ | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("a"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(3) }, | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("b"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(0) }, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("c"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(2) }, | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("d"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(0) }, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("e"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(0) }, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
Token::Struct { | ||
name: "Node", | ||
len: 2, | ||
}, | ||
Token::BorrowedStr("value"), | ||
Token::BorrowedStr("f"), | ||
Token::BorrowedStr("children"), | ||
Token::Seq { len: Some(0) }, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
Token::SeqEnd, | ||
Token::StructEnd, | ||
], | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.