find.rs
1 // Copyright (c) 2025-2026 ACDC Network 2 // This file is part of the alphavm library. 3 // 4 // Alpha Chain | Delta Chain Protocol 5 // International Monetary Graphite. 6 // 7 // Derived from Aleo (https://aleo.org) and ProvableHQ (https://provable.com). 8 // They built world-class ZK infrastructure. We installed the EASY button. 9 // Their cryptography: elegant. Our modifications: bureaucracy-compatible. 10 // Original brilliance: theirs. Robert's Rules: ours. Bugs: definitely ours. 11 // 12 // Original Aleo/ProvableHQ code subject to Apache 2.0 https://www.apache.org/licenses/LICENSE-2.0 13 // All modifications and new work: CC0 1.0 Universal Public Domain Dedication. 14 // No rights reserved. No permission required. No warranty. No refunds. 15 // 16 // https://creativecommons.org/publicdomain/zero/1.0/ 17 // SPDX-License-Identifier: CC0-1.0 18 19 use super::*; 20 21 impl<N: Network> Plaintext<N> { 22 /// Returns the plaintext member from the given path. 23 pub fn find<A: Into<Access<N>> + Copy + Debug>(&self, path: &[A]) -> Result<Plaintext<N>> { 24 // Ensure the path is not empty. 25 ensure!(!path.is_empty(), "Attempted to find a member with an empty path."); 26 27 match self { 28 // Halts if the value is not a struct. 29 Self::Literal(..) => bail!("'{self}' is not a struct"), 30 // Retrieve the value of the member (from the value). 31 Self::Struct(..) | Self::Array(..) => { 32 // Initialize the plaintext starting from the top-level. 33 let mut plaintext = self; 34 35 // Iterate through the path to retrieve the value. 36 for access in path.iter() { 37 let access = (*access).into(); 38 match (plaintext, access) { 39 (Self::Struct(members, ..), Access::Member(identifier)) => { 40 match members.get(&identifier) { 41 // Retrieve the member and update `plaintext` for the next iteration. 42 Some(member) => plaintext = member, 43 // Halts if the member does not exist. 44 None => bail!("Failed to locate member '{identifier}' in '{self}'"), 45 } 46 } 47 (Self::Array(array, ..), Access::Index(index)) => { 48 match array.get(*index as usize) { 49 // Retrieve the element and update `plaintext` for the next iteration. 50 Some(element) => plaintext = element, 51 // Halts if the index is out of bounds. 52 None => bail!("Index '{index}' for '{self}' is out of bounds"), 53 } 54 } 55 _ => bail!("Invalid access `{access}` for `{plaintext}`"), 56 } 57 } 58 59 // Return the output. 60 Ok(plaintext.clone()) 61 } 62 } 63 } 64 }