54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
![]() |
use crate::renderer::context_element::ContextElement;
|
||
|
use crate::renderer::WalkError;
|
||
|
|
||
|
enum WalkResult<'a> {
|
||
|
NoWalk,
|
||
|
PartialWalk,
|
||
|
FullyWalked(&'a dyn ContextElement),
|
||
|
}
|
||
|
|
||
|
fn walk_path_from_single_level<'a>(
|
||
|
context: &'a dyn ContextElement,
|
||
|
path: &Vec<&str>,
|
||
|
) -> WalkResult<'a> {
|
||
|
if path.is_empty() {
|
||
|
return WalkResult::FullyWalked(context);
|
||
|
}
|
||
|
|
||
|
let mut walk_failure = WalkResult::NoWalk;
|
||
|
let mut output = context;
|
||
|
for elem in path.iter() {
|
||
|
let new_val = output.walk(elem);
|
||
|
match output.walk(elem) {
|
||
|
Err(WalkError::CantWalk { .. }) => {
|
||
|
return walk_failure;
|
||
|
}
|
||
|
Ok(new_val) => {
|
||
|
walk_failure = WalkResult::PartialWalk;
|
||
|
output = new_val;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
WalkResult::FullyWalked(output)
|
||
|
}
|
||
|
|
||
|
pub fn walk_path<'a>(
|
||
|
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
||
|
path: &'a Vec<&str>,
|
||
|
) -> Result<&'a dyn ContextElement, WalkError> {
|
||
|
for context in breadcrumbs.iter().rev() {
|
||
|
match walk_path_from_single_level(*context, path) {
|
||
|
// If no walking was done at all, keep looping
|
||
|
WalkResult::NoWalk => {}
|
||
|
// If we partially walked then stop trying to find
|
||
|
// anything
|
||
|
WalkResult::PartialWalk => {
|
||
|
return Err(WalkError::CantWalk);
|
||
|
}
|
||
|
WalkResult::FullyWalked(new_context) => return Ok(new_context),
|
||
|
}
|
||
|
}
|
||
|
Err(WalkError::CantWalk)
|
||
|
}
|