snarkvm_console_program/data_types/record_type/
serialize.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM 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
16use super::*;
17
18impl<N: Network> Serialize for RecordType<N> {
19    /// Serializes the record 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
28impl<'de, N: Network> Deserialize<'de> for RecordType<N> {
29    /// Deserializes the record 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, "record type"),
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41    use snarkvm_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        &["record message: owner as address.private; is_new as boolean.public; total_supply as u64.private;"];
48
49    fn check_serde_json<
50        T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
51    >(
52        expected: T,
53    ) {
54        // Serialize
55        let expected_string = &expected.to_string();
56        let candidate_string = serde_json::to_string(&expected).unwrap();
57        assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string).unwrap().as_str().unwrap());
58
59        // Deserialize
60        assert_eq!(expected, T::from_str(expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
61        assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
62    }
63
64    fn check_bincode<
65        T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
66    >(
67        expected: T,
68    ) {
69        // Serialize
70        let expected_bytes = expected.to_bytes_le().unwrap();
71        let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
72        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
73
74        // Deserialize
75        assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
76        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
77    }
78
79    #[test]
80    fn test_serde_json() {
81        for case in TEST_CASES.iter() {
82            check_serde_json(RecordType::<CurrentNetwork>::from_str(case).unwrap());
83        }
84    }
85
86    #[test]
87    fn test_bincode() {
88        for case in TEST_CASES.iter() {
89            check_bincode(RecordType::<CurrentNetwork>::from_str(case).unwrap());
90        }
91    }
92}