snarkvm_console_program/data/future/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> Serialize for Future<N> {
19 /// Serializes the future into a string or as 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 Future<N> {
29 /// Deserializes the future 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, "future"),
34 }
35 }
36}