Skip to content
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

Implement serde traits for ego-tree #34

Merged
merged 17 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ authors = [
license = "ISC"
repository = "https://github.com/rust-scraper/ego-tree"
readme = "README.md"

[features]
serde = ["dep:serde"]

[dependencies]
serde = { version = "1.0.209" , optional = true }
LoZack19 marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
serde = "1.0.209"
serde_json = "1.0.127"
cfvescovo marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
use std::fmt::{self, Debug, Display, Formatter};
use std::num::NonZeroUsize;

#[cfg(feature = "serde")]
/// Implement serde traits for Tree
pub mod serde;

/// Vec-backed ID-tree.
///
/// Always contains at least a root node.
Expand Down
82 changes: 82 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::num::NonZeroUsize;

use serde::{Deserialize, Serialize};

use crate::{Node, NodeId, Tree};

impl<T: Serialize> Serialize for Tree<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.vec.serialize(serializer)
}
}

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 vec = Vec::deserialize(deserializer)?;
Ok(Tree { vec })
}
}

impl<T: Serialize> Serialize for Node<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
(
&self.parent,
&self.prev_sibling,
&self.next_sibling,
LoZack19 marked this conversation as resolved.
Show resolved Hide resolved
&self.children,
&self.value,
)
.serialize(serializer)
}
}

impl<'de, T: Deserialize<'de>> Deserialize<'de> for Node<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let (parent, prev_sibling, next_sibling, children, value) =
<(
Option<NodeId>,
Option<NodeId>,
Option<NodeId>,
Option<(NodeId, NodeId)>,
T,
)>::deserialize(deserializer)?;
Ok(Node {
parent,
prev_sibling,
next_sibling,
children,
value,
})
}
}

impl Serialize for NodeId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.serialize(serializer)
}
}

impl<'de> Deserialize<'de> for NodeId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let index = <NonZeroUsize>::deserialize(deserializer)?;
Ok(NodeId(index))
}
}
16 changes: 16 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![cfg(feature = "serde")]
#[macro_use]
extern crate ego_tree;
adamreichold marked this conversation as resolved.
Show resolved Hide resolved
use std::assert_eq;
LoZack19 marked this conversation as resolved.
Show resolved Hide resolved

use ego_tree::Tree;

#[test]
fn test_serialize() {
let tree = tree!("r" => {"a", "b" => { "d", "e" }, "c"});

let serialized = serde_json::to_string(&tree).unwrap();
let deserialized: Tree<&str> = serde_json::from_str(&serialized).unwrap();
LoZack19 marked this conversation as resolved.
Show resolved Hide resolved

assert_eq!(tree, deserialized);
}
Loading