Merge branch 'rework_errors' into partial
This commit is contained in:
commit
ad498abb74
58
src/bin.rs
58
src/bin.rs
@ -8,6 +8,7 @@ use renderer::DustRenderer;
|
|||||||
use renderer::Loopable;
|
use renderer::Loopable;
|
||||||
use renderer::RenderError;
|
use renderer::RenderError;
|
||||||
use renderer::Renderable;
|
use renderer::Renderable;
|
||||||
|
use renderer::WalkError;
|
||||||
use renderer::Walkable;
|
use renderer::Walkable;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@ -91,67 +92,48 @@ impl Renderable for serde_json::Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Walkable for serde_json::Value {
|
impl Walkable for serde_json::Value {
|
||||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
|
||||||
match self {
|
match self {
|
||||||
serde_json::Value::Null => Err(RenderError::CantWalk {
|
serde_json::Value::Null => Err(WalkError::CantWalk),
|
||||||
segment: segment.to_string(),
|
serde_json::Value::Bool(_boolean) => Err(WalkError::CantWalk),
|
||||||
elem: self,
|
serde_json::Value::Number(_num) => Err(WalkError::CantWalk),
|
||||||
}),
|
serde_json::Value::String(_string) => Err(WalkError::CantWalk),
|
||||||
serde_json::Value::Bool(_boolean) => Err(RenderError::CantWalk {
|
serde_json::Value::Array(_arr) => Err(WalkError::CantWalk),
|
||||||
segment: segment.to_string(),
|
serde_json::Value::Object(obj) => obj
|
||||||
elem: self,
|
.get(segment)
|
||||||
}),
|
|
||||||
serde_json::Value::Number(_num) => Err(RenderError::CantWalk {
|
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
}),
|
|
||||||
serde_json::Value::String(_string) => Err(RenderError::CantWalk {
|
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
}),
|
|
||||||
serde_json::Value::Array(_arr) => Err(RenderError::CantWalk {
|
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
}),
|
|
||||||
serde_json::Value::Object(obj) => {
|
|
||||||
obj.get(segment)
|
|
||||||
.map(|val| val as _)
|
.map(|val| val as _)
|
||||||
.ok_or(RenderError::WontWalk {
|
.ok_or(WalkError::CantWalk),
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Loopable for serde_json::Value {
|
impl Loopable for serde_json::Value {
|
||||||
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
match self {
|
match self {
|
||||||
serde_json::Value::Null => Ok(Vec::new()),
|
serde_json::Value::Null => Vec::new(),
|
||||||
serde_json::Value::Bool(boolean) => {
|
serde_json::Value::Bool(boolean) => {
|
||||||
if *boolean {
|
if *boolean {
|
||||||
Ok(vec![self])
|
vec![self]
|
||||||
} else {
|
} else {
|
||||||
Ok(Vec::new())
|
Vec::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serde_json::Value::Number(_num) => Ok(vec![self]),
|
serde_json::Value::Number(_num) => vec![self],
|
||||||
serde_json::Value::String(string_value) => {
|
serde_json::Value::String(string_value) => {
|
||||||
if string_value.is_empty() {
|
if string_value.is_empty() {
|
||||||
Ok(Vec::new())
|
Vec::new()
|
||||||
} else {
|
} else {
|
||||||
Ok(vec![self])
|
vec![self]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serde_json::Value::Array(array_value) => {
|
serde_json::Value::Array(array_value) => {
|
||||||
if array_value.is_empty() {
|
if array_value.is_empty() {
|
||||||
Ok(Vec::new())
|
Vec::new()
|
||||||
} else {
|
} else {
|
||||||
Ok(array_value.iter().map(|x| x as _).collect())
|
array_value.iter().map(|x| x as _).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
serde_json::Value::Object(_obj) => Ok(vec![self]),
|
serde_json::Value::Object(_obj) => vec![self],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
use crate::parser::Filter;
|
use crate::parser::Filter;
|
||||||
use crate::renderer::errors::RenderError;
|
use crate::renderer::errors::RenderError;
|
||||||
|
use crate::renderer::errors::WalkError;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
pub trait ContextElement: Debug + Walkable + Renderable + Loopable {}
|
pub trait ContextElement: Debug + Walkable + Renderable + Loopable {}
|
||||||
|
|
||||||
pub trait Walkable {
|
pub trait Walkable {
|
||||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError>;
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Renderable {
|
pub trait Renderable {
|
||||||
@ -22,5 +23,5 @@ pub trait Loopable {
|
|||||||
/// once with the context being the element at that path. Finally,
|
/// once with the context being the element at that path. Finally,
|
||||||
/// if its an array-like value then it will render n-times, once
|
/// if its an array-like value then it will render n-times, once
|
||||||
/// for each element of the array.
|
/// for each element of the array.
|
||||||
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError>;
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement>;
|
||||||
}
|
}
|
||||||
|
@ -1,27 +1,16 @@
|
|||||||
use crate::renderer::context_element::ContextElement;
|
|
||||||
use std::error;
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
pub enum RenderError<'a> {
|
/// Fatal errors while rendering.
|
||||||
|
///
|
||||||
|
/// A RenderError will halt rendering.
|
||||||
|
pub enum RenderError {
|
||||||
Generic(String),
|
Generic(String),
|
||||||
/// For when walking is absolutely impossible
|
TemplateNotFound(String),
|
||||||
CantWalk {
|
}
|
||||||
segment: String,
|
|
||||||
elem: &'a dyn ContextElement,
|
pub enum WalkError {
|
||||||
},
|
CantWalk,
|
||||||
/// For when walking fails (example, a missing key on a map)
|
|
||||||
WontWalk {
|
|
||||||
segment: String,
|
|
||||||
elem: &'a dyn ContextElement,
|
|
||||||
},
|
|
||||||
NotFound {
|
|
||||||
path: &'a Vec<&'a str>,
|
|
||||||
breadcrumbs: Vec<&'a dyn ContextElement>,
|
|
||||||
},
|
|
||||||
/// Attempting to render and unrenderable type (for example, an object without any filters)
|
|
||||||
CantRender {
|
|
||||||
elem: &'a dyn ContextElement,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -29,43 +18,51 @@ pub struct CompileError {
|
|||||||
pub message: String,
|
pub message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for RenderError<'_> {
|
impl fmt::Display for RenderError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
RenderError::Generic(msg) => write!(f, "{}", msg),
|
RenderError::Generic(msg) => write!(f, "{}", msg),
|
||||||
RenderError::CantWalk { segment, elem } => {
|
RenderError::TemplateNotFound(name) => {
|
||||||
write!(f, "Tried to walk to {} from {:?}", segment, elem)
|
write!(f, "No template named {} in context", name)
|
||||||
}
|
|
||||||
RenderError::WontWalk { segment, elem } => {
|
|
||||||
write!(f, "Failed to walk to {} from {:?}", segment, elem)
|
|
||||||
}
|
|
||||||
RenderError::CantRender { elem } => write!(f, "Cant render {:?}", elem),
|
|
||||||
RenderError::NotFound { path, breadcrumbs } => {
|
|
||||||
write!(f, "Could not find {:?} in {:?}", path, breadcrumbs)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for RenderError<'_> {
|
impl fmt::Debug for RenderError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
RenderError::Generic(msg) => write!(f, "{}", msg),
|
RenderError::Generic(msg) => write!(f, "{}", msg),
|
||||||
RenderError::CantWalk { segment, elem } => {
|
RenderError::TemplateNotFound(name) => {
|
||||||
write!(f, "Tried to walk to {} from {:?}", segment, elem)
|
write!(f, "No template named {} in context", name)
|
||||||
}
|
|
||||||
RenderError::WontWalk { segment, elem } => {
|
|
||||||
write!(f, "Failed to walk to {} from {:?}", segment, elem)
|
|
||||||
}
|
|
||||||
RenderError::CantRender { elem } => write!(f, "Cant render {:?}", elem),
|
|
||||||
RenderError::NotFound { path, breadcrumbs } => {
|
|
||||||
write!(f, "Could not find {:?} in {:?}", path, breadcrumbs)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for RenderError<'_> {
|
impl error::Error for RenderError {
|
||||||
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for WalkError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
WalkError::CantWalk => write!(f, "Failed to walk"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Debug for WalkError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
WalkError::CantWalk => write!(f, "Failed to walk"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl error::Error for WalkError {
|
||||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ pub use context_element::Renderable;
|
|||||||
pub use context_element::Walkable;
|
pub use context_element::Walkable;
|
||||||
pub use errors::CompileError;
|
pub use errors::CompileError;
|
||||||
pub use errors::RenderError;
|
pub use errors::RenderError;
|
||||||
|
pub use errors::WalkError;
|
||||||
pub use renderer::compile_template;
|
pub use renderer::compile_template;
|
||||||
pub use renderer::CompiledTemplate;
|
pub use renderer::CompiledTemplate;
|
||||||
pub use renderer::DustRenderer;
|
pub use renderer::DustRenderer;
|
||||||
|
@ -4,6 +4,7 @@ use crate::renderer::context_element::ContextElement;
|
|||||||
use crate::renderer::Loopable;
|
use crate::renderer::Loopable;
|
||||||
use crate::renderer::RenderError;
|
use crate::renderer::RenderError;
|
||||||
use crate::renderer::Renderable;
|
use crate::renderer::Renderable;
|
||||||
|
use crate::renderer::WalkError;
|
||||||
use crate::renderer::Walkable;
|
use crate::renderer::Walkable;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@ -38,17 +39,15 @@ impl<'a> Renderable for ParametersContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Loopable for ParametersContext<'a> {
|
impl<'a> Loopable for ParametersContext<'a> {
|
||||||
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
Ok(vec![self])
|
// TODO: Would this even ever be called? Won't matter, but I'd like to know.
|
||||||
|
vec![self]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Walkable 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
|
// TODO: Actually implement
|
||||||
Err(RenderError::CantWalk {
|
Err(WalkError::CantWalk)
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ use crate::parser::TemplateElement;
|
|||||||
use crate::renderer::context_element::ContextElement;
|
use crate::renderer::context_element::ContextElement;
|
||||||
use crate::renderer::errors::CompileError;
|
use crate::renderer::errors::CompileError;
|
||||||
use crate::renderer::errors::RenderError;
|
use crate::renderer::errors::RenderError;
|
||||||
|
use crate::renderer::errors::WalkError;
|
||||||
use crate::renderer::parameters_context::ParametersContext;
|
use crate::renderer::parameters_context::ParametersContext;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
@ -50,14 +51,11 @@ impl<'a> DustRenderer<'a> {
|
|||||||
&'a self,
|
&'a self,
|
||||||
name: &str,
|
name: &str,
|
||||||
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
||||||
) -> Result<String, RenderError<'a>> {
|
) -> Result<String, RenderError> {
|
||||||
let main_template = match self.templates.get(name) {
|
let main_template = match self.templates.get(name) {
|
||||||
Some(tmpl) => tmpl,
|
Some(tmpl) => tmpl,
|
||||||
None => {
|
None => {
|
||||||
return Err(RenderError::Generic(format!(
|
return Err(RenderError::TemplateNotFound(name.to_owned()));
|
||||||
"No template named {} in context",
|
|
||||||
name
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
self.render_body(&main_template.contents, breadcrumbs)
|
self.render_body(&main_template.contents, breadcrumbs)
|
||||||
@ -67,7 +65,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
&'a self,
|
&'a self,
|
||||||
body: &'a Body,
|
body: &'a Body,
|
||||||
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
||||||
) -> Result<String, RenderError<'a>> {
|
) -> Result<String, RenderError> {
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
for elem in &body.elements {
|
for elem in &body.elements {
|
||||||
match elem {
|
match elem {
|
||||||
@ -85,7 +83,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
&'a self,
|
&'a self,
|
||||||
tag: &'a DustTag,
|
tag: &'a DustTag,
|
||||||
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
||||||
) -> Result<String, RenderError<'a>> {
|
) -> Result<String, RenderError> {
|
||||||
match tag {
|
match tag {
|
||||||
DustTag::DTComment(_comment) => (),
|
DustTag::DTComment(_comment) => (),
|
||||||
DustTag::DTSpecial(special) => {
|
DustTag::DTSpecial(special) => {
|
||||||
@ -101,21 +99,20 @@ impl<'a> DustRenderer<'a> {
|
|||||||
DustTag::DTReference(reference) => {
|
DustTag::DTReference(reference) => {
|
||||||
let val = walk_path(breadcrumbs, &reference.path.keys);
|
let val = walk_path(breadcrumbs, &reference.path.keys);
|
||||||
match val {
|
match val {
|
||||||
Err(RenderError::NotFound { .. }) => return Ok("".to_owned()),
|
Err(WalkError::CantWalk) => return Ok("".to_owned()),
|
||||||
Ok(final_val) => {
|
Ok(final_val) => {
|
||||||
let loop_elements = final_val.get_loop_elements()?;
|
let loop_elements = final_val.get_loop_elements();
|
||||||
if loop_elements.is_empty() {
|
if loop_elements.is_empty() {
|
||||||
return Ok("".to_owned());
|
return Ok("".to_owned());
|
||||||
} else {
|
} else {
|
||||||
return final_val.render(&reference.filters);
|
return final_val.render(&reference.filters);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(render_error) => return Err(render_error),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DustTag::DTSection(container) => {
|
DustTag::DTSection(container) => {
|
||||||
let val = walk_path(breadcrumbs, &container.path.keys);
|
let val = walk_path(breadcrumbs, &container.path.keys);
|
||||||
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?;
|
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
|
||||||
if loop_elements.is_empty() {
|
if loop_elements.is_empty() {
|
||||||
// Oddly enough if the value is falsey (like
|
// Oddly enough if the value is falsey (like
|
||||||
// an empty array or null), Dust uses the
|
// an empty array or null), Dust uses the
|
||||||
@ -145,7 +142,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
}
|
}
|
||||||
DustTag::DTExists(container) => {
|
DustTag::DTExists(container) => {
|
||||||
let val = walk_path(breadcrumbs, &container.path.keys);
|
let val = walk_path(breadcrumbs, &container.path.keys);
|
||||||
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?;
|
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
|
||||||
if loop_elements.is_empty() {
|
if loop_elements.is_empty() {
|
||||||
return match &container.else_contents {
|
return match &container.else_contents {
|
||||||
Some(body) => self.render_body(&body, breadcrumbs),
|
Some(body) => self.render_body(&body, breadcrumbs),
|
||||||
@ -160,7 +157,7 @@ impl<'a> DustRenderer<'a> {
|
|||||||
}
|
}
|
||||||
DustTag::DTNotExists(container) => {
|
DustTag::DTNotExists(container) => {
|
||||||
let val = walk_path(breadcrumbs, &container.path.keys);
|
let val = walk_path(breadcrumbs, &container.path.keys);
|
||||||
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val)?;
|
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
|
||||||
if !loop_elements.is_empty() {
|
if !loop_elements.is_empty() {
|
||||||
return match &container.else_contents {
|
return match &container.else_contents {
|
||||||
Some(body) => self.render_body(&body, breadcrumbs),
|
Some(body) => self.render_body(&body, breadcrumbs),
|
||||||
@ -196,13 +193,11 @@ impl<'a> DustRenderer<'a> {
|
|||||||
/// block, this will return an empty vector.
|
/// block, this will return an empty vector.
|
||||||
fn get_loop_elements<'b>(
|
fn get_loop_elements<'b>(
|
||||||
&'a self,
|
&'a self,
|
||||||
walk_result: Result<&'b dyn ContextElement, RenderError<'b>>,
|
walk_result: Result<&'b dyn ContextElement, WalkError>,
|
||||||
) -> Result<Vec<&'b dyn ContextElement>, RenderError<'b>> {
|
) -> Vec<&'b dyn ContextElement> {
|
||||||
if let Err(RenderError::NotFound { .. }) = walk_result {
|
match walk_result {
|
||||||
// If reference does not exist in the context, render the else block
|
Err(WalkError::CantWalk) => Vec::new(),
|
||||||
Ok(vec![])
|
Ok(walk_target) => walk_target.get_loop_elements(),
|
||||||
} else {
|
|
||||||
Ok(walk_result?.get_loop_elements()?)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -216,50 +211,46 @@ enum WalkResult<'a> {
|
|||||||
fn walk_path_from_single_level<'a>(
|
fn walk_path_from_single_level<'a>(
|
||||||
context: &'a dyn ContextElement,
|
context: &'a dyn ContextElement,
|
||||||
path: &Vec<&str>,
|
path: &Vec<&str>,
|
||||||
) -> Result<WalkResult<'a>, RenderError<'a>> {
|
) -> WalkResult<'a> {
|
||||||
if path.is_empty() {
|
if path.is_empty() {
|
||||||
return Ok(WalkResult::FullyWalked(context));
|
return WalkResult::FullyWalked(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut walk_failure = WalkResult::NoWalk;
|
let mut walk_failure = WalkResult::NoWalk;
|
||||||
let mut output = context;
|
let mut output = context;
|
||||||
for elem in path.iter() {
|
for elem in path.iter() {
|
||||||
let new_val = output.walk(elem);
|
let new_val = output.walk(elem);
|
||||||
if let Err(RenderError::WontWalk { .. }) = new_val {
|
match output.walk(elem) {
|
||||||
return Ok(walk_failure);
|
Err(WalkError::CantWalk { .. }) => {
|
||||||
} else if let Err(RenderError::CantWalk { .. }) = new_val {
|
return walk_failure;
|
||||||
return Ok(walk_failure);
|
|
||||||
}
|
}
|
||||||
|
Ok(new_val) => {
|
||||||
walk_failure = WalkResult::PartialWalk;
|
walk_failure = WalkResult::PartialWalk;
|
||||||
output = new_val?;
|
output = new_val;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(WalkResult::FullyWalked(output))
|
WalkResult::FullyWalked(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn walk_path<'a>(
|
fn walk_path<'a>(
|
||||||
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
breadcrumbs: &Vec<&'a dyn ContextElement>,
|
||||||
path: &'a Vec<&str>,
|
path: &'a Vec<&str>,
|
||||||
) -> Result<&'a dyn ContextElement, RenderError<'a>> {
|
) -> Result<&'a dyn ContextElement, WalkError> {
|
||||||
for context in breadcrumbs.iter().rev() {
|
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
|
// If no walking was done at all, keep looping
|
||||||
WalkResult::NoWalk => {}
|
WalkResult::NoWalk => {}
|
||||||
// If we partially walked then stop trying to find
|
// If we partially walked then stop trying to find
|
||||||
// anything
|
// anything
|
||||||
WalkResult::PartialWalk => {
|
WalkResult::PartialWalk => {
|
||||||
return Err(RenderError::NotFound {
|
return Err(WalkError::CantWalk);
|
||||||
path: path,
|
|
||||||
breadcrumbs: breadcrumbs.clone(),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
WalkResult::FullyWalked(new_context) => return Ok(new_context),
|
WalkResult::FullyWalked(new_context) => return Ok(new_context),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(RenderError::NotFound {
|
Err(WalkError::CantWalk)
|
||||||
path: path,
|
|
||||||
breadcrumbs: breadcrumbs.clone(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -291,57 +282,48 @@ mod tests {
|
|||||||
impl<I: ContextElement> Renderable for HashMap<&str, I> {
|
impl<I: ContextElement> Renderable for HashMap<&str, I> {
|
||||||
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
|
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
|
||||||
// TODO: handle the filters
|
// TODO: handle the filters
|
||||||
Err(RenderError::CantRender { elem: self })
|
Ok("[object Object]".to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: ContextElement> Walkable for HashMap<&str, I> {
|
impl<I: ContextElement> Walkable for HashMap<&str, I> {
|
||||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
|
||||||
let child = self.get(segment).ok_or(RenderError::WontWalk {
|
let child = self.get(segment).ok_or(WalkError::CantWalk)?;
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
})?;
|
|
||||||
Ok(child)
|
Ok(child)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Walkable for &str {
|
impl Walkable for &str {
|
||||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
|
||||||
Err(RenderError::CantWalk {
|
Err(WalkError::CantWalk)
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Walkable for u32 {
|
impl Walkable for u32 {
|
||||||
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError> {
|
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> {
|
||||||
Err(RenderError::CantWalk {
|
Err(WalkError::CantWalk)
|
||||||
segment: segment.to_string(),
|
|
||||||
elem: self,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Loopable for &str {
|
impl Loopable for &str {
|
||||||
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
if self.is_empty() {
|
if self.is_empty() {
|
||||||
Ok(Vec::new())
|
Vec::new()
|
||||||
} else {
|
} else {
|
||||||
Ok(vec![self])
|
vec![self]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Loopable for u32 {
|
impl Loopable for u32 {
|
||||||
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
Ok(vec![self])
|
vec![self]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: ContextElement> Loopable for HashMap<&str, I> {
|
impl<I: ContextElement> Loopable for HashMap<&str, I> {
|
||||||
fn get_loop_elements(&self) -> Result<Vec<&dyn ContextElement>, RenderError> {
|
fn get_loop_elements(&self) -> Vec<&dyn ContextElement> {
|
||||||
Ok(vec![self])
|
vec![self]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user