serialize.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the deltavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 use deltavm_utilities::DeserializeExt; 19 20 impl<N: Network> Serialize for HeaderLeaf<N> { 21 /// Serializes the leaf into string or bytes. 22 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 23 match serializer.is_human_readable() { 24 true => { 25 let mut leaf = serializer.serialize_struct("HeaderLeaf", 2)?; 26 leaf.serialize_field("index", &self.index)?; 27 leaf.serialize_field("id", &self.id)?; 28 leaf.end() 29 } 30 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 31 } 32 } 33 } 34 35 impl<'de, N: Network> Deserialize<'de> for HeaderLeaf<N> { 36 /// Deserializes the leaf from a string or bytes. 37 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 38 match deserializer.is_human_readable() { 39 true => { 40 // Parse the leaf from a string into a value. 41 let mut leaf = serde_json::Value::deserialize(deserializer)?; 42 // Recover the leaf. 43 Ok(Self::new( 44 // Retrieve the index. 45 DeserializeExt::take_from_value::<D>(&mut leaf, "index")?, 46 // Retrieve the id. 47 DeserializeExt::take_from_value::<D>(&mut leaf, "id")?, 48 )) 49 } 50 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "header leaf"), 51 } 52 } 53 } 54 55 #[cfg(test)] 56 mod tests { 57 use super::*; 58 59 #[test] 60 fn test_serde_json() -> Result<()> { 61 let mut rng = TestRng::default(); 62 63 // Sample the leaf. 64 let expected = test_helpers::sample_leaf(&mut rng); 65 66 // Serialize 67 let expected_string = &expected.to_string(); 68 let candidate_string = serde_json::to_string(&expected)?; 69 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 70 71 // Deserialize 72 assert_eq!(expected, HeaderLeaf::from_str(expected_string)?); 73 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 74 75 Ok(()) 76 } 77 78 #[test] 79 fn test_bincode() -> Result<()> { 80 let mut rng = TestRng::default(); 81 82 // Sample the leaf. 83 let expected = test_helpers::sample_leaf(&mut rng); 84 85 // Serialize 86 let expected_bytes = expected.to_bytes_le()?; 87 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 88 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 89 90 // Deserialize 91 assert_eq!(expected, HeaderLeaf::read_le(&expected_bytes[..])?); 92 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 93 94 Ok(()) 95 } 96 }