snarkvm_console_program/data/access/
parse.rsuse super::*;
impl<N: Network> Parser for Access<N> {
fn parse(string: &str) -> ParserResult<Self> {
alt((
map(pair(tag("["), pair(U32::parse, tag("]"))), |(_, (index, _))| Self::Index(index)),
map(pair(tag("."), Identifier::parse), |(_, identifier)| Self::Member(identifier)),
))(string)
}
}
impl<N: Network> FromStr for Access<N> {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network> Debug for Access<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for Access<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Member(identifier) => write!(f, ".{}", identifier),
Self::Index(index) => write!(f, "[{}]", index),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
#[test]
fn test_parse() -> Result<()> {
assert_eq!(Access::parse(".data"), Ok(("", Access::<CurrentNetwork>::Member(Identifier::from_str("data")?))));
assert_eq!(Access::parse("[0u32]"), Ok(("", Access::<CurrentNetwork>::Index(U32::new(0)))));
Ok(())
}
#[test]
fn test_parse_fails() -> Result<()> {
assert!(Access::<CurrentNetwork>::parse("").is_err());
assert!(Access::<CurrentNetwork>::parse(".").is_err());
assert!(Access::<CurrentNetwork>::parse("[]").is_err());
assert!(Access::<CurrentNetwork>::parse(".0").is_err());
assert!(Access::<CurrentNetwork>::parse("[index]").is_err());
assert!(Access::<CurrentNetwork>::parse("[0.0]").is_err());
assert!(Access::<CurrentNetwork>::parse("[999999999999]").is_err());
let access =
Access::<CurrentNetwork>::parse(".foo_bar_baz_qux_quux_quuz_corge_grault_garply_waldo_fred_plugh_xyzzy");
assert!(access.is_err());
Ok(())
}
#[test]
fn test_display() -> Result<()> {
assert_eq!(Access::<CurrentNetwork>::Member(Identifier::from_str("foo")?).to_string(), ".foo");
assert_eq!(Access::<CurrentNetwork>::Index(U32::new(0)).to_string(), "[0u32]");
Ok(())
}
}