starling/tree/
tree_leaf.rs

1use crate::Array;
2#[cfg(feature = "bincode")]
3use bincode::{deserialize, serialize};
4#[cfg(feature = "cbor")]
5use ciborium::de::from_reader;
6#[cfg(feature = "cbor")]
7use ciborium::ser::into_writer;
8#[cfg(feature = "ron")]
9use ron;
10#[cfg(feature = "serde")]
11use serde::{Deserialize, Serialize};
12#[cfg(feature = "json")]
13use serde_json;
14#[cfg(feature = "pickle")]
15use serde_pickle;
16#[cfg(feature = "yaml")]
17use serde_yaml;
18
19#[cfg(feature = "serde")]
20use crate::merkle_bit::BinaryMerkleTreeResult;
21use crate::traits::Leaf;
22#[cfg(feature = "serde")]
23use crate::traits::{Decode, Encode};
24
25/// Represents a leaf of the tree.  Holds a pointer to the location of the underlying `Data` node.
26#[derive(Copy, Clone, Debug, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
28pub struct TreeLeaf<const N: usize> {
29    /// The associated key with this node.
30    key: Array<N>,
31    /// The location of the `Data` node in the tree.
32    data: Array<N>,
33}
34
35impl<const N: usize> Default for TreeLeaf<N> {
36    #[inline]
37    #[cfg(feature = "serde")]
38    fn default() -> Self {
39        Self {
40            key: Array::default(),
41            data: Array::default(),
42        }
43    }
44
45    #[inline]
46    #[cfg(not(any(feature = "serde")))]
47    fn default() -> Self {
48        Self {
49            key: [0; N],
50            data: [0; N],
51        }
52    }
53}
54
55impl<const N: usize> Leaf<N> for TreeLeaf<N> {
56    /// Creates a new `TreeLeaf`
57    #[inline]
58    fn new() -> Self {
59        Self::default()
60    }
61
62    /// Gets the associated key with this node.
63    #[inline]
64    fn get_key(&self) -> &Array<N> {
65        &self.key
66    }
67
68    /// Gets the location of the `Data` node.
69    #[inline]
70    fn get_data(&self) -> &Array<N> {
71        &self.data
72    }
73
74    /// Sets the associated key with this node.
75    #[inline]
76    fn set_key(&mut self, key: Array<N>) {
77        self.key = key;
78    }
79
80    /// Sets the location for the `Data` node.
81    #[inline]
82    fn set_data(&mut self, data: Array<N>) {
83        self.data = data;
84    }
85
86    /// Decomposes the struct into its constituent parts.
87    #[inline]
88    fn decompose(self) -> (Array<N>, Array<N>) {
89        (self.key, self.data)
90    }
91}
92
93#[cfg(feature = "bincode")]
94impl<const N: usize> Encode for TreeLeaf<N> {
95    #[inline]
96    fn encode(&self) -> BinaryMerkleTreeResult<Vec<u8>> {
97        Ok(serialize(self)?)
98    }
99}
100
101#[cfg(feature = "json")]
102impl<const N: usize> Encode for TreeLeaf<N> {
103    #[inline]
104    fn encode(&self) -> BinaryMerkleTreeResult<Vec<u8>> {
105        let encoded = serde_json::to_string(&self)?;
106        Ok(encoded.as_bytes().to_vec())
107    }
108}
109
110#[cfg(feature = "cbor")]
111impl<const N: usize> Encode for TreeLeaf<N> {
112    #[inline]
113    fn encode(&self) -> BinaryMerkleTreeResult<Vec<u8>> {
114        let mut buf = Vec::new();
115        into_writer(&self, &mut buf)?;
116        Ok(buf)
117    }
118}
119
120#[cfg(feature = "yaml")]
121impl<const N: usize> Encode for TreeLeaf<N> {
122    #[inline]
123    fn encode(&self) -> BinaryMerkleTreeResult<Vec<u8>> {
124        Ok(serde_yaml::to_vec(&self)?)
125    }
126}
127
128#[cfg(feature = "pickle")]
129impl<const N: usize> Encode for TreeLeaf<N> {
130    #[inline]
131    fn encode(&self) -> BinaryMerkleTreeResult<Vec<u8>> {
132        Ok(serde_pickle::to_vec(&self, Default::default())?)
133    }
134}
135
136#[cfg(feature = "ron")]
137impl<const N: usize> Encode for TreeLeaf<N> {
138    #[inline]
139    fn encode(&self) -> BinaryMerkleTreeResult<Vec<u8>> {
140        Ok(ron::ser::to_string(&self)?.as_bytes().to_vec())
141    }
142}
143
144#[cfg(feature = "bincode")]
145impl<const N: usize> Decode for TreeLeaf<N> {
146    #[inline]
147    fn decode(buffer: &[u8]) -> BinaryMerkleTreeResult<Self> {
148        Ok(deserialize(buffer)?)
149    }
150}
151
152#[cfg(feature = "json")]
153impl<const N: usize> Decode for TreeLeaf<N> {
154    #[inline]
155    fn decode(buffer: &[u8]) -> BinaryMerkleTreeResult<Self> {
156        let decoded_string = String::from_utf8(buffer.to_vec())?;
157        let decoded = serde_json::from_str(&decoded_string)?;
158        Ok(decoded)
159    }
160}
161
162#[cfg(feature = "cbor")]
163impl<const N: usize> Decode for TreeLeaf<N> {
164    #[inline]
165    fn decode(buffer: &[u8]) -> BinaryMerkleTreeResult<Self> {
166        Ok(from_reader(buffer)?)
167    }
168}
169
170#[cfg(feature = "yaml")]
171impl<const N: usize> Decode for TreeLeaf<N> {
172    #[inline]
173    fn decode(buffer: &[u8]) -> BinaryMerkleTreeResult<Self> {
174        Ok(serde_yaml::from_slice(buffer)?)
175    }
176}
177
178#[cfg(feature = "pickle")]
179impl<const N: usize> Decode for TreeLeaf<N> {
180    #[inline]
181    fn decode(buffer: &[u8]) -> BinaryMerkleTreeResult<Self> {
182        Ok(serde_pickle::from_slice(buffer, Default::default())?)
183    }
184}
185
186#[cfg(feature = "ron")]
187impl<const N: usize> Decode for TreeLeaf<N> {
188    #[inline]
189    fn decode(buffer: &[u8]) -> BinaryMerkleTreeResult<Self> {
190        Ok(ron::de::from_bytes(buffer)?)
191    }
192}