/ console / program / src / data_types / array_type / serialize.rs
serialize.rs
  1  // Copyright (c) 2025-2026 ACDC Network
  2  // This file is part of the alphavm library.
  3  //
  4  // Alpha Chain | Delta Chain Protocol
  5  // International Monetary Graphite.
  6  //
  7  // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com).
  8  // They built world-class ZK infrastructure. We installed the EASY button.
  9  // Their cryptography: elegant. Our modifications: bureaucracy-compatible.
 10  // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours.
 11  //
 12  // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0
 13  // All modifications and new work: CC0 1.0 Universal Public Domain Dedication.
 14  // No rights reserved. No permission required. No warranty. No refunds.
 15  //
 16  // https://creativecommons.org/publicdomain/zero/1.0/
 17  // SPDX-License-Identifier: CC0-1.0
 18  
 19  use super::*;
 20  
 21  impl<N: Network> Serialize for ArrayType<N> {
 22      /// Serializes the array type into string or bytes.
 23      fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
 24          match serializer.is_human_readable() {
 25              true => serializer.collect_str(self),
 26              false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
 27          }
 28      }
 29  }
 30  
 31  impl<'de, N: Network> Deserialize<'de> for ArrayType<N> {
 32      /// Deserializes the array type from a string or bytes.
 33      fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
 34          match deserializer.is_human_readable() {
 35              true => FromStr::from_str(&String::deserialize(deserializer)?).map_err(de::Error::custom),
 36              false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "array type"),
 37          }
 38      }
 39  }
 40  
 41  #[cfg(test)]
 42  pub(crate) mod tests {
 43      use super::*;
 44      use alphavm_console_network::MainnetV0;
 45  
 46      /// Add test cases here to be checked for serialization.
 47      pub(crate) const TEST_CASES: &[&str] = &[
 48          "[boolean; 31u32]",
 49          "[field; 32u32]",
 50          "[group; 32u32]",
 51          "[scalar; 32u32]",
 52          "[u8; 1u32]",
 53          "[u16; 1u32]",
 54          "[u32; 1u32]",
 55          "[u64; 1u32]",
 56          "[u128; 1u32]",
 57          "[i8; 1u32]",
 58          "[i16; 1u32]",
 59          "[i32; 1u32]",
 60          "[i64; 1u32]",
 61          "[i128; 1u32]",
 62          "[signature; 1u32]",
 63          "[foo; 4u32]",
 64          "[bar; 4u32]",
 65          "[[u8; 1u32]; 2u32]",
 66          "[[[u8; 1u32]; 2u32]; 3u32]",
 67          "[[[[u8; 1u32]; 2u32]; 3u32]; 4u32]",
 68          "[[[[[u8; 1u32]; 2u32]; 3u32]; 4u32]; 5u32]",
 69          "[[[[[[u8; 1u32]; 2u32]; 3u32]; 4u32]; 5u32]; 6u32]",
 70          "[[[[[[[u8; 1u32]; 2u32]; 3u32]; 4u32]; 5u32]; 6u32]; 7u32]",
 71          "[[[[[[[[u8; 1u32]; 2u32]; 3u32]; 4u32]; 5u32]; 6u32]; 7u32]; 8u32]",
 72      ];
 73  
 74      fn check_serde_json<
 75          T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
 76      >(
 77          expected: T,
 78      ) {
 79          // Serialize
 80          let expected_string = &expected.to_string();
 81          let candidate_string = serde_json::to_string(&expected).unwrap();
 82          assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string).unwrap().as_str().unwrap());
 83  
 84          // Deserialize
 85          assert_eq!(expected, T::from_str(expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
 86          assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
 87      }
 88  
 89      fn check_bincode<
 90          T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
 91      >(
 92          expected: T,
 93      ) {
 94          // Serialize
 95          let expected_bytes = expected.to_bytes_le().unwrap();
 96          let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
 97          assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
 98  
 99          // Deserialize
100          assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
101          assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
102      }
103  
104      #[test]
105      fn test_serde_json() {
106          for case in TEST_CASES.iter() {
107              check_serde_json(ArrayType::<MainnetV0>::from_str(case).unwrap());
108          }
109      }
110  
111      #[test]
112      fn test_bincode() {
113          for case in TEST_CASES.iter() {
114              check_bincode(ArrayType::<MainnetV0>::from_str(case).unwrap());
115          }
116      }
117  }