snarkvm_circuit_program/data/access/
mod.rsuse crate::Identifier;
use snarkvm_circuit_network::Aleo;
use snarkvm_circuit_types::{U32, environment::prelude::*};
use std::{
fmt,
fmt::{Debug, Display, Formatter},
str::FromStr,
};
#[derive(Clone)]
pub enum Access<A: Aleo> {
Member(Identifier<A>),
Index(U32<A>),
}
#[cfg(feature = "console")]
impl<A: Aleo> Inject for Access<A> {
type Primitive = console::Access<A::Network>;
fn new(_m: Mode, plaintext: Self::Primitive) -> Self {
match plaintext {
Self::Primitive::Member(identifier) => Self::Member(Identifier::new(_m, identifier)),
Self::Primitive::Index(index) => Self::Index(U32::new(_m, index)),
}
}
}
#[cfg(feature = "console")]
impl<A: Aleo> Eject for Access<A> {
type Primitive = console::Access<A::Network>;
fn eject_mode(&self) -> Mode {
match self {
Self::Member(member) => member.eject_mode(),
Self::Index(index) => index.eject_mode(),
}
}
fn eject_value(&self) -> Self::Primitive {
match self {
Self::Member(identifier) => console::Access::Member(identifier.eject_value()),
Self::Index(index) => console::Access::Index(index.eject_value()),
}
}
}
#[cfg(feature = "console")]
impl<A: Aleo> Parser for Access<A> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
let (string, access) = console::Access::parse(string)?;
Ok((string, Access::constant(access)))
}
}
#[cfg(feature = "console")]
impl<A: Aleo> FromStr for Access<A> {
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}"),
}
}
}
#[cfg(feature = "console")]
impl<A: Aleo> Debug for Access<A> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
#[cfg(feature = "console")]
impl<A: Aleo> Display for Access<A> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.eject_value())
}
}
impl<A: Aleo> Eq for Access<A> {}
impl<A: Aleo> PartialEq for Access<A> {
fn eq(&self, other: &Self) -> bool {
self.eject_value() == other.eject_value()
}
}