serialize.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm 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 alphavm_utilities::DeserializeExt; 19 20 impl<N: Network> Serialize for TransitionLeaf<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("TransitionLeaf", 4)?; 26 leaf.serialize_field("version", &self.version)?; 27 leaf.serialize_field("index", &self.index)?; 28 leaf.serialize_field("variant", &self.variant)?; 29 leaf.serialize_field("id", &self.id)?; 30 leaf.end() 31 } 32 false => ToBytesSerializer::serialize_with_size_encoding(self, serializer), 33 } 34 } 35 } 36 37 impl<'de, N: Network> Deserialize<'de> for TransitionLeaf<N> { 38 /// Deserializes the leaf from a string or bytes. 39 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 40 match deserializer.is_human_readable() { 41 true => { 42 // Parse the leaf from a string into a value. 43 let mut leaf = serde_json::Value::deserialize(deserializer)?; 44 // Recover the leaf. 45 Ok(Self::from( 46 // Retrieve the version. 47 DeserializeExt::take_from_value::<D>(&mut leaf, "version")?, 48 // Retrieve the index. 49 DeserializeExt::take_from_value::<D>(&mut leaf, "index")?, 50 // Retrieve the variant. 51 DeserializeExt::take_from_value::<D>(&mut leaf, "variant")?, 52 // Retrieve the id. 53 DeserializeExt::take_from_value::<D>(&mut leaf, "id")?, 54 )) 55 } 56 false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "transition leaf"), 57 } 58 } 59 } 60 61 #[cfg(test)] 62 mod tests { 63 use super::*; 64 65 #[test] 66 fn test_serde_json() -> Result<()> { 67 let mut rng = TestRng::default(); 68 69 // Sample the leaf. 70 let expected = test_helpers::sample_leaf(&mut rng); 71 72 // Serialize 73 let expected_string = &expected.to_string(); 74 let candidate_string = serde_json::to_string(&expected)?; 75 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 76 77 // Deserialize 78 assert_eq!(expected, TransitionLeaf::from_str(expected_string)?); 79 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 80 81 Ok(()) 82 } 83 84 #[test] 85 fn test_bincode() -> Result<()> { 86 let mut rng = TestRng::default(); 87 88 // Sample the leaf. 89 let expected = test_helpers::sample_leaf(&mut rng); 90 91 // Serialize 92 let expected_bytes = expected.to_bytes_le()?; 93 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 94 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 95 96 // Deserialize 97 assert_eq!(expected, TransitionLeaf::read_le(&expected_bytes[..])?); 98 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 99 100 Ok(()) 101 } 102 }