snarkvm_circuit_program/data/access/
mod.rs

1// Copyright 2024-2025 Aleo Network Foundation
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use 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/// A helper type for accessing an entry in a register, struct, array, or record.
27#[derive(Clone)]
28pub enum Access<A: Aleo> {
29    /// Access a member of a register, struct, or record.
30    Member(Identifier<A>),
31    /// Access an element of an array.
32    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    /// Initializes a new access circuit from a primitive.
40    /// Note: Access types are always `Mode::Constant`.
41    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    /// Ejects the mode of the access.
54    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    /// Ejects the access.
62    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    /// Parses a UTF-8 string into an access.
73    #[inline]
74    fn parse(string: &str) -> ParserResult<Self> {
75        // Parse the identifier from the string.
76        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    /// Parses a UTF-8 string into an identifier.
87    #[inline]
88    fn from_str(string: &str) -> Result<Self> {
89        match Self::parse(string) {
90            Ok((remainder, object)) => {
91                // Ensure the remainder is empty.
92                ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
93                // Return the object.
94                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    /// Prints the identifier as a string.
111    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    /// Implements the `Eq` trait for the access.
120    fn eq(&self, other: &Self) -> bool {
121        self.eject_value() == other.eject_value()
122    }
123}