From 4cf48a9b44083c16c81ea90795e77bc4b708a520 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Sun, 31 Dec 2023 18:25:37 +0800 Subject: [PATCH] refactor: remove option on RenderErrorReason --- src/error.rs | 40 +++++++++++++------------------ src/helpers/mod.rs | 4 ++-- src/helpers/string_helpers/mod.rs | 3 +-- src/registry.rs | 22 +++++++---------- tests/subexpression.rs | 2 +- 5 files changed, 28 insertions(+), 43 deletions(-) diff --git a/src/error.rs b/src/error.rs index 12b273669..b99b03e23 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,23 +13,20 @@ use walkdir::Error as WalkdirError; use rhai::{EvalAltResult, ParseError}; /// Error when rendering data on template. -#[derive(Debug, Default)] +#[derive(Debug)] pub struct RenderError { pub template_name: Option, pub line_no: Option, pub column_no: Option, - cause: Option>, + cause: Box, unimplemented: bool, // backtrace: Backtrace, } impl fmt::Display for RenderError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { - let desc = self - .cause - .as_ref() - .map(|e| e.to_string()) - .unwrap_or_else(|| "".to_owned()); + let desc = self.cause.to_string(); + match (self.line_no, self.column_no) { (Some(line), Some(col)) => write!( f, @@ -135,6 +132,8 @@ pub enum RenderErrorReason { #[source] ScriptError, ), + #[error("Unimplemented")] + Unimplemented, #[error("{0}")] Other(String), } @@ -142,8 +141,11 @@ pub enum RenderErrorReason { impl From for RenderError { fn from(e: RenderErrorReason) -> RenderError { RenderError { - cause: Some(Box::new(e)), - ..Default::default() + template_name: None, + line_no: None, + column_no: None, + cause: Box::new(e), + unimplemented: false, } } } @@ -154,13 +156,6 @@ impl RenderError { RenderErrorReason::Other(desc.as_ref().to_string()).into() } - pub(crate) fn unimplemented() -> RenderError { - RenderError { - unimplemented: true, - ..Default::default() - } - } - pub fn strict_error(path: Option<&String>) -> RenderError { RenderErrorReason::MissingVariable(path.map(|p| p.to_owned())).into() } @@ -170,26 +165,23 @@ impl RenderError { where E: StdError + Send + Sync + 'static, { - RenderError { - cause: Some(Box::new(RenderErrorReason::NestedError(Box::new(cause)))), - ..Default::default() - } + RenderErrorReason::NestedError(Box::new(cause)).into() } #[inline] pub(crate) fn is_unimplemented(&self) -> bool { - self.unimplemented + matches!(*self.cause, RenderErrorReason::Unimplemented) } /// Get `RenderErrorReason` for this error - pub fn reason(&self) -> Option<&RenderErrorReason> { - self.cause.as_ref().map(|e| e.as_ref()) + pub fn reason(&self) -> &RenderErrorReason { + self.cause.as_ref() } } impl StdError for RenderError { fn source(&self) -> Option<&(dyn StdError + 'static)> { - self.reason().and_then(|e| e.source()) + Some(self.reason()) } } diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index aae1b5192..de69e20e7 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -1,5 +1,5 @@ use crate::context::Context; -use crate::error::RenderError; +use crate::error::{RenderError, RenderErrorReason}; use crate::json::value::ScopedJson; use crate::output::Output; use crate::registry::Registry; @@ -95,7 +95,7 @@ pub trait HelperDef { _: &'rc Context, _: &mut RenderContext<'reg, 'rc>, ) -> Result, RenderError> { - Err(RenderError::unimplemented()) + Err(RenderErrorReason::Unimplemented.into()) } /// A complex version of helper interface. diff --git a/src/helpers/string_helpers/mod.rs b/src/helpers/string_helpers/mod.rs index e31b6d3e3..5b289a31d 100644 --- a/src/helpers/string_helpers/mod.rs +++ b/src/helpers/string_helpers/mod.rs @@ -119,14 +119,13 @@ mod tests { #[test] fn test_invalid_input() { use crate::error::RenderErrorReason; - use std::error::Error; let hbs = crate::registry::Registry::new(); let err = hbs .render_template("{{snakeCase 1}}", &json!({})) .unwrap_err(); assert!(matches!( - err.reason().unwrap(), + err.reason(), RenderErrorReason::ParamTypeMismatchForName(_, _, _) )); } diff --git a/src/registry.rs b/src/registry.rs index b229ba49d..1f4feab63 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -1159,13 +1159,10 @@ mod test { .unwrap_err(); assert_eq!(render_error.column_no.unwrap(), 26); assert_eq!( - render_error - .reason() - .and_then(|e| match e { - RenderErrorReason::MissingVariable(path) => path.to_owned(), - _ => unreachable!(), - }) - .unwrap(), + match render_error.reason() { + RenderErrorReason::MissingVariable(path) => path.as_ref().unwrap(), + _ => unreachable!(), + }, "the_key_never_exists" ); @@ -1181,13 +1178,10 @@ mod test { .unwrap_err(); assert_eq!(render_error2.column_no.unwrap(), 31); assert_eq!( - render_error2 - .reason() - .and_then(|e| match e { - RenderErrorReason::MissingVariable(path) => path.to_owned(), - _ => unreachable!(), - }) - .unwrap(), + match render_error2.reason() { + RenderErrorReason::MissingVariable(path) => path.as_ref().unwrap(), + _ => unreachable!(), + }, "this.[3]" ) } diff --git a/tests/subexpression.rs b/tests/subexpression.rs index 8c3b6acf1..766fe0f90 100644 --- a/tests/subexpression.rs +++ b/tests/subexpression.rs @@ -81,7 +81,7 @@ fn invalid_json_path() { let hbs = Handlebars::new(); let error = hbs.render_template("{{x[]@this}}", &data).unwrap_err(); - let cause = error.reason().unwrap(); + let cause = error.reason(); assert!(matches!(cause, RenderErrorReason::HelperNotFound(_))); }