🩹 Fix param getter with idiomatic error handling

This commit is contained in:
Manuel Cillero 2024-04-28 23:59:38 +02:00
parent 53b97c728c
commit b5fc06e84e
3 changed files with 27 additions and 10 deletions

View file

@ -27,7 +27,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) {
.with_weight(weight), .with_weight(weight),
)); ));
if let Some(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_ICONS) { if let Ok(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_ICONS) {
cx.alter_assets(AssetsOp::AddStyleSheet( cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/icons.min.css") StyleSheet::at("/base/css/icons.min.css")
.with_version("1.11.1") .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::<bool>(PARAM_BASE_INCLUDE_FLEX_ASSETS) { if let Ok(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_FLEX_ASSETS) {
cx.alter_assets(AssetsOp::AddStyleSheet( cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/flex.css") StyleSheet::at("/base/css/flex.css")
.with_version("0.0.1") .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::<bool>(PARAM_BASE_INCLUDE_MENU_ASSETS) { if let Ok(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_MENU_ASSETS) {
cx.alter_assets(AssetsOp::AddStyleSheet( cx.alter_assets(AssetsOp::AddStyleSheet(
StyleSheet::at("/base/css/menu.css") StyleSheet::at("/base/css/menu.css")
.with_version("0.0.1") .with_version("0.0.1")

View file

@ -1,5 +1,5 @@
mod context; mod context;
pub use context::{AssetsOp, Context}; pub use context::{AssetsOp, Context, ParamError};
pub type FnContextualPath = fn(cx: &Context) -> &str; pub type FnContextualPath = fn(cx: &Context) -> &str;
mod definition; mod definition;

View file

@ -10,6 +10,8 @@ use crate::service::HttpRequest;
use crate::util::TypeInfo; use crate::util::TypeInfo;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::str::FromStr; use std::str::FromStr;
pub enum AssetsOp { pub enum AssetsOp {
@ -32,6 +34,23 @@ pub enum AssetsOp {
AddBaseAssets, 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] #[rustfmt::skip]
pub struct Context { pub struct Context {
request : HttpRequest, request : HttpRequest,
@ -134,13 +153,11 @@ impl Context {
&self.regions &self.regions
} }
pub fn get_param<T: FromStr + ToString>(&mut self, key: &'static str) -> Option<T> { pub fn get_param<T: FromStr + ToString>(&self, key: &'static str) -> Result<T, ParamError> {
if let Some(value) = self.params.get(key) { match self.params.get(key) {
if let Ok(value) = T::from_str(value) { Some(value) => T::from_str(value).map_err(|_| ParamError::ParseError(value.clone())),
return Some(value); None => Err(ParamError::NotFound),
}
} }
None
} }
/// Context PREPARE. /// Context PREPARE.