snarkvm_console_program/data/access/
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 Access<N> {
19    /// Serializes the access into a 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 Access<N> {
29    /// Deserializes the access 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, "access"),
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    fn check_serde_json<
46        T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
47    >(
48        expected: T,
49    ) {
50        // Serialize
51        let expected_string = &expected.to_string();
52        let candidate_string = serde_json::to_string(&expected).unwrap();
53        assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string).unwrap().as_str().unwrap());
54
55        // Deserialize
56        assert_eq!(expected, T::from_str(expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
57        assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
58    }
59
60    fn check_bincode<
61        T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
62    >(
63        expected: T,
64    ) {
65        // Serialize
66        let expected_bytes = expected.to_bytes_le().unwrap();
67        let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
68        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
69
70        // Deserialize
71        assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
72        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
73    }
74
75    #[test]
76    fn test_serde_json() {
77        for i in 0..1000 {
78            check_serde_json(Access::<CurrentNetwork>::from_str(&format!(".owner_{i}")).unwrap());
79            check_serde_json(Access::<CurrentNetwork>::from_str(&format!("[{i}u32]")).unwrap());
80        }
81    }
82
83    #[test]
84    fn test_bincode() {
85        for i in 0..1000 {
86            check_bincode(Access::<CurrentNetwork>::from_str(&format!(".owner_{i}")).unwrap());
87            check_bincode(Access::<CurrentNetwork>::from_str(&format!("[{i}u32]")).unwrap());
88        }
89    }
90}