snarkvm_synthesizer_program/
serialize.rs

1// Copyright 2024 Aleo Network Foundation
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, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Serialize
19    for ProgramCore<N, Instruction, Command>
20{
21    /// Serializes the program into string or bytes.
22    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
23        match serializer.is_human_readable() {
24            true => serializer.collect_str(self),
25            false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
26        }
27    }
28}
29
30impl<'de, N: Network, Instruction: InstructionTrait<N>, Command: CommandTrait<N>> Deserialize<'de>
31    for ProgramCore<N, Instruction, Command>
32{
33    /// Deserializes the program from a string or bytes.
34    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
35        match deserializer.is_human_readable() {
36            true => FromStr::from_str(&String::deserialize(deserializer)?).map_err(de::Error::custom),
37            false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "program"),
38        }
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::Program;
46    use console::network::MainnetV0;
47
48    type CurrentNetwork = MainnetV0;
49
50    #[test]
51    fn test_serde_json() -> Result<()> {
52        let program_string = r"program to_parse.aleo;
53
54struct message:
55    first as field;
56    second as field;
57
58function compute:
59    input r0 as message.private;
60    add r0.first r0.second into r1;
61    output r1 as field.private;
62";
63        // Parse a new program.
64        let expected = Program::<CurrentNetwork>::from_str(program_string)?;
65
66        // Serialize
67        let expected_string = &expected.to_string();
68        let candidate_string = serde_json::to_string(&expected)?;
69        assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string)?.as_str().unwrap());
70
71        // Deserialize
72        assert_eq!(expected, Program::from_str(expected_string)?);
73        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
74
75        Ok(())
76    }
77
78    #[test]
79    fn test_bincode() -> Result<()> {
80        let program_string = r"program to_parse.aleo;
81
82struct message:
83    first as field;
84    second as field;
85
86function compute:
87    input r0 as message.private;
88    add r0.first r0.second into r1;
89    output r1 as field.private;
90";
91        // Parse a new program.
92        let expected = Program::<CurrentNetwork>::from_str(program_string)?;
93
94        // Serialize
95        let expected_bytes = expected.to_bytes_le()?;
96        let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
97        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
98
99        // Deserialize
100        assert_eq!(expected, Program::read_le(&expected_bytes[..])?);
101        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
102
103        Ok(())
104    }
105}