Need to do loop elements.
This commit is contained in:
parent
fcb2f3fc4d
commit
2712126b3c
@ -58,33 +58,23 @@ impl error::Error for RenderError<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for WalkError<'_> {
|
||||
impl fmt::Display for WalkError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
WalkError::CantWalk { segment, elem } => {
|
||||
write!(f, "Tried to walk to {} from {:?}", segment, elem)
|
||||
}
|
||||
WalkError::NotFound { path, breadcrumbs } => {
|
||||
write!(f, "Could not find {:?} in {:?}", path, breadcrumbs)
|
||||
}
|
||||
WalkError::CantWalk => write!(f, "Failed to walk"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for WalkError<'_> {
|
||||
impl fmt::Debug for WalkError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
WalkError::CantWalk { segment, elem } => {
|
||||
write!(f, "Tried to walk to {} from {:?}", segment, elem)
|
||||
}
|
||||
WalkError::NotFound { path, breadcrumbs } => {
|
||||
write!(f, "Could not find {:?} in {:?}", path, breadcrumbs)
|
||||
}
|
||||
WalkError::CantWalk => write!(f, "Failed to walk"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for WalkError<'_> {
|
||||
impl error::Error for WalkError {
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||
None
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ pub use context_element::Renderable;
|
||||
pub use context_element::Walkable;
|
||||
pub use errors::CompileError;
|
||||
pub use errors::RenderError;
|
||||
pub use errors::WalkError;
|
||||
pub use renderer::compile_template;
|
||||
pub use renderer::CompiledTemplate;
|
||||
pub use renderer::DustRenderer;
|
||||
|
@ -4,6 +4,7 @@ use crate::renderer::context_element::ContextElement;
|
||||
use crate::renderer::Loopable;
|
||||
use crate::renderer::RenderError;
|
||||
use crate::renderer::Renderable;
|
||||
use crate::renderer::WalkError;
|
||||
use crate::renderer::Walkable;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -44,11 +45,8 @@ impl<'a> Loopable for ParametersContext<'a> {
|
||||
}
|
||||
|
||||
impl<'a> Walkable for ParametersContext<'a> {
|
||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
|
||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
|
||||
// TODO: Actually implement
|
||||
Err(RenderError::CantWalk {
|
||||
segment: segment.to_string(),
|
||||
elem: self,
|
||||
})
|
||||
Err(WalkError::CantWalk)
|
||||
}
|
||||
}
|
||||
|
@ -102,7 +102,7 @@ impl<'a> DustRenderer<'a> {
|
||||
DustTag::DTReference(reference) => {
|
||||
let val = walk_path(breadcrumbs, &reference.path.keys);
|
||||
match val {
|
||||
Err(RenderError::NotFound { .. }) => return Ok("".to_owned()),
|
||||
Err(WalkError::CantWalk) => return Ok("".to_owned()),
|
||||
Ok(final_val) => {
|
||||
let loop_elements = final_val.get_loop_elements()?;
|
||||
if loop_elements.is_empty() {
|
||||
@ -111,7 +111,6 @@ impl<'a> DustRenderer<'a> {
|
||||
return final_val.render(&reference.filters);
|
||||
}
|
||||
}
|
||||
Err(render_error) => return Err(render_error),
|
||||
}
|
||||
}
|
||||
DustTag::DTSection(container) => {
|
||||
@ -218,48 +217,46 @@ enum WalkResult<'a> {
|
||||
fn walk_path_from_single_level<'a>(
|
||||
context: &'a dyn ContextElement,
|
||||
path: &Vec<&str>,
|
||||
) -> Result<WalkResult<'a>, RenderError<'a>> {
|
||||
) -> WalkResult<'a> {
|
||||
if path.is_empty() {
|
||||
return Ok(WalkResult::FullyWalked(context));
|
||||
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);
|
||||
if let Err(WalkError::CantWalk { .. }) = new_val {
|
||||
return Ok(walk_failure);
|
||||
match output.walk(elem) {
|
||||
Err(WalkError::CantWalk { .. }) => {
|
||||
return walk_failure;
|
||||
}
|
||||
Ok(new_val) => {
|
||||
walk_failure = WalkResult::PartialWalk;
|
||||
output = new_val?;
|
||||
output = new_val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(WalkResult::FullyWalked(output))
|
||||
WalkResult::FullyWalked(output)
|
||||
}
|
||||
|
||||
fn walk_path<'a>(
|
||||
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
||||
path: &'a Vec<&str>,
|
||||
) -> Result<&'a dyn ContextElement, RenderError<'a>> {
|
||||
) -> Result<&'a dyn ContextElement, WalkError> {
|
||||
for context in breadcrumbs.iter().rev() {
|
||||
match walk_path_from_single_level(*context, path)? {
|
||||
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(RenderError::NotFound {
|
||||
path: path,
|
||||
breadcrumbs: breadcrumbs.clone(),
|
||||
});
|
||||
return Err(WalkError::CantWalk);
|
||||
}
|
||||
WalkResult::FullyWalked(new_context) => return Ok(new_context),
|
||||
}
|
||||
}
|
||||
Err(RenderError::NotFound {
|
||||
path: path,
|
||||
breadcrumbs: breadcrumbs.clone(),
|
||||
})
|
||||
Err(WalkError::CantWalk)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
Loading…
Reference in New Issue
Block a user