use super::*;
impl<A: Aleo> Plaintext<A> {
pub fn find<A0: Into<Access<A>> + Clone + Debug>(&self, path: &[A0]) -> Result<Plaintext<A>> {
if path.is_empty() {
A::halt("Attempted to find member with an empty path.")
}
match self {
Self::Literal(..) => A::halt("A literal is not a struct or an array"),
Self::Struct(..) | Self::Array(..) => {
let mut plaintext = self;
for access in path.iter() {
let access = access.clone().into();
match (plaintext, &access) {
(Self::Struct(members, ..), Access::Member(identifier)) => {
match members.get(identifier) {
Some(member) => plaintext = member,
None => bail!("Failed to locate member '{identifier}'"),
}
}
(Self::Array(array, ..), Access::Index(index)) => {
let index = match index.eject_mode() {
Mode::Constant => index.eject_value(),
_ => bail!("'{index}' must be a constant"),
};
match array.get(*index as usize) {
Some(element) => plaintext = element,
None => bail!("Failed to locate element '{index}'"),
}
}
_ => bail!("Invalid access `{access}``"),
}
}
Ok(plaintext.clone())
}
}
}
}