parse.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use super::*; 20 21 impl<N: Network> Parser for Value<N> { 22 /// Parses a string into a value. 23 #[inline] 24 fn parse(string: &str) -> ParserResult<'_, Self> { 25 // Note that the order of the parsers matters. 26 alt(( 27 map(Future::parse, Value::Future), 28 map(Plaintext::parse, Value::Plaintext), 29 map(Record::parse, Value::Record), 30 ))(string) 31 } 32 } 33 34 impl<N: Network> FromStr for Value<N> { 35 type Err = Error; 36 37 /// Parses a string into a value. 38 #[inline] 39 fn from_str(string: &str) -> Result<Self> { 40 match Self::parse(string) { 41 Ok((remainder, object)) => { 42 // Ensure the remainder is empty. 43 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\""); 44 // Return the object. 45 Ok(object) 46 } 47 Err(error) => bail!("Failed to parse string. {error}"), 48 } 49 } 50 } 51 52 impl<N: Network> Debug for Value<N> { 53 /// Prints the value as a string. 54 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 55 Display::fmt(self, f) 56 } 57 } 58 59 impl<N: Network> Display for Value<N> { 60 /// Prints the value as a string. 61 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 62 match self { 63 Value::Plaintext(plaintext) => Display::fmt(plaintext, f), 64 Value::Record(record) => Display::fmt(record, f), 65 Value::Future(future) => Display::fmt(future, f), 66 } 67 } 68 } 69 70 #[cfg(test)] 71 mod tests { 72 use super::*; 73 use alphavm_console_network::MainnetV0; 74 75 type CurrentNetwork = MainnetV0; 76 77 #[test] 78 fn test_value_plaintext_parse() { 79 // Prepare the plaintext string. 80 let string = r"{ 81 owner: ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w, 82 token_amount: 100u64 83 }"; 84 // Construct a new plaintext value. 85 let expected = Value::<CurrentNetwork>::from_str(string).unwrap(); 86 assert!(matches!(expected, Value::Plaintext(..))); 87 assert_eq!(string, format!("{expected}")); 88 } 89 90 #[test] 91 fn test_value_record_parse() { 92 // Prepare the record string. 93 let string = r"{ 94 owner: ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w.private, 95 token_amount: 100u64.private, 96 _nonce: 6122363155094913586073041054293642159180066699840940609722305038224296461351group.public, 97 _version: 0u8.public 98 }"; 99 // Construct a new record value. 100 let expected = Value::<CurrentNetwork>::from_str(string).unwrap(); 101 assert!(matches!(expected, Value::Record(..))); 102 assert_eq!(string, format!("{expected}")); 103 } 104 105 #[test] 106 fn test_value_future_parse() { 107 // Prepare the future string. 108 let string = r"{ 109 program_id: credits.alpha, 110 function_name: transfer_public_to_private, 111 arguments: [ 112 ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w, 113 100000000u64 114 ] 115 }"; 116 // Construct a new future value. 117 let expected = Value::<CurrentNetwork>::from_str(string).unwrap(); 118 assert!(matches!(expected, Value::Future(..))); 119 assert_eq!(string, format!("{expected}")); 120 } 121 }