/ console / program / src / data_types / plaintext_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 PlaintextType<N> {
 19      /// Serializes the plaintext 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 PlaintextType<N> {
 29      /// Deserializes the plaintext 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, "plaintext 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      ];
 72  
 73      fn check_serde_json<
 74          T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
 75      >(
 76          expected: T,
 77      ) {
 78          // Serialize
 79          let expected_string = &expected.to_string();
 80          let candidate_string = serde_json::to_string(&expected).unwrap();
 81          assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string).unwrap().as_str().unwrap());
 82  
 83          // Deserialize
 84          assert_eq!(expected, T::from_str(expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
 85          assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
 86      }
 87  
 88      fn check_bincode<
 89          T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
 90      >(
 91          expected: T,
 92      ) {
 93          // Serialize
 94          let expected_bytes = expected.to_bytes_le().unwrap();
 95          let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
 96          assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
 97  
 98          // Deserialize
 99          assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
100          assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
101      }
102  
103      #[test]
104      fn test_serde_json() {
105          for case in TEST_CASES.iter().chain(crate::data_types::array_type::serialize::tests::TEST_CASES) {
106              check_serde_json(PlaintextType::<CurrentNetwork>::from_str(case).unwrap());
107          }
108      }
109  
110      #[test]
111      fn test_bincode() {
112          for case in TEST_CASES.iter().chain(crate::data_types::array_type::serialize::tests::TEST_CASES) {
113              check_bincode(PlaintextType::<CurrentNetwork>::from_str(case).unwrap());
114          }
115      }
116  }