From b481d84cbad10a5d2522f4282bd8018841db692a Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 27 Jul 2024 22:07:26 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Json=20serialization=20for=20?= =?UTF-8?q?context=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 1 + src/base/component/flex/container.rs | 2 +- src/base/component/menu/menu_main.rs | 4 +- src/core/component/context.rs | 84 +++++++++++++++------------- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 08fe7df9..db27dd98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ static-files = "0.2.3" pagetop-macros = { version = "0.0", path = "helpers/pagetop-macros" } serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" [dependencies.futures] version = "0.3.30" diff --git a/src/base/component/flex/container.rs b/src/base/component/flex/container.rs index 0c2cbb18..0b095c30 100644 --- a/src/base/component/flex/container.rs +++ b/src/base/component/flex/container.rs @@ -47,7 +47,7 @@ impl ComponentTrait for Container { .join(" "), ); - cx.set_param::(PARAM_BASE_INCLUDE_FLEX_ASSETS, true); + cx.set_param::(PARAM_BASE_INCLUDE_FLEX_ASSETS, &true); } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { diff --git a/src/base/component/menu/menu_main.rs b/src/base/component/menu/menu_main.rs index 0ecbceee..94d51d8d 100644 --- a/src/base/component/menu/menu_main.rs +++ b/src/base/component/menu/menu_main.rs @@ -19,8 +19,8 @@ impl ComponentTrait for Menu { } fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { - cx.set_param::(PARAM_BASE_INCLUDE_MENU_ASSETS, true); - cx.set_param::(PARAM_BASE_INCLUDE_ICONS, true); + cx.set_param::(PARAM_BASE_INCLUDE_MENU_ASSETS, &true); + cx.set_param::(PARAM_BASE_INCLUDE_ICONS, &true); PrepareMarkup::With(html! { div id=[self.id()] class="menu__container" { diff --git a/src/core/component/context.rs b/src/core/component/context.rs index bc8925bc..80ab4f12 100644 --- a/src/core/component/context.rs +++ b/src/core/component/context.rs @@ -1,5 +1,4 @@ use crate::base::component::add_base_assets; -use crate::concat_string; use crate::core::component::AnyOp; use crate::core::theme::all::{theme_by_short_name, THEME_DEFAULT}; use crate::core::theme::{ComponentsInRegions, ThemeRef}; @@ -8,11 +7,16 @@ use crate::html::{Assets, HeadScript, HeadStyles, JavaScript, StyleSheet}; use crate::locale::{LanguageIdentifier, LANGID_DEFAULT}; use crate::service::HttpRequest; use crate::util::TypeInfo; +use crate::{concat_string, trace}; + +use serde::de::DeserializeOwned; +use serde::Serialize; +use serde_json as json; use std::collections::HashMap; use std::error::Error; + use std::fmt; -use std::str::FromStr; pub enum AssetsOp { LangId(&'static LanguageIdentifier), @@ -37,7 +41,7 @@ pub enum AssetsOp { #[derive(Debug)] pub enum ParamError { NotFound, - ParseError(String), + ParseError(json::Error), } impl fmt::Display for ParamError { @@ -53,17 +57,17 @@ impl Error for ParamError {} #[rustfmt::skip] pub struct Context { - request : HttpRequest, - langid : &'static LanguageIdentifier, - theme : ThemeRef, - layout : &'static str, - stylesheet : Assets, // Stylesheets. - headstyles : Assets, // Styles in head. - javascript : Assets, // JavaScripts. - headscript : Assets, // Scripts in head. - regions : ComponentsInRegions, - params : HashMap<&'static str, String>, - id_counter : usize, + request : HttpRequest, + langid : &'static LanguageIdentifier, + theme : ThemeRef, + layout : &'static str, + stylesheet: Assets, // Stylesheets. + headstyles: Assets, // Styles in head. + javascript: Assets, // JavaScripts. + headscript: Assets, // Scripts in head. + regions : ComponentsInRegions, + params : HashMap<&'static str, json::Value>, + id_counter: usize, } impl Context { @@ -71,16 +75,16 @@ impl Context { pub(crate) fn new(request: HttpRequest) -> Self { Context { request, - langid : &LANGID_DEFAULT, - theme : *THEME_DEFAULT, - layout : "default", - stylesheet : Assets::::new(), // Stylesheets. - headstyles : Assets::::new(), // Styles in head. - javascript : Assets::::new(), // JavaScripts. - headscript : Assets::::new(), // Scripts in head. - regions : ComponentsInRegions::default(), - params : HashMap::<&str, String>::new(), - id_counter : 0, + langid : &LANGID_DEFAULT, + theme : *THEME_DEFAULT, + layout : "default", + stylesheet: Assets::::new(), // Stylesheets. + headstyles: Assets::::new(), // Styles in head. + javascript: Assets::::new(), // JavaScripts. + headscript: Assets::::new(), // Scripts in head. + regions : ComponentsInRegions::default(), + params : HashMap::<&str, json::Value>::new(), + id_counter: 0, } } @@ -121,17 +125,17 @@ impl Context { self } - pub fn set_param(&mut self, key: &'static str, value: T) -> &mut Self { - self.params.insert(key, value.to_string()); + pub fn set_param(&mut self, key: &'static str, value: &T) -> &mut Self { + json::to_value(value).map_or_else( + |e| trace::error!("Serialization failed for param {key}: {e}"), + |v| { + self.params.insert(key, v); + }, + ); self } - pub fn remove_param(&mut self, key: &'static str) -> &mut Self { - self.params.remove(key); - self - } - - /// Context GETTERS. + // Context GETTERS. pub fn request(&self) -> &HttpRequest { &self.request @@ -153,14 +157,14 @@ impl Context { &self.regions } - 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), - } + pub fn get_param(&self, key: &'static str) -> Result { + self.params + .get(key) + .ok_or(ParamError::NotFound) + .and_then(|v| json::from_value(v.clone()).map_err(ParamError::ParseError)) } - /// Context PREPARE. + // Context PREPARE. pub(crate) fn prepare_assets(&mut self) -> Markup { html! { @@ -179,6 +183,10 @@ impl Context { // Context EXTRAS. + pub fn remove_param(&mut self, key: &'static str) -> bool { + self.params.remove(key).is_some() + } + pub fn required_id(&mut self, id: Option) -> String { if let Some(id) = id { id