snarkvm_circuit_program/data/plaintext/
find.rs

1// Copyright (c) 2019-2025 Provable Inc.
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 super::*;
17
18impl<A: Aleo> Plaintext<A> {
19    /// Returns the plaintext member from the given path.
20    pub fn find<A0: Into<Access<A>> + Clone + Debug>(&self, path: &[A0]) -> Result<Plaintext<A>> {
21        // Ensure the path is not empty.
22        if path.is_empty() {
23            A::halt("Attempted to find member with an empty path.")
24        }
25
26        match self {
27            // Halts if the value is not a struct or an array.
28            Self::Literal(..) => A::halt("A literal is not a struct or an array"),
29            // Retrieve the value of the member (from the value).
30            Self::Struct(..) | Self::Array(..) => {
31                // Initialize the plaintext starting from the top-level.
32                let mut plaintext = self;
33
34                // Iterate through the path to retrieve the value.
35                for access in path.iter() {
36                    let access = access.clone().into();
37                    match (plaintext, &access) {
38                        (Self::Struct(members, ..), Access::Member(identifier)) => {
39                            match members.get(identifier) {
40                                // Retrieve the member and update `plaintext` for the next iteration.
41                                Some(member) => plaintext = member,
42                                // Halts if the member does not exist.
43                                None => bail!("Failed to locate member '{identifier}'"),
44                            }
45                        }
46                        (Self::Array(array, ..), Access::Index(index)) => {
47                            let index = match index.eject_mode() {
48                                Mode::Constant => index.eject_value(),
49                                _ => bail!("'{index}' must be a constant"),
50                            };
51                            match array.get(*index as usize) {
52                                // Retrieve the element and update `plaintext` for the next iteration.
53                                Some(element) => plaintext = element,
54                                // Halts if the element does not exist.
55                                None => bail!("Failed to locate element '{index}'"),
56                            }
57                        }
58                        _ => bail!("Invalid access `{access}``"),
59                    }
60                }
61
62                // Return the output.
63                Ok(plaintext.clone())
64            }
65        }
66    }
67}