snarkvm_circuit_program/data/access/
mod.rs1use crate::Identifier;
17use snarkvm_circuit_network::Aleo;
18use snarkvm_circuit_types::{U32, environment::prelude::*};
19
20use std::{
21 fmt,
22 fmt::{Debug, Display, Formatter},
23 str::FromStr,
24};
25
26#[derive(Clone)]
28pub enum Access<A: Aleo> {
29 Member(Identifier<A>),
31 Index(U32<A>),
33}
34
35#[cfg(feature = "console")]
36impl<A: Aleo> Inject for Access<A> {
37 type Primitive = console::Access<A::Network>;
38
39 fn new(_m: Mode, plaintext: Self::Primitive) -> Self {
42 match plaintext {
43 Self::Primitive::Member(identifier) => Self::Member(Identifier::new(_m, identifier)),
44 Self::Primitive::Index(index) => Self::Index(U32::new(_m, index)),
45 }
46 }
47}
48
49#[cfg(feature = "console")]
50impl<A: Aleo> Eject for Access<A> {
51 type Primitive = console::Access<A::Network>;
52
53 fn eject_mode(&self) -> Mode {
55 match self {
56 Self::Member(member) => member.eject_mode(),
57 Self::Index(index) => index.eject_mode(),
58 }
59 }
60
61 fn eject_value(&self) -> Self::Primitive {
63 match self {
64 Self::Member(identifier) => console::Access::Member(identifier.eject_value()),
65 Self::Index(index) => console::Access::Index(index.eject_value()),
66 }
67 }
68}
69
70#[cfg(feature = "console")]
71impl<A: Aleo> Parser for Access<A> {
72 #[inline]
74 fn parse(string: &str) -> ParserResult<Self> {
75 let (string, access) = console::Access::parse(string)?;
77
78 Ok((string, Access::constant(access)))
79 }
80}
81
82#[cfg(feature = "console")]
83impl<A: Aleo> FromStr for Access<A> {
84 type Err = Error;
85
86 #[inline]
88 fn from_str(string: &str) -> Result<Self> {
89 match Self::parse(string) {
90 Ok((remainder, object)) => {
91 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
93 Ok(object)
95 }
96 Err(error) => bail!("Failed to parse string. {error}"),
97 }
98 }
99}
100
101#[cfg(feature = "console")]
102impl<A: Aleo> Debug for Access<A> {
103 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
104 Display::fmt(self, f)
105 }
106}
107
108#[cfg(feature = "console")]
109impl<A: Aleo> Display for Access<A> {
110 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
112 write!(f, "{}", self.eject_value())
113 }
114}
115
116impl<A: Aleo> Eq for Access<A> {}
117
118impl<A: Aleo> PartialEq for Access<A> {
119 fn eq(&self, other: &Self) -> bool {
121 self.eject_value() == other.eject_value()
122 }
123}