parse.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the deltavm 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 MapKey<N> { 19 /// Parses a string into a key statement of the form `key as {plaintext_type}.public;`. 20 #[inline] 21 fn parse(string: &str) -> ParserResult<Self> { 22 // Parse the whitespace and comments from the string. 23 let (string, _) = Sanitizer::parse(string)?; 24 // Parse the keyword from the string. 25 let (string, _) = tag(Self::type_name())(string)?; 26 // Parse the whitespace from the string. 27 let (string, _) = Sanitizer::parse_whitespaces(string)?; 28 // Parse the "as" from the string. 29 let (string, _) = tag("as")(string)?; 30 // Parse the whitespace from the string. 31 let (string, _) = Sanitizer::parse_whitespaces(string)?; 32 // Parse the plaintext type from the string. 33 let (string, (plaintext_type, _)) = pair(PlaintextType::parse, tag(".public"))(string)?; 34 // Parse the whitespace from the string. 35 let (string, _) = Sanitizer::parse_whitespaces(string)?; 36 // Parse the semicolon from the string. 37 let (string, _) = tag(";")(string)?; 38 // Return the key statement. 39 Ok((string, Self { plaintext_type })) 40 } 41 } 42 43 impl<N: Network> FromStr for MapKey<N> { 44 type Err = Error; 45 46 /// Parses a string into the key statement. 47 #[inline] 48 fn from_str(string: &str) -> Result<Self> { 49 match Self::parse(string) { 50 Ok((remainder, object)) => { 51 // Ensure the remainder is empty. 52 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\""); 53 // Return the object. 54 Ok(object) 55 } 56 Err(error) => bail!("Failed to parse string. {error}"), 57 } 58 } 59 } 60 61 impl<N: Network> Debug for MapKey<N> { 62 /// Prints the key statement as a string. 63 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 64 Display::fmt(self, f) 65 } 66 } 67 68 impl<N: Network> Display for MapKey<N> { 69 /// Prints the key statement as a string. 70 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 71 write!( 72 f, 73 "{type_} as {plaintext_type}.public;", 74 type_ = Self::type_name(), 75 plaintext_type = self.plaintext_type 76 ) 77 } 78 } 79 80 #[cfg(test)] 81 mod tests { 82 use super::*; 83 use console::network::MainnetV0; 84 85 type CurrentNetwork = MainnetV0; 86 87 #[test] 88 fn test_key_parse() -> Result<()> { 89 // Literal 90 let key = MapKey::<CurrentNetwork>::parse("key as field.public;").unwrap().1; 91 assert_eq!(key.plaintext_type(), &PlaintextType::<CurrentNetwork>::from_str("field")?); 92 93 Ok(()) 94 } 95 96 #[test] 97 fn test_key_display() -> Result<()> { 98 // Literal 99 let key = MapKey::<CurrentNetwork>::from_str("key as field.public;")?; 100 assert_eq!("key as field.public;", key.to_string()); 101 102 Ok(()) 103 } 104 }