🩹 Fix param getter with idiomatic error handling
This commit is contained in:
parent
53b97c728c
commit
b5fc06e84e
3 changed files with 27 additions and 10 deletions
|
|
@ -27,7 +27,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) {
|
|||
.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(
|
||||
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::<bool>(PARAM_BASE_INCLUDE_FLEX_ASSETS) {
|
||||
if let Ok(true) = cx.get_param::<bool>(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::<bool>(PARAM_BASE_INCLUDE_MENU_ASSETS) {
|
||||
if let Ok(true) = cx.get_param::<bool>(PARAM_BASE_INCLUDE_MENU_ASSETS) {
|
||||
cx.alter_assets(AssetsOp::AddStyleSheet(
|
||||
StyleSheet::at("/base/css/menu.css")
|
||||
.with_version("0.0.1")
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<T: FromStr + ToString>(&mut self, key: &'static str) -> Option<T> {
|
||||
if let Some(value) = self.params.get(key) {
|
||||
if let Ok(value) = T::from_str(value) {
|
||||
return Some(value);
|
||||
}
|
||||
pub fn get_param<T: FromStr + ToString>(&self, key: &'static str) -> Result<T, ParamError> {
|
||||
match self.params.get(key) {
|
||||
Some(value) => T::from_str(value).map_err(|_| ParamError::ParseError(value.clone())),
|
||||
None => Err(ParamError::NotFound),
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Context PREPARE.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue