snarkvm_console_program/data/value/
parse.rs

1// Copyright 2024-2025 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> Parser for Value<N> {
19    /// Parses a string into a value.
20    #[inline]
21    fn parse(string: &str) -> ParserResult<Self> {
22        // Note that the order of the parsers matters.
23        alt((
24            map(Future::parse, Value::Future),
25            map(Plaintext::parse, Value::Plaintext),
26            map(Record::parse, Value::Record),
27        ))(string)
28    }
29}
30
31impl<N: Network> FromStr for Value<N> {
32    type Err = Error;
33
34    /// Parses a string into a value.
35    #[inline]
36    fn from_str(string: &str) -> Result<Self> {
37        match Self::parse(string) {
38            Ok((remainder, object)) => {
39                // Ensure the remainder is empty.
40                ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
41                // Return the object.
42                Ok(object)
43            }
44            Err(error) => bail!("Failed to parse string. {error}"),
45        }
46    }
47}
48
49impl<N: Network> Debug for Value<N> {
50    /// Prints the value as a string.
51    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
52        Display::fmt(self, f)
53    }
54}
55
56impl<N: Network> Display for Value<N> {
57    /// Prints the value as a string.
58    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
59        match self {
60            Value::Plaintext(plaintext) => Display::fmt(plaintext, f),
61            Value::Record(record) => Display::fmt(record, f),
62            Value::Future(future) => Display::fmt(future, f),
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70    use snarkvm_console_network::MainnetV0;
71
72    type CurrentNetwork = MainnetV0;
73
74    #[test]
75    fn test_value_plaintext_parse() {
76        // Prepare the plaintext string.
77        let string = r"{
78  owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah,
79  token_amount: 100u64
80}";
81        // Construct a new plaintext value.
82        let expected = Value::<CurrentNetwork>::from_str(string).unwrap();
83        assert!(matches!(expected, Value::Plaintext(..)));
84        assert_eq!(string, format!("{expected}"));
85    }
86
87    #[test]
88    fn test_value_record_parse() {
89        // Prepare the record string.
90        let string = r"{
91  owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah.private,
92  token_amount: 100u64.private,
93  _nonce: 6122363155094913586073041054293642159180066699840940609722305038224296461351group.public
94}";
95        // Construct a new record value.
96        let expected = Value::<CurrentNetwork>::from_str(string).unwrap();
97        assert!(matches!(expected, Value::Record(..)));
98        assert_eq!(string, format!("{expected}"));
99    }
100
101    #[test]
102    fn test_value_future_parse() {
103        // Prepare the future string.
104        let string = r"{
105  program_id: credits.aleo,
106  function_name: transfer_public_to_private,
107  arguments: [
108    aleo1g8qul5a44vk22u9uuvaewdcjw4v6xg8wx0llru39nnjn7eu08yrscxe4e2,
109    100000000u64
110  ]
111}";
112        // Construct a new future value.
113        let expected = Value::<CurrentNetwork>::from_str(string).unwrap();
114        assert!(matches!(expected, Value::Future(..)));
115        assert_eq!(string, format!("{expected}"));
116    }
117}