snarkvm_console_program/data/identifier/
parse.rsuse super::*;
impl<N: Network> Parser for Identifier<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
map_res(recognize(pair(alpha1, many0(alt((alphanumeric1, tag("_")))))), |identifier: &str| {
Self::from_str(identifier)
})(string)
}
}
impl<N: Network> FromStr for Identifier<N> {
type Err = Error;
fn from_str(identifier: &str) -> Result<Self, Self::Err> {
match identifier.chars().next() {
Some(character) => ensure!(character.is_ascii_alphabetic(), "Identifier must start with a letter"),
None => bail!("Identifier cannot be empty"),
}
if identifier.chars().any(|character| !character.is_ascii_alphanumeric() && character != '_') {
bail!("Identifier '{identifier}' must consist of letters, digits, and underscores")
}
let max_bytes = Field::<N>::size_in_data_bits() / 8; if identifier.len() > max_bytes {
bail!("Identifier is too large. Identifiers must be <= {max_bytes} bytes long")
}
ensure!(
!enum_iterator::all::<crate::LiteralType>().any(|lt| lt.type_name() == identifier),
"Identifier '{identifier}' is a reserved literal type"
);
Ok(Self(
Field::<N>::from_bits_le(&identifier.as_bytes().to_bits_le())?,
u8::try_from(identifier.len()).or_halt_with::<N>("Identifier `from_str` exceeds maximum length"),
))
}
}
impl<N: Network> Debug for Identifier<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for Identifier<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let bytes = self.0.to_bytes_le().map_err(|_| fmt::Error)?;
let string = String::from_utf8(bytes).map_err(|_| fmt::Error)?;
match string.split('\0').next() {
Some(string) => match string.len() == self.1 as usize {
true => write!(f, "{string}"),
false => Err(fmt::Error),
},
None => Err(fmt::Error),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::data::identifier::tests::{sample_identifier, sample_identifier_as_string};
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
const ITERATIONS: usize = 100;
#[test]
fn test_parse() -> Result<()> {
let (remainder, candidate) = Identifier::<CurrentNetwork>::parse("foo_bar1")?;
assert_eq!("foo_bar1", candidate.to_string());
assert_eq!("", remainder);
let (remainder, candidate) = Identifier::<CurrentNetwork>::parse("foo_bar~baz")?;
assert_eq!("foo_bar", candidate.to_string());
assert_eq!("~baz", remainder);
let (remainder, candidate) = Identifier::<CurrentNetwork>::parse("foo_bar-baz")?;
assert_eq!("foo_bar", candidate.to_string());
assert_eq!("-baz", remainder);
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected_string = sample_identifier_as_string::<CurrentNetwork>(&mut rng)?;
let expected_field = Field::<CurrentNetwork>::from_bits_le(&expected_string.to_bits_le())?;
let (remainder, candidate) = Identifier::<CurrentNetwork>::parse(expected_string.as_str()).unwrap();
assert_eq!(expected_string, candidate.to_string());
assert_eq!(expected_field, candidate.0);
assert_eq!(expected_string.len(), candidate.1 as usize);
assert_eq!("", remainder);
}
Ok(())
}
#[test]
fn test_parse_fails() {
assert!(Identifier::<CurrentNetwork>::parse("_").is_err());
assert!(Identifier::<CurrentNetwork>::parse("__").is_err());
assert!(Identifier::<CurrentNetwork>::parse("___").is_err());
assert!(Identifier::<CurrentNetwork>::parse("____").is_err());
assert!(Identifier::<CurrentNetwork>::parse("1").is_err());
assert!(Identifier::<CurrentNetwork>::parse("2").is_err());
assert!(Identifier::<CurrentNetwork>::parse("3").is_err());
assert!(Identifier::<CurrentNetwork>::parse("1foo").is_err());
assert!(Identifier::<CurrentNetwork>::parse("12").is_err());
assert!(Identifier::<CurrentNetwork>::parse("111").is_err());
let identifier =
Identifier::<CurrentNetwork>::parse("foo_bar_baz_qux_quux_quuz_corge_grault_garply_waldo_fred_plugh_xyzzy");
assert!(identifier.is_err());
}
#[test]
fn test_from_str() -> Result<()> {
let candidate = Identifier::<CurrentNetwork>::from_str("foo_bar").unwrap();
assert_eq!("foo_bar", candidate.to_string());
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected_string = sample_identifier_as_string::<CurrentNetwork>(&mut rng)?;
let expected_field = Field::<CurrentNetwork>::from_bits_le(&expected_string.to_bits_le())?;
let candidate = Identifier::<CurrentNetwork>::from_str(&expected_string)?;
assert_eq!(expected_string, candidate.to_string());
assert_eq!(expected_field, candidate.0);
assert_eq!(expected_string.len(), candidate.1 as usize);
}
Ok(())
}
#[test]
fn test_from_str_fails() {
assert!(Identifier::<CurrentNetwork>::from_str("").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("foo_bar~baz").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("foo_bar-baz").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("_").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("__").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("___").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("____").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("1").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("2").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("3").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("1foo").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("12").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("111").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("_foo").is_err());
assert!(Identifier::<CurrentNetwork>::from_str("\u{03b1}").is_err()); assert!(Identifier::<CurrentNetwork>::from_str("\u{03b2}").is_err()); let identifier = Identifier::<CurrentNetwork>::from_str(
"foo_bar_baz_qux_quux_quuz_corge_grault_garply_waldo_fred_plugh_xyzzy",
);
assert!(identifier.is_err());
}
#[test]
fn test_display() -> Result<()> {
let identifier = Identifier::<CurrentNetwork>::from_str("foo_bar")?;
assert_eq!("foo_bar", format!("{identifier}"));
Ok(())
}
#[test]
fn test_proxy_bits_equivalence() -> Result<()> {
let mut rng = TestRng::default();
let identifier: Identifier<CurrentNetwork> = sample_identifier(&mut rng)?;
let bytes1 = identifier.0.to_bytes_le()?;
let bits_le = identifier.0.to_bits_le();
let bytes2 = bits_le.chunks(8).map(u8::from_bits_le).collect::<Result<Vec<u8>, _>>()?;
assert_eq!(bytes1, bytes2);
Ok(())
}
}