Skip to main content

snarkvm_circuit_program/data/future/
find.rs

1// Copyright (c) 2019-2026 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> Future<A> {
19    /// Returns the value from the given path.
20    pub fn find<A0: Into<Access<A>> + Clone + Debug>(&self, path: &[A0]) -> Result<Value<A>> {
21        // Ensure the path is not empty.
22        ensure!(!path.is_empty(), "Attempted to find an argument with an empty path.");
23
24        // A helper enum to track the the argument.
25        enum ArgumentRefType<'a, A: Aleo> {
26            /// A plaintext type.
27            Plaintext(&'a Plaintext<A>),
28            /// A future.
29            Future(&'a Future<A>),
30            /// A dynamic future.
31            DynamicFuture(&'a DynamicFuture<A>),
32        }
33
34        // Initialize a value starting from the top-level.
35        let mut value = ArgumentRefType::Future(self);
36
37        // Iterate through the path to retrieve the value.
38        for access in path.iter() {
39            let access = access.clone().into();
40            match (value, &access) {
41                (ArgumentRefType::Plaintext(Plaintext::Struct(members, ..)), Access::Member(identifier)) => {
42                    match members.get(identifier) {
43                        // Retrieve the member and update `value` for the next iteration.
44                        Some(member) => value = ArgumentRefType::Plaintext(member),
45                        // Halts if the member does not exist.
46                        None => bail!("Failed to locate member '{identifier}''"),
47                    }
48                }
49                (ArgumentRefType::Plaintext(Plaintext::Array(array, ..)), Access::Index(index)) => {
50                    let index = match index.eject_mode() {
51                        Mode::Constant => index.eject_value(),
52                        _ => bail!("'{index}' must be a constant"),
53                    };
54                    match array.get(*index as usize) {
55                        // Retrieve the element and update `value` for the next iteration.
56                        Some(element) => value = ArgumentRefType::Plaintext(element),
57                        // Halts if the index is out of bounds.
58                        None => bail!("Index '{index}' is out of bounds"),
59                    }
60                }
61                (ArgumentRefType::Future(future), Access::Index(index)) => {
62                    let index = match index.eject_mode() {
63                        Mode::Constant => index.eject_value(),
64                        _ => bail!("'{index}' must be a constant"),
65                    };
66                    match future.arguments.get(*index as usize) {
67                        // If the argument is a future, update `value` for the next iteration.
68                        Some(Argument::Future(future)) => value = ArgumentRefType::Future(future),
69                        // If the argument is a plaintext, update `value` for the next iteration.
70                        Some(Argument::Plaintext(plaintext)) => value = ArgumentRefType::Plaintext(plaintext),
71                        // If the argument is a dynamic future, update `value` for the next iteration.
72                        Some(Argument::DynamicFuture(dynamic_future)) => {
73                            value = ArgumentRefType::DynamicFuture(dynamic_future)
74                        }
75                        // Halts if the index is out of bounds.
76                        None => bail!("Index '{index}' is out of bounds"),
77                    }
78                }
79                _ => bail!("Invalid access `{access}`"),
80            }
81        }
82
83        match value {
84            ArgumentRefType::Plaintext(plaintext) => Ok(Value::Plaintext(plaintext.clone())),
85            ArgumentRefType::Future(future) => Ok(Value::Future(future.clone())),
86            ArgumentRefType::DynamicFuture(dynamic_future) => Ok(Value::DynamicFuture(dynamic_future.clone())),
87        }
88    }
89}