/ console / program / src / data_types / register_type / serialize.rs
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  impl<N: Network> Serialize for RegisterType<N> {
 19      /// Serializes the register type into string or bytes.
 20      fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
 21          match serializer.is_human_readable() {
 22              true => serializer.collect_str(self),
 23              false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
 24          }
 25      }
 26  }
 27  
 28  impl<'de, N: Network> Deserialize<'de> for RegisterType<N> {
 29      /// Deserializes the register type from a string or bytes.
 30      fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
 31          match deserializer.is_human_readable() {
 32              true => FromStr::from_str(&String::deserialize(deserializer)?).map_err(de::Error::custom),
 33              false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "register type"),
 34          }
 35      }
 36  }
 37  
 38  #[cfg(test)]
 39  mod tests {
 40      use super::*;
 41      use alphavm_console_network::MainnetV0;
 42  
 43      type CurrentNetwork = MainnetV0;
 44  
 45      /// Add test cases here to be checked for serialization.
 46      const TEST_CASES: &[&str] = &[
 47          // Literal
 48          "address",
 49          "boolean",
 50          "field",
 51          "group",
 52          "i8",
 53          "i16",
 54          "i32",
 55          "i64",
 56          "i128",
 57          "u8",
 58          "u16",
 59          "u32",
 60          "u64",
 61          "u128",
 62          "scalar",
 63          "string",
 64          // Struct
 65          "signature",
 66          "message",
 67          "item",
 68          "passport",
 69          "object",
 70          "array",
 71          // Record
 72          "token.record",
 73          "hello.record",
 74          // ExternalRecord
 75          "token.alpha/token.record",
 76      ];
 77  
 78      fn check_serde_json<
 79          T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
 80      >(
 81          expected: T,
 82      ) {
 83          // Serialize
 84          let expected_string = &expected.to_string();
 85          let candidate_string = serde_json::to_string(&expected).unwrap();
 86          assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string).unwrap().as_str().unwrap());
 87  
 88          // Deserialize
 89          assert_eq!(expected, T::from_str(expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
 90          assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
 91      }
 92  
 93      fn check_bincode<
 94          T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
 95      >(
 96          expected: T,
 97      ) {
 98          // Serialize
 99          let expected_bytes = expected.to_bytes_le().unwrap();
100          let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
101          assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
102  
103          // Deserialize
104          assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
105          assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
106      }
107  
108      #[test]
109      fn test_serde_json() {
110          for case in TEST_CASES.iter() {
111              check_serde_json(RegisterType::<CurrentNetwork>::from_str(case).unwrap());
112          }
113      }
114  
115      #[test]
116      fn test_bincode() {
117          for case in TEST_CASES.iter() {
118              check_bincode(RegisterType::<CurrentNetwork>::from_str(case).unwrap());
119          }
120      }
121  }