snarkvm_console_program/data_types/record_type/entry_type/
parse.rsuse super::*;
impl<N: Network> Parser for EntryType<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
alt((
map(pair(PlaintextType::parse, tag(".constant")), |(plaintext_type, _)| Self::Constant(plaintext_type)),
map(pair(PlaintextType::parse, tag(".public")), |(plaintext_type, _)| Self::Public(plaintext_type)),
map(pair(PlaintextType::parse, tag(".private")), |(plaintext_type, _)| Self::Private(plaintext_type)),
))(string)
}
}
impl<N: Network> FromStr for EntryType<N> {
type Err = Error;
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 EntryType<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for EntryType<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Constant(plaintext_type) => write!(f, "{plaintext_type}.constant"),
Self::Public(plaintext_type) => write!(f, "{plaintext_type}.public"),
Self::Private(plaintext_type) => write!(f, "{plaintext_type}.private"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
#[test]
fn test_parse() -> Result<()> {
assert_eq!(
Ok(("", EntryType::<CurrentNetwork>::from_str("field.constant")?)),
EntryType::<CurrentNetwork>::parse("field.constant")
);
assert_eq!(
Ok(("", EntryType::<CurrentNetwork>::from_str("field.public")?)),
EntryType::<CurrentNetwork>::parse("field.public")
);
assert_eq!(
Ok(("", EntryType::<CurrentNetwork>::from_str("field.private")?)),
EntryType::<CurrentNetwork>::parse("field.private")
);
assert_eq!(
Ok(("", EntryType::<CurrentNetwork>::from_str("signature.constant")?)),
EntryType::<CurrentNetwork>::parse("signature.constant")
);
assert_eq!(
Ok(("", EntryType::<CurrentNetwork>::from_str("signature.public")?)),
EntryType::<CurrentNetwork>::parse("signature.public")
);
assert_eq!(
Ok(("", EntryType::<CurrentNetwork>::from_str("signature.private")?)),
EntryType::<CurrentNetwork>::parse("signature.private")
);
Ok(())
}
#[test]
fn test_parse_fails() -> Result<()> {
assert!(EntryType::<CurrentNetwork>::parse("field").is_err());
assert!(EntryType::<CurrentNetwork>::parse("signature").is_err());
assert!(EntryType::<CurrentNetwork>::parse("token").is_err());
assert!(EntryType::<CurrentNetwork>::parse("").is_err());
assert!(EntryType::<CurrentNetwork>::parse("{}").is_err());
assert!(EntryType::<CurrentNetwork>::parse("_").is_err());
assert!(EntryType::<CurrentNetwork>::parse("__").is_err());
assert!(EntryType::<CurrentNetwork>::parse("___").is_err());
assert!(EntryType::<CurrentNetwork>::parse("-").is_err());
assert!(EntryType::<CurrentNetwork>::parse("--").is_err());
assert!(EntryType::<CurrentNetwork>::parse("---").is_err());
assert!(EntryType::<CurrentNetwork>::parse("*").is_err());
assert!(EntryType::<CurrentNetwork>::parse("**").is_err());
assert!(EntryType::<CurrentNetwork>::parse("***").is_err());
assert!(EntryType::<CurrentNetwork>::parse("1").is_err());
assert!(EntryType::<CurrentNetwork>::parse("2").is_err());
assert!(EntryType::<CurrentNetwork>::parse("3").is_err());
assert!(EntryType::<CurrentNetwork>::parse("1foo").is_err());
assert!(EntryType::<CurrentNetwork>::parse("12").is_err());
assert!(EntryType::<CurrentNetwork>::parse("111").is_err());
let struct_ = EntryType::<CurrentNetwork>::parse(
"foo_bar_baz_qux_quux_quuz_corge_grault_garply_waldo_fred_plugh_xyzzy.private",
);
assert!(struct_.is_err());
Ok(())
}
#[test]
fn test_display() -> Result<()> {
assert_eq!(EntryType::<CurrentNetwork>::from_str("field.constant")?.to_string(), "field.constant");
assert_eq!(EntryType::<CurrentNetwork>::from_str("field.public")?.to_string(), "field.public");
assert_eq!(EntryType::<CurrentNetwork>::from_str("field.private")?.to_string(), "field.private");
assert_eq!(EntryType::<CurrentNetwork>::from_str("signature.constant")?.to_string(), "signature.constant");
assert_eq!(EntryType::<CurrentNetwork>::from_str("signature.public")?.to_string(), "signature.public");
assert_eq!(EntryType::<CurrentNetwork>::from_str("signature.private")?.to_string(), "signature.private");
Ok(())
}
}