use super::*;
impl<N: Network> Parser for ProgramID<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
map_res(pair(Identifier::parse, pair(tag("."), Identifier::parse)), |(name, (_, network))| {
Self::try_from((name, network))
})(string)
}
}
impl<N: Network> FromStr for ProgramID<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 ProgramID<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for ProgramID<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{name}.{network}", name = self.name, network = self.network)
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_parse() -> Result<()> {
let id = ProgramID::<CurrentNetwork>::parse("bar.aleo").unwrap().1;
assert_eq!(id.name(), &Identifier::<CurrentNetwork>::from_str("bar")?);
assert_eq!(id.network(), &Identifier::<CurrentNetwork>::from_str("aleo")?);
assert!(ProgramID::<CurrentNetwork>::parse("foo").is_err());
Ok(())
}
#[test]
fn test_display() -> Result<()> {
let id = ProgramID::<CurrentNetwork>::from_str("bar.aleo")?;
assert_eq!("bar.aleo", id.to_string());
assert!(ProgramID::<CurrentNetwork>::from_str("foo").is_err());
assert!(ProgramID::<CurrentNetwork>::from_str("Bar.aleo").is_err());
assert!(ProgramID::<CurrentNetwork>::from_str("foO.aleo").is_err());
assert!(ProgramID::<CurrentNetwork>::from_str("0foo.aleo").is_err());
assert!(ProgramID::<CurrentNetwork>::from_str("0_foo.aleo").is_err());
assert!(ProgramID::<CurrentNetwork>::from_str("_foo.aleo").is_err());
Ok(())
}
}