2020-05-09 15:15:43 -04:00
|
|
|
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)
|
|
|
|
}
|
2020-05-10 21:34:18 -04:00
|
|
|
|
|
|
|
pub fn owned_walk_path<'a>(
|
|
|
|
breadcrumbs: &Vec<Box<dyn ContextElement>>,
|
|
|
|
path: &Vec<String>,
|
|
|
|
) -> Result<&'a dyn ContextElement, WalkError> {
|
|
|
|
// TODO: Implement owned_walk_path
|
|
|
|
Err(WalkError::CantWalk)
|
|
|
|
}
|