orx_tree/common_traits/serde.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
use crate::{
    pinned_storage::PinnedStorage, MemoryPolicy, NodeRef, Traversal, Traverser, Tree, TreeVariant,
};
use core::marker::PhantomData;
use serde::{de::Visitor, ser::SerializeSeq, Deserialize, Serialize};
// Serialize
impl<V, M, P> Serialize for Tree<V, M, P>
where
    V: TreeVariant,
    M: MemoryPolicy,
    P: PinnedStorage,
    P::PinnedVec<V>: Default,
    V::Item: Serialize,
{
    /// Serializes the tree into linearized [`DepthFirstSequence`].
    ///
    /// [`DepthFirstSequence`]: crate::DepthFirstSequence
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_tree::*;
    ///
    /// let tree = BinaryTree::<i32>::empty();
    /// let json = serde_json::to_string(&tree).unwrap();
    /// assert_eq!(json, "[]");
    ///
    /// let tree = DynTree::new(10);
    /// let json = serde_json::to_string(&tree).unwrap();
    /// assert_eq!(json, "[[0,10]]");
    ///
    /// //      0
    /// //     ╱ ╲
    /// //    ╱   ╲
    /// //   1     2
    /// //  ╱     ╱ ╲
    /// // 3     4   5
    /// // |         |
    /// // 6         7
    /// let mut tree = DaryTree::<4, _>::new(0);
    /// let [id1, id2] = tree.root_mut().push_children([1, 2]);
    /// let id3 = tree.node_mut(&id1).push_child(3);
    /// tree.node_mut(&id3).push_child(6);
    /// let [_, id5] = tree.node_mut(&id2).push_children([4, 5]);
    /// tree.node_mut(&id5).push_child(7);
    ///
    /// let json = serde_json::to_string(&tree).unwrap();
    /// assert_eq!(json, "[[0,0],[1,1],[2,3],[3,6],[1,2],[2,4],[2,5],[3,7]]");
    /// ```
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.len()))?;
        if let Some(root) = self.get_root() {
            let mut t = Traversal.dfs().with_depth();
            for x in root.walk_with(&mut t) {
                seq.serialize_element(&x)?;
            }
        }
        seq.end()
    }
}
// Deserialize
/// Tree deserializer using the linear DepthFirstSequence representation of a tree.
struct TreeDeserializer<V, M, P>
where
    V: TreeVariant,
    M: MemoryPolicy,
    P: PinnedStorage,
    P::PinnedVec<V>: Default,
{
    phantom: PhantomData<(V, M, P)>,
}
impl<'de, V, M, P> Visitor<'de> for TreeDeserializer<V, M, P>
where
    V: TreeVariant,
    M: MemoryPolicy,
    P: PinnedStorage,
    P::PinnedVec<V>: Default,
    V::Item: Deserialize<'de>,
{
    type Value = Tree<V, M, P>;
    fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
        formatter.write_str("Expecting a DepthFirstSequence which contains (depth, value) pairs in depth-first order. Following is an example valid sequence of (depth, value) pairs: [[0, 0], [1, 1], [2, 3], [3, 6], [1, 2], [2, 4], [2, 5], [3, 7]].")
    }
    #[allow(clippy::unwrap_in_result)]
    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        let err = |x| Err(serde::de::Error::custom(x));
        use alloc::format;
        let mut tree = Tree::default();
        if let Some(x) = seq.next_element()? {
            let (depth, value): (usize, V::Item) = x;
            if depth != 0 {
                return err(format!("First element of DepthFirstSequence (root of the tree) must have depth 0; however, received a depth of {}. Following is an example valid sequence of (depth, value) pairs: [[0, 0], [1, 1], [2, 3], [3, 6], [1, 2], [2, 4], [2, 5], [3, 7]].", depth));
            }
            tree.push_root(value);
            let mut current_depth = depth;
            let mut dst = tree.root_mut();
            while let Some((depth, value)) = seq.next_element::<(usize, V::Item)>()? {
                match depth > current_depth {
                    true => {
                        if depth > current_depth + 1 {
                            return err(format!("Let d1 and d2 be two consecutive depths in the depth-first sequence. Then, (i) d2=d1+1, (ii) d2=d1 or (iii) d2<d1 are valid cases. However, received the invalid case where d2>d1+1 (d1={}, d2={}). Please see DepthFirstSequenceError documentation for details. Following is an example valid sequence of (depth, value) pairs: [[0, 0], [1, 1], [2, 3], [3, 6], [1, 2], [2, 4], [2, 5], [3, 7]].", current_depth, depth));
                        }
                    }
                    false => {
                        let num_parent_moves = current_depth - depth + 1;
                        for _ in 0..num_parent_moves {
                            dst = dst.into_parent_mut().expect("in bounds");
                        }
                    }
                }
                let position = dst.num_children();
                dst.push_child(value);
                dst = dst.into_child_mut(position).expect("child exists");
                current_depth = depth;
            }
        }
        Ok(tree)
    }
}
impl<'de, V, M, P> Deserialize<'de> for Tree<V, M, P>
where
    V: TreeVariant,
    M: MemoryPolicy,
    P: PinnedStorage,
    P::PinnedVec<V>: Default,
    V::Item: Deserialize<'de>,
{
    /// Deserializes the tree from linearized [`DepthFirstSequence`].
    ///
    /// [`DepthFirstSequence`]: crate::DepthFirstSequence
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_tree::*;
    ///
    /// let json = "[]";
    /// let result: Result<DaryTree<4, i32>, _> = serde_json::from_str(json);
    /// let tree = result.unwrap();
    /// assert!(tree.is_empty());
    ///
    /// let json = "[[0,10]]";
    /// let result: Result<DynTree<i32>, _> = serde_json::from_str(json);
    /// let tree = result.unwrap();
    /// assert_eq!(tree.len(), 1);
    /// assert_eq!(tree.root().data(), &10);
    ///
    /// //      0
    /// //     ╱ ╲
    /// //    ╱   ╲
    /// //   1     2
    /// //  ╱     ╱ ╲
    /// // 3     4   5
    /// // |         |
    /// // 6         7
    /// let json = "[[0, 0], [1, 1], [2, 3], [3, 6], [1, 2], [2, 4], [2, 5], [3, 7]]";
    /// let result: Result<BinaryTree<i32>, _> = serde_json::from_str(json);
    /// let tree = result.unwrap();
    /// let bfs_values: Vec<_> = tree.root().walk::<Bfs>().copied().collect();
    /// assert_eq!(bfs_values, [0, 1, 2, 3, 4, 5, 6, 7]);
    ///
    /// // errors
    ///
    /// // A. First element of DepthFirstSequence (root of the tree) must have depth 0;
    /// // however, received a depth of 1.
    /// let json = "[[1,10]]";
    /// let result: Result<DynTree<i32>, _> = serde_json::from_str(json);
    /// assert!(result.is_err());
    ///
    /// // B. Let d1 and d2 be two consecutive depths in the depth-first sequence.
    /// // Then, (i) d2=d1+1, (ii) d2=d1 or (iii) d2<d1 are valid cases.
    /// // However, received the invalid case where d2>d1+1 (d1=1, d2=3).
    /// let json = "[[0, 0], [1, 1], [3, 6], [1, 2], [2, 4], [2, 5], [3, 7]]";
    /// let result: Result<BinaryTree<i32>, _> = serde_json::from_str(json);
    /// assert!(result.is_err());
    /// ```
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_seq(TreeDeserializer {
            phantom: PhantomData,
        })
    }
}