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 Locator<N> { 22 /// Parses a string into a locator of the form `{program_id}/{resource}`. 23 #[inline] 24 fn parse(string: &str) -> ParserResult<'_, Self> { 25 // Parse the program ID from the string. 26 let (string, id) = ProgramID::parse(string)?; 27 // Parse the "/" and resource from the string. 28 let (string, (_, resource)) = pair(tag("/"), Identifier::parse)(string)?; 29 // Return the locator. 30 Ok((string, Self { id, resource })) 31 } 32 } 33 34 impl<N: Network> FromStr for Locator<N> { 35 type Err = Error; 36 37 /// Parses a string into a locator. 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 Locator<N> { 53 /// Prints the locator 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 Locator<N> { 60 /// Prints the locator as a string. 61 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 62 write!(f, "{id}/{resource}", id = self.id, resource = self.resource) 63 } 64 } 65 66 #[cfg(test)] 67 mod tests { 68 use super::*; 69 use alphavm_console_network::MainnetV0; 70 71 type CurrentNetwork = MainnetV0; 72 73 #[test] 74 fn test_import_parse() -> Result<()> { 75 let locator = Locator::<CurrentNetwork>::parse("foo.alpha/compute").unwrap().1; 76 assert_eq!(locator.name(), &Identifier::<CurrentNetwork>::from_str("foo")?); 77 assert_eq!(locator.network(), &Identifier::<CurrentNetwork>::from_str("alpha")?); 78 assert_eq!(locator.resource(), &Identifier::<CurrentNetwork>::from_str("compute")?); 79 80 assert!(Locator::<CurrentNetwork>::parse("foo.alpha").is_err()); 81 assert!(Locator::<CurrentNetwork>::parse("foo/compute").is_err()); 82 83 Ok(()) 84 } 85 86 #[test] 87 fn test_import_display() -> Result<()> { 88 let id = Locator::<CurrentNetwork>::from_str("foo.alpha/compute")?; 89 assert_eq!("foo.alpha/compute", id.to_string()); 90 91 assert!(Locator::<CurrentNetwork>::parse("foo.alpha").is_err()); 92 assert!(Locator::<CurrentNetwork>::parse("foo/compute").is_err()); 93 94 Ok(()) 95 } 96 }