snarkvm_console_program/data/value/
parse.rs1use super::*;
17
18impl<N: Network> Parser for Value<N> {
19 #[inline]
21 fn parse(string: &str) -> ParserResult<Self> {
22 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 #[inline]
36 fn from_str(string: &str) -> Result<Self> {
37 match Self::parse(string) {
38 Ok((remainder, object)) => {
39 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
41 Ok(object)
43 }
44 Err(error) => bail!("Failed to parse string. {error}"),
45 }
46 }
47}
48
49impl<N: Network> Debug for Value<N> {
50 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
52 Display::fmt(self, f)
53 }
54}
55
56impl<N: Network> Display for Value<N> {
57 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 let string = r"{
78 owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah,
79 token_amount: 100u64
80}";
81 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 let string = r"{
91 owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah.private,
92 token_amount: 100u64.private,
93 _nonce: 6122363155094913586073041054293642159180066699840940609722305038224296461351group.public
94}";
95 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 let string = r"{
105 program_id: credits.aleo,
106 function_name: transfer_public_to_private,
107 arguments: [
108 aleo1g8qul5a44vk22u9uuvaewdcjw4v6xg8wx0llru39nnjn7eu08yrscxe4e2,
109 100000000u64
110 ]
111}";
112 let expected = Value::<CurrentNetwork>::from_str(string).unwrap();
114 assert!(matches!(expected, Value::Future(..)));
115 assert_eq!(string, format!("{expected}"));
116 }
117}