/ console / program / src / data / value / parse.rs
parse.rs
  1  // Copyright (c) 2019-2025 Alpha-Delta Network Inc.
  2  // This file is part of the alphavm 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> 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  
 31  impl<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  
 49  impl<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  
 56  impl<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)]
 68  mod tests {
 69      use super::*;
 70      use alphavm_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: ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w,
 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: ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w.private,
 92    token_amount: 100u64.private,
 93    _nonce: 6122363155094913586073041054293642159180066699840940609722305038224296461351group.public,
 94    _version: 0u8.public
 95  }";
 96          // Construct a new record value.
 97          let expected = Value::<CurrentNetwork>::from_str(string).unwrap();
 98          assert!(matches!(expected, Value::Record(..)));
 99          assert_eq!(string, format!("{expected}"));
100      }
101  
102      #[test]
103      fn test_value_future_parse() {
104          // Prepare the future string.
105          let string = r"{
106    program_id: credits.alpha,
107    function_name: transfer_public_to_private,
108    arguments: [
109      ax150w2lvhdzychwvzu54ys5zas7tm5s0ycdyw563pms83g9u0vucgqe5fs5w,
110      100000000u64
111    ]
112  }";
113          // Construct a new future value.
114          let expected = Value::<CurrentNetwork>::from_str(string).unwrap();
115          assert!(matches!(expected, Value::Future(..)));
116          assert_eq!(string, format!("{expected}"));
117      }
118  }