diff --git a/src/base/component.rs b/src/base/component.rs index 1aeaa4d1..801f565c 100644 --- a/src/base/component.rs +++ b/src/base/component.rs @@ -27,7 +27,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) { .with_weight(weight), )); - if let Some(true) = cx.get_param::(PARAM_BASE_INCLUDE_ICONS) { + if let Ok(true) = cx.get_param::(PARAM_BASE_INCLUDE_ICONS) { cx.alter_assets(AssetsOp::AddStyleSheet( StyleSheet::at("/base/css/icons.min.css") .with_version("1.11.1") @@ -35,7 +35,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) { )); } - if let Some(true) = cx.get_param::(PARAM_BASE_INCLUDE_FLEX_ASSETS) { + if let Ok(true) = cx.get_param::(PARAM_BASE_INCLUDE_FLEX_ASSETS) { cx.alter_assets(AssetsOp::AddStyleSheet( StyleSheet::at("/base/css/flex.css") .with_version("0.0.1") @@ -43,7 +43,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) { )); } - if let Some(true) = cx.get_param::(PARAM_BASE_INCLUDE_MENU_ASSETS) { + if let Ok(true) = cx.get_param::(PARAM_BASE_INCLUDE_MENU_ASSETS) { cx.alter_assets(AssetsOp::AddStyleSheet( StyleSheet::at("/base/css/menu.css") .with_version("0.0.1") diff --git a/src/core/component.rs b/src/core/component.rs index b6650981..af116771 100644 --- a/src/core/component.rs +++ b/src/core/component.rs @@ -1,5 +1,5 @@ mod context; -pub use context::{AssetsOp, Context}; +pub use context::{AssetsOp, Context, ParamError}; pub type FnContextualPath = fn(cx: &Context) -> &str; mod definition; diff --git a/src/core/component/context.rs b/src/core/component/context.rs index dd68dd41..37531e44 100644 --- a/src/core/component/context.rs +++ b/src/core/component/context.rs @@ -10,6 +10,8 @@ use crate::service::HttpRequest; use crate::util::TypeInfo; use std::collections::HashMap; +use std::error::Error; +use std::fmt; use std::str::FromStr; pub enum AssetsOp { @@ -32,6 +34,23 @@ pub enum AssetsOp { AddBaseAssets, } +#[derive(Debug)] +pub enum ParamError { + NotFound, + ParseError(String), +} + +impl fmt::Display for ParamError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + ParamError::NotFound => write!(f, "Parameter not found"), + ParamError::ParseError(e) => write!(f, "Parse error: {}", e), + } + } +} + +impl Error for ParamError {} + #[rustfmt::skip] pub struct Context { request : HttpRequest, @@ -134,13 +153,11 @@ impl Context { &self.regions } - pub fn get_param(&mut self, key: &'static str) -> Option { - if let Some(value) = self.params.get(key) { - if let Ok(value) = T::from_str(value) { - return Some(value); - } + pub fn get_param(&self, key: &'static str) -> Result { + match self.params.get(key) { + Some(value) => T::from_str(value).map_err(|_| ParamError::ParseError(value.clone())), + None => Err(ParamError::NotFound), } - None } /// Context PREPARE.