serialize.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the deltavm 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 ProgramCore<N> { 19 /// Serializes the program 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 ProgramCore<N> { 29 /// Deserializes the program 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, "program"), 34 } 35 } 36 } 37 38 #[cfg(test)] 39 mod tests { 40 use super::*; 41 use crate::Program; 42 use console::network::MainnetV0; 43 44 type CurrentNetwork = MainnetV0; 45 46 #[test] 47 fn test_serde_json() -> Result<()> { 48 let program_string = r"program to_parse.delta; 49 50 struct message: 51 first as field; 52 second as field; 53 54 function compute: 55 input r0 as message.private; 56 add r0.first r0.second into r1; 57 output r1 as field.private; 58 "; 59 // Parse a new program. 60 let expected = Program::<CurrentNetwork>::from_str(program_string)?; 61 62 // Serialize 63 let expected_string = &expected.to_string(); 64 let candidate_string = serde_json::to_string(&expected)?; 65 assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string)?.as_str().unwrap()); 66 67 // Deserialize 68 assert_eq!(expected, Program::from_str(expected_string)?); 69 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 70 71 Ok(()) 72 } 73 74 #[test] 75 fn test_bincode() -> Result<()> { 76 let program_string = r"program to_parse.delta; 77 78 struct message: 79 first as field; 80 second as field; 81 82 function compute: 83 input r0 as message.private; 84 add r0.first r0.second into r1; 85 output r1 as field.private; 86 "; 87 // Parse a new program. 88 let expected = Program::<CurrentNetwork>::from_str(program_string)?; 89 90 // Serialize 91 let expected_bytes = expected.to_bytes_le()?; 92 let expected_bytes_with_size_encoding = bincode::serialize(&expected)?; 93 assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]); 94 95 // Deserialize 96 assert_eq!(expected, Program::read_le(&expected_bytes[..])?); 97 assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?); 98 99 Ok(()) 100 } 101 }