Remove unused code and clean up warnings.

This commit is contained in:
Tom Alexander
2020-06-07 13:25:27 -04:00
parent 4ab311c178
commit d06fbea288
13 changed files with 80 additions and 1530 deletions

View File

@@ -1,4 +1,4 @@
use crate::renderer::context_element::ContextElement;
use crate::renderer::breadcrumb_tree::BreadcrumbTreeElement;
use crate::renderer::context_element::IntoContextElement;
use crate::renderer::WalkError;
use std::borrow::Borrow;
@@ -9,19 +9,21 @@ enum WalkResult<'a> {
FullyWalked(&'a dyn IntoContextElement),
}
fn walk_path_from_single_level<'a, P, C>(context: &'a C, path: &[P]) -> WalkResult<'a>
fn walk_path_from_single_level<'a, P>(
context: &'a dyn IntoContextElement,
path: &[P],
) -> WalkResult<'a>
where
P: Borrow<str>,
C: Borrow<dyn IntoContextElement + 'a>,
{
if path.is_empty() {
return WalkResult::FullyWalked(context.borrow());
return WalkResult::FullyWalked(context);
}
let mut walk_failure = WalkResult::NoWalk;
let mut output = context.borrow();
let mut output = context;
for elem in path.iter() {
match output.borrow().walk(elem.borrow()) {
match output.walk(elem.borrow()) {
Err(WalkError::CantWalk { .. }) => {
return walk_failure;
}
@@ -35,68 +37,58 @@ where
WalkResult::FullyWalked(output)
}
pub fn get_first_non_pseudo_element<'a, B>(breadcrumbs: &'a Vec<B>) -> Option<&B>
where
B: Borrow<dyn IntoContextElement + 'a>,
{
fn get_first_non_pseudo_element<'a>(
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
) -> Option<&'a BreadcrumbTreeElement<'a>> {
breadcrumbs
.iter()
.rev()
.filter(|b| !(*b).borrow().is_pseudo_element())
.filter(|b| {
!std::borrow::Borrow::<dyn IntoContextElement + 'a>::borrow(*b).is_pseudo_element()
})
.next()
}
pub fn walk_path<'a, B, P>(
breadcrumbs: &'a Vec<B>,
pub fn walk_path<'a, P>(
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
path: &Vec<P>,
) -> Result<&'a dyn IntoContextElement, WalkError>
where
B: Borrow<dyn IntoContextElement + 'a>,
P: Borrow<str>,
{
if breadcrumbs.is_empty() {
// This happens when you use a section with an explicit
// context path, where both the path and the explicit context
// path fail, leaving you with absolutely no context.
return Err(WalkError::CantWalk);
}
if path.is_empty() {
return Ok(breadcrumbs
.last()
.expect("Breadcrumbs should never be empty.")
.borrow());
}
if path
.first()
.expect("Already proved path is not empty")
.borrow()
== "."
{
let first_non_pseudo_element = get_first_non_pseudo_element(breadcrumbs);
return match first_non_pseudo_element {
None => Err(WalkError::CantWalk),
Some(current_context) => {
match walk_path_from_single_level(current_context, &path[1..]) {
// If no walking was done at all or we partially walked
// then stop trying to find anything because '.' restricts
// us to the current scope
WalkResult::NoWalk | WalkResult::PartialWalk => Err(WalkError::CantWalk),
WalkResult::FullyWalked(new_context) => Ok(new_context),
match (breadcrumbs.last(), path.first()) {
(None, _) => return Err(WalkError::CantWalk),
(Some(last_elem), None) => return Ok(last_elem.borrow()),
(Some(_), Some(path_first)) if path_first.borrow() == "." => {
let first_non_pseudo_element = get_first_non_pseudo_element(breadcrumbs);
return match first_non_pseudo_element {
None => Err(WalkError::CantWalk),
Some(current_context) => {
match walk_path_from_single_level(current_context.borrow(), &path[1..]) {
// If no walking was done at all or we partially walked
// then stop trying to find anything because '.' restricts
// us to the current scope
WalkResult::NoWalk | WalkResult::PartialWalk => Err(WalkError::CantWalk),
WalkResult::FullyWalked(new_context) => Ok(new_context),
}
}
};
}
(Some(_), Some(_path_first)) => {
for context in breadcrumbs.iter().rev() {
match walk_path_from_single_level(context.borrow(), 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),
}
}
};
}
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)
}