/ console / program / src / id / parse.rs
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 ProgramID<N> {
22      /// Parses a string into a program ID of the form `{name}.{network}`.
23      #[inline]
24      fn parse(string: &str) -> ParserResult<'_, Self> {
25          // Parse the name, ".", and network-level domain (NLD) from the string.
26          map_res(pair(Identifier::parse, pair(tag("."), Identifier::parse)), |(name, (_, network))| {
27              // Return the program ID.
28              Self::try_from((name, network))
29          })(string)
30      }
31  }
32  
33  impl<N: Network> FromStr for ProgramID<N> {
34      type Err = Error;
35  
36      /// Parses a string into a program ID.
37      #[inline]
38      fn from_str(string: &str) -> Result<Self> {
39          match Self::parse(string) {
40              Ok((remainder, object)) => {
41                  // Ensure the remainder is empty.
42                  ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
43                  // Return the object.
44                  Ok(object)
45              }
46              Err(error) => bail!("Failed to parse string. {error}"),
47          }
48      }
49  }
50  
51  impl<N: Network> Debug for ProgramID<N> {
52      /// Prints the program ID as a string.
53      fn fmt(&self, f: &mut Formatter) -> fmt::Result {
54          Display::fmt(self, f)
55      }
56  }
57  
58  impl<N: Network> Display for ProgramID<N> {
59      /// Prints the program ID as a string.
60      fn fmt(&self, f: &mut Formatter) -> fmt::Result {
61          write!(f, "{name}.{network}", name = self.name, network = self.network)
62      }
63  }
64  
65  #[cfg(test)]
66  mod tests {
67      use super::*;
68      use alphavm_console_network::MainnetV0;
69  
70      type CurrentNetwork = MainnetV0;
71  
72      #[test]
73      fn test_parse() -> Result<()> {
74          let id = ProgramID::<CurrentNetwork>::parse("bar.alpha").unwrap().1;
75          assert_eq!(id.name(), &Identifier::<CurrentNetwork>::from_str("bar")?);
76          assert_eq!(id.network(), &Identifier::<CurrentNetwork>::from_str("alpha")?);
77  
78          assert!(ProgramID::<CurrentNetwork>::parse("foo").is_err());
79  
80          Ok(())
81      }
82  
83      #[test]
84      fn test_display() -> Result<()> {
85          let id = ProgramID::<CurrentNetwork>::from_str("bar.alpha")?;
86          assert_eq!("bar.alpha", id.to_string());
87  
88          assert!(ProgramID::<CurrentNetwork>::from_str("foo").is_err());
89          assert!(ProgramID::<CurrentNetwork>::from_str("Bar.alpha").is_err());
90          assert!(ProgramID::<CurrentNetwork>::from_str("foO.alpha").is_err());
91          assert!(ProgramID::<CurrentNetwork>::from_str("0foo.alpha").is_err());
92          assert!(ProgramID::<CurrentNetwork>::from_str("0_foo.alpha").is_err());
93          assert!(ProgramID::<CurrentNetwork>::from_str("_foo.alpha").is_err());
94  
95          Ok(())
96      }
97  }