From 5b8c249785dab14611f33b32e6652101e9cc3c67 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Fri, 3 Nov 2023 22:37:55 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A5=20Major=20Handle's=20improvement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/base/component/block.rs | 18 +------ pagetop/src/base/component/branding.rs | 8 +-- pagetop/src/base/component/flex/container.rs | 18 +------ pagetop/src/base/component/flex/item.rs | 18 +------ pagetop/src/base/component/form/button.rs | 8 +-- pagetop/src/base/component/form/date.rs | 8 +-- pagetop/src/base/component/form/form_main.rs | 18 +------ pagetop/src/base/component/form/hidden.rs | 8 +-- pagetop/src/base/component/form/input.rs | 8 +-- pagetop/src/base/component/heading.rs | 8 +-- pagetop/src/base/component/html.rs | 8 +-- pagetop/src/base/component/icon.rs | 8 +-- pagetop/src/base/component/image.rs | 8 +-- pagetop/src/base/component/menu/element.rs | 8 +-- pagetop/src/base/component/menu/group.rs | 8 +-- pagetop/src/base/component/menu/item.rs | 8 +-- pagetop/src/base/component/menu/megamenu.rs | 8 +-- pagetop/src/base/component/menu/menu_main.rs | 18 +------ pagetop/src/base/component/menu/submenu.rs | 8 +-- pagetop/src/base/component/paragraph.rs | 8 +-- pagetop/src/base/component/powered_by.rs | 8 +-- pagetop/src/base/component/translate.rs | 8 +-- pagetop/src/base/component/wrapper.rs | 18 +------ pagetop/src/base/theme/basic.rs | 8 +-- pagetop/src/base/theme/chassis.rs | 8 +-- pagetop/src/base/theme/inception.rs | 8 +-- pagetop/src/core/component/arc.rs | 10 ++-- pagetop/src/core/component/definition.rs | 6 +-- pagetop/src/core/module/definition.rs | 6 +-- pagetop/src/lib.rs | 11 ++++- pagetop/src/prelude.rs | 4 +- pagetop/src/response/fatal_error/error403.rs | 10 ++-- pagetop/src/response/fatal_error/error404.rs | 10 ++-- pagetop/src/util.rs | 52 ++++++++++++++------ 34 files changed, 114 insertions(+), 263 deletions(-) diff --git a/pagetop/src/base/component/block.rs b/pagetop/src/base/component/block.rs index 0cf9cd1b..c85e2562 100644 --- a/pagetop/src/base/component/block.rs +++ b/pagetop/src/base/component/block.rs @@ -1,9 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_BLOCK); - -actions_for_component!(Block); - #[rustfmt::skip] #[derive(Default)] pub struct Block { @@ -16,15 +12,13 @@ pub struct Block { template : String, } +impl_handle!(COMPONENT_BASE_BLOCK for Block); + impl ComponentTrait for Block { fn new() -> Self { Block::default().with_classes(ClassesOp::Add, "block") } - fn handle(&self) -> Handle { - COMPONENT_BASE_BLOCK - } - fn id(&self) -> Option { self.id.get() } @@ -37,10 +31,6 @@ impl ComponentTrait for Block { (self.renderable.check)(cx) } - fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_block(self, cx); - } - fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { let id = cx.required_id::(self.id()); PrepareMarkup::With(html! { @@ -54,10 +44,6 @@ impl ComponentTrait for Block { } }) } - - fn after_prepare_component(&mut self, cx: &mut Context) { - run_actions_after_prepare_block(self, cx); - } } impl Block { diff --git a/pagetop/src/base/component/branding.rs b/pagetop/src/base/component/branding.rs index 5ae83b8c..c0d59fb0 100644 --- a/pagetop/src/base/component/branding.rs +++ b/pagetop/src/base/component/branding.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_BRANDING); - type SiteLogo = TypedComponent; #[rustfmt::skip] @@ -28,15 +26,13 @@ impl Default for Branding { } } +impl_handle!(COMPONENT_BASE_BRANDING for Branding); + impl ComponentTrait for Branding { fn new() -> Self { Branding::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_BRANDING - } - fn id(&self) -> Option { Some("pt-branding".to_owned()) } diff --git a/pagetop/src/base/component/flex/container.rs b/pagetop/src/base/component/flex/container.rs index 89068fa9..f879101f 100644 --- a/pagetop/src/base/component/flex/container.rs +++ b/pagetop/src/base/component/flex/container.rs @@ -1,9 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_FLEX_CONTAINER); - -actions_for_component!(Container); - #[rustfmt::skip] #[derive(Default)] pub struct Container { @@ -19,15 +15,13 @@ pub struct Container { gap : flex::Gap, } +impl_handle!(COMPONENT_BASE_FLEX_CONTAINER for Container); + impl ComponentTrait for Container { fn new() -> Self { Container::default().with_classes(ClassesOp::Add, flex::Direction::Default.to_string()) } - fn handle(&self) -> Handle { - COMPONENT_BASE_FLEX_CONTAINER - } - fn id(&self) -> Option { self.id.get() } @@ -40,10 +34,6 @@ impl ComponentTrait for Container { (self.renderable.check)(cx) } - fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_container(self, cx); - } - fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { cx.set_param::(PARAM_BASE_INCLUDE_FLEX_ASSETS, true); @@ -58,10 +48,6 @@ impl ComponentTrait for Container { } }) } - - fn after_prepare_component(&mut self, cx: &mut Context) { - run_actions_after_prepare_container(self, cx); - } } impl Container { diff --git a/pagetop/src/base/component/flex/item.rs b/pagetop/src/base/component/flex/item.rs index 0687a547..375f528f 100644 --- a/pagetop/src/base/component/flex/item.rs +++ b/pagetop/src/base/component/flex/item.rs @@ -1,9 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_FLEX_ITEM); - -actions_for_component!(Item); - #[rustfmt::skip] #[derive(Default)] pub struct Item { @@ -20,6 +16,8 @@ pub struct Item { stuff : ArcComponents, } +impl_handle!(COMPONENT_BASE_FLEX_ITEM for Item); + impl ComponentTrait for Item { fn new() -> Self { Item::default() @@ -27,10 +25,6 @@ impl ComponentTrait for Item { .with_inner_classes(ClassesOp::Add, "pt-flex__item-inner") } - fn handle(&self) -> Handle { - COMPONENT_BASE_FLEX_ITEM - } - fn id(&self) -> Option { self.id.get() } @@ -43,10 +37,6 @@ impl ComponentTrait for Item { (self.renderable.check)(cx) } - fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_item(self, cx); - } - fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { let order = match self.weight() { 0 => None, @@ -60,10 +50,6 @@ impl ComponentTrait for Item { } }) } - - fn after_prepare_component(&mut self, cx: &mut Context) { - run_actions_after_prepare_item(self, cx); - } } impl Item { diff --git a/pagetop/src/base/component/form/button.rs b/pagetop/src/base/component/form/button.rs index dddc9afa..3a5be6ef 100644 --- a/pagetop/src/base/component/form/button.rs +++ b/pagetop/src/base/component/form/button.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_BUTTON); - #[derive(Default)] pub enum ButtonType { #[default] @@ -24,15 +22,13 @@ pub struct Button { template : String, } +impl_handle!(COMPONENT_BASE_BUTTON for Button); + impl ComponentTrait for Button { fn new() -> Self { Button::default().with_classes(ClassesOp::Add, "btn btn-primary form-button") } - fn handle(&self) -> Handle { - COMPONENT_BASE_BUTTON - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/form/date.rs b/pagetop/src/base/component/form/date.rs index ed5e11a9..08b47e40 100644 --- a/pagetop/src/base/component/form/date.rs +++ b/pagetop/src/base/component/form/date.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_DATE); - #[rustfmt::skip] #[derive(Default)] pub struct Date { @@ -21,15 +19,13 @@ pub struct Date { template : String, } +impl_handle!(COMPONENT_BASE_DATE for Date); + impl ComponentTrait for Date { fn new() -> Self { Date::default().with_classes(ClassesOp::Add, "form-item form-type-date") } - fn handle(&self) -> Handle { - COMPONENT_BASE_DATE - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/form/form_main.rs b/pagetop/src/base/component/form/form_main.rs index caeeebf2..8f7a0879 100644 --- a/pagetop/src/base/component/form/form_main.rs +++ b/pagetop/src/base/component/form/form_main.rs @@ -1,9 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_FORM); - -actions_for_component!(Form); - #[derive(Default)] pub enum FormMethod { #[default] @@ -25,6 +21,8 @@ pub struct Form { template : String, } +impl_handle!(COMPONENT_BASE_FORM for Form); + impl ComponentTrait for Form { fn new() -> Self { Form::default() @@ -32,10 +30,6 @@ impl ComponentTrait for Form { .with_charset("UTF-8") } - fn handle(&self) -> Handle { - COMPONENT_BASE_FORM - } - fn id(&self) -> Option { self.id.get() } @@ -48,10 +42,6 @@ impl ComponentTrait for Form { (self.renderable.check)(cx) } - fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_form(self, cx); - } - fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { let method = match self.method() { FormMethod::Post => Some("post".to_owned()), @@ -69,10 +59,6 @@ impl ComponentTrait for Form { } }) } - - fn after_prepare_component(&mut self, cx: &mut Context) { - run_actions_after_prepare_form(self, cx); - } } impl Form { diff --git a/pagetop/src/base/component/form/hidden.rs b/pagetop/src/base/component/form/hidden.rs index 3fbef11f..9a809295 100644 --- a/pagetop/src/base/component/form/hidden.rs +++ b/pagetop/src/base/component/form/hidden.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_HIDDEN); - #[rustfmt::skip] #[derive(Default)] pub struct Hidden { @@ -10,15 +8,13 @@ pub struct Hidden { value : OptionString, } +impl_handle!(COMPONENT_BASE_HIDDEN for Hidden); + impl ComponentTrait for Hidden { fn new() -> Self { Hidden::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_HIDDEN - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/form/input.rs b/pagetop/src/base/component/form/input.rs index b3f43c5a..4e8ec54e 100644 --- a/pagetop/src/base/component/form/input.rs +++ b/pagetop/src/base/component/form/input.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_INPUT); - #[derive(Default)] pub enum InputType { #[default] @@ -36,6 +34,8 @@ pub struct Input { template : String, } +impl_handle!(COMPONENT_BASE_INPUT for Input); + impl ComponentTrait for Input { fn new() -> Self { Input::default() @@ -44,10 +44,6 @@ impl ComponentTrait for Input { .with_maxlength(Some(128)) } - fn handle(&self) -> Handle { - COMPONENT_BASE_INPUT - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/heading.rs b/pagetop/src/base/component/heading.rs index fd8546dd..5909796c 100644 --- a/pagetop/src/base/component/heading.rs +++ b/pagetop/src/base/component/heading.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_HEADING); - #[derive(Default)] pub enum HeadingType { #[default] @@ -52,15 +50,13 @@ pub struct Heading { display : HeadingDisplay, } +impl_handle!(COMPONENT_BASE_HEADING for Heading); + impl ComponentTrait for Heading { fn new() -> Self { Heading::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_HEADING - } - fn id(&self) -> Option { self.id.get() } diff --git a/pagetop/src/base/component/html.rs b/pagetop/src/base/component/html.rs index bc4f1e62..7105e715 100644 --- a/pagetop/src/base/component/html.rs +++ b/pagetop/src/base/component/html.rs @@ -1,19 +1,15 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_HTML); - #[derive(Default)] pub struct Html(Markup); +impl_handle!(COMPONENT_BASE_HTML for Html); + impl ComponentTrait for Html { fn new() -> Self { Html::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_HTML - } - fn prepare_component(&self, _cx: &mut Context) -> PrepareMarkup { PrepareMarkup::With(html! { (self.html()) }) } diff --git a/pagetop/src/base/component/icon.rs b/pagetop/src/base/component/icon.rs index afb9660e..4b3fd6f9 100644 --- a/pagetop/src/base/component/icon.rs +++ b/pagetop/src/base/component/icon.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_ICON); - #[rustfmt::skip] #[derive(Default)] pub struct Icon { @@ -12,15 +10,13 @@ pub struct Icon { icon_name : String, } +impl_handle!(COMPONENT_BASE_ICON for Icon); + impl ComponentTrait for Icon { fn new() -> Self { Icon::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_ICON - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/image.rs b/pagetop/src/base/component/image.rs index 393b9bd8..ac096323 100644 --- a/pagetop/src/base/component/image.rs +++ b/pagetop/src/base/component/image.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_IMAGE); - const IMG_FLUID: &str = "pt-img__fluid"; const IMG_FIXED: &str = "pt-img__fixed"; @@ -26,15 +24,13 @@ pub struct Image { size : ImageSize, } +impl_handle!(COMPONENT_BASE_IMAGE for Image); + impl ComponentTrait for Image { fn new() -> Self { Image::default().with_classes(ClassesOp::Add, IMG_FLUID) } - fn handle(&self) -> Handle { - COMPONENT_BASE_IMAGE - } - fn id(&self) -> Option { self.id.get() } diff --git a/pagetop/src/base/component/menu/element.rs b/pagetop/src/base/component/menu/element.rs index d51d1d26..c06d5689 100644 --- a/pagetop/src/base/component/menu/element.rs +++ b/pagetop/src/base/component/menu/element.rs @@ -2,8 +2,6 @@ use crate::prelude::*; use super::Submenu; -new_handle!(COMPONENT_BASE_MENU_ELEMENT); - type Content = TypedComponent; type SubmenuItems = TypedComponent; @@ -25,15 +23,13 @@ pub struct Element { element_type: ElementType, } +impl_handle!(COMPONENT_BASE_MENU_ELEMENT for Element); + impl ComponentTrait for Element { fn new() -> Self { Element::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_MENU_ELEMENT - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/menu/group.rs b/pagetop/src/base/component/menu/group.rs index 7f827025..52181a51 100644 --- a/pagetop/src/base/component/menu/group.rs +++ b/pagetop/src/base/component/menu/group.rs @@ -2,8 +2,6 @@ use crate::prelude::*; use super::Element; -new_handle!(COMPONENT_BASE_MENU_GROUP); - type Elements = TypedComponents; #[rustfmt::skip] @@ -15,15 +13,13 @@ pub struct Group { elements : Elements, } +impl_handle!(COMPONENT_BASE_MENU_GROUP for Group); + impl ComponentTrait for Group { fn new() -> Self { Group::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_MENU_GROUP - } - fn id(&self) -> Option { self.id.get() } diff --git a/pagetop/src/base/component/menu/item.rs b/pagetop/src/base/component/menu/item.rs index 462b90bb..71147c92 100644 --- a/pagetop/src/base/component/menu/item.rs +++ b/pagetop/src/base/component/menu/item.rs @@ -2,8 +2,6 @@ use crate::prelude::*; use super::{Megamenu, Submenu}; -new_handle!(COMPONENT_BASE_MENU_ITEM); - type Label = L10n; type Content = TypedComponent; type SubmenuItems = TypedComponent; @@ -32,15 +30,13 @@ pub struct Item { description: OptionTranslated, } +impl_handle!(COMPONENT_BASE_MENU_ITEM for Item); + impl ComponentTrait for Item { fn new() -> Self { Item::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_MENU_ITEM - } - fn weight(&self) -> Weight { self.weight } diff --git a/pagetop/src/base/component/menu/megamenu.rs b/pagetop/src/base/component/menu/megamenu.rs index 846d7f80..fb0d717a 100644 --- a/pagetop/src/base/component/menu/megamenu.rs +++ b/pagetop/src/base/component/menu/megamenu.rs @@ -2,8 +2,6 @@ use crate::prelude::*; use super::Group; -new_handle!(COMPONENT_BASE_MENU_MEGAMENU); - type Groups = TypedComponents; #[rustfmt::skip] @@ -15,15 +13,13 @@ pub struct Megamenu { groups : Groups, } +impl_handle!(COMPONENT_BASE_MENU_MEGAMENU for Megamenu); + impl ComponentTrait for Megamenu { fn new() -> Self { Megamenu::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_MENU_MEGAMENU - } - fn id(&self) -> Option { self.id.get() } diff --git a/pagetop/src/base/component/menu/menu_main.rs b/pagetop/src/base/component/menu/menu_main.rs index 4190ec99..b429a9df 100644 --- a/pagetop/src/base/component/menu/menu_main.rs +++ b/pagetop/src/base/component/menu/menu_main.rs @@ -2,10 +2,6 @@ use crate::prelude::*; use super::Item; -new_handle!(COMPONENT_BASE_MENU); - -actions_for_component!(Menu); - #[rustfmt::skip] #[derive(Default)] pub struct Menu { @@ -15,15 +11,13 @@ pub struct Menu { items : TypedComponents, } +impl_handle!(COMPONENT_BASE_MENU for Menu); + impl ComponentTrait for Menu { fn new() -> Self { Menu::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_MENU - } - fn id(&self) -> Option { self.id.get() } @@ -36,10 +30,6 @@ impl ComponentTrait for Menu { (self.renderable.check)(cx) } - fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_menu(self, cx); - } - 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); @@ -75,10 +65,6 @@ impl ComponentTrait for Menu { } }) } - - fn after_prepare_component(&mut self, cx: &mut Context) { - run_actions_after_prepare_menu(self, cx); - } } impl Menu { diff --git a/pagetop/src/base/component/menu/submenu.rs b/pagetop/src/base/component/menu/submenu.rs index e5f9c396..ff72d0e8 100644 --- a/pagetop/src/base/component/menu/submenu.rs +++ b/pagetop/src/base/component/menu/submenu.rs @@ -2,8 +2,6 @@ use crate::prelude::*; use super::Item; -new_handle!(COMPONENT_BASE_MENU_SUBMENU); - type Items = TypedComponents; #[rustfmt::skip] @@ -16,15 +14,13 @@ pub struct Submenu { items : Items, } +impl_handle!(COMPONENT_BASE_MENU_SUBMENU for Submenu); + impl ComponentTrait for Submenu { fn new() -> Self { Submenu::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_MENU_SUBMENU - } - fn id(&self) -> Option { self.id.get() } diff --git a/pagetop/src/base/component/paragraph.rs b/pagetop/src/base/component/paragraph.rs index 2470f84f..c939cf0c 100644 --- a/pagetop/src/base/component/paragraph.rs +++ b/pagetop/src/base/component/paragraph.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_PARAGRAPH); - #[rustfmt::skip] #[derive(Default)] pub struct Paragraph { @@ -13,15 +11,13 @@ pub struct Paragraph { stuff : ArcComponents, } +impl_handle!(COMPONENT_BASE_PARAGRAPH for Paragraph); + impl ComponentTrait for Paragraph { fn new() -> Self { Paragraph::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_PARAGRAPH - } - fn id(&self) -> Option { self.id.get() } diff --git a/pagetop/src/base/component/powered_by.rs b/pagetop/src/base/component/powered_by.rs index fc5d81ba..4e87dd70 100644 --- a/pagetop/src/base/component/powered_by.rs +++ b/pagetop/src/base/component/powered_by.rs @@ -1,7 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_POWEREDBY); - #[derive(Default, Eq, PartialEq)] pub enum PoweredByLogo { #[default] @@ -21,6 +19,8 @@ pub struct PoweredBy { logo : PoweredByLogo, } +impl_handle!(COMPONENT_BASE_POWEREDBY for PoweredBy); + impl ComponentTrait for PoweredBy { fn new() -> Self { let year = Utc::now().format("%Y").to_string(); @@ -31,10 +31,6 @@ impl ComponentTrait for PoweredBy { } } - fn handle(&self) -> Handle { - COMPONENT_BASE_POWEREDBY - } - fn id(&self) -> Option { Some("pt-poweredby".to_owned()) } diff --git a/pagetop/src/base/component/translate.rs b/pagetop/src/base/component/translate.rs index 0e658172..dd41c215 100644 --- a/pagetop/src/base/component/translate.rs +++ b/pagetop/src/base/component/translate.rs @@ -1,19 +1,15 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_TRANSLATE); - #[derive(Default)] pub struct Translate(L10n); +impl_handle!(COMPONENT_BASE_TRANSLATE for Translate); + impl ComponentTrait for Translate { fn new() -> Self { Translate::default() } - fn handle(&self) -> Handle { - COMPONENT_BASE_TRANSLATE - } - fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { PrepareMarkup::With(self.l10n().escaped(cx.langid())) } diff --git a/pagetop/src/base/component/wrapper.rs b/pagetop/src/base/component/wrapper.rs index 918b3e33..121af8ed 100644 --- a/pagetop/src/base/component/wrapper.rs +++ b/pagetop/src/base/component/wrapper.rs @@ -1,9 +1,5 @@ use crate::prelude::*; -new_handle!(COMPONENT_BASE_WRAPPER); - -actions_for_component!(Wrapper); - #[derive(Default)] pub enum WrapperType { #[default] @@ -27,6 +23,8 @@ pub struct Wrapper { template : String, } +impl_handle!(COMPONENT_BASE_WRAPPER for Wrapper); + impl ComponentTrait for Wrapper { fn new() -> Self { Wrapper::default() @@ -34,10 +32,6 @@ impl ComponentTrait for Wrapper { .with_inner_classes(ClassesOp::Add, "container") } - fn handle(&self) -> Handle { - COMPONENT_BASE_WRAPPER - } - fn id(&self) -> Option { self.id.get() } @@ -50,10 +44,6 @@ impl ComponentTrait for Wrapper { (self.renderable.check)(cx) } - fn before_prepare_component(&mut self, cx: &mut Context) { - run_actions_before_prepare_wrapper(self, cx); - } - fn prepare_component(&self, cx: &mut Context) -> PrepareMarkup { match self.wrapper_type() { WrapperType::Header => PrepareMarkup::With(html! { @@ -91,10 +81,6 @@ impl ComponentTrait for Wrapper { }), } } - - fn after_prepare_component(&mut self, cx: &mut Context) { - run_actions_after_prepare_wrapper(self, cx); - } } impl Wrapper { diff --git a/pagetop/src/base/theme/basic.rs b/pagetop/src/base/theme/basic.rs index f28a6ebf..0a49642e 100644 --- a/pagetop/src/base/theme/basic.rs +++ b/pagetop/src/base/theme/basic.rs @@ -1,14 +1,10 @@ use crate::prelude::*; -new_handle!(THEME_BASIC); - pub struct Basic; -impl ModuleTrait for Basic { - fn handle(&self) -> Handle { - THEME_BASIC - } +impl_handle!(THEME_BASIC for Basic); +impl ModuleTrait for Basic { fn name(&self) -> L10n { L10n::n("Basic") } diff --git a/pagetop/src/base/theme/chassis.rs b/pagetop/src/base/theme/chassis.rs index 03180b5e..f9da9574 100644 --- a/pagetop/src/base/theme/chassis.rs +++ b/pagetop/src/base/theme/chassis.rs @@ -1,14 +1,10 @@ use crate::prelude::*; -new_handle!(THEME_CHASSIS); - pub struct Chassis; -impl ModuleTrait for Chassis { - fn handle(&self) -> Handle { - THEME_CHASSIS - } +impl_handle!(THEME_CHASSIS for Chassis); +impl ModuleTrait for Chassis { fn name(&self) -> L10n { L10n::n("Chassis") } diff --git a/pagetop/src/base/theme/inception.rs b/pagetop/src/base/theme/inception.rs index f706fe31..1454d5e1 100644 --- a/pagetop/src/base/theme/inception.rs +++ b/pagetop/src/base/theme/inception.rs @@ -1,14 +1,10 @@ use crate::prelude::*; -new_handle!(THEME_INCEPTION); - pub struct Inception; -impl ModuleTrait for Inception { - fn handle(&self) -> Handle { - THEME_INCEPTION - } +impl_handle!(THEME_INCEPTION for Inception); +impl ModuleTrait for Inception { fn name(&self) -> L10n { L10n::n("Inception") } diff --git a/pagetop/src/core/component/arc.rs b/pagetop/src/core/component/arc.rs index f27913b9..c6674c3b 100644 --- a/pagetop/src/core/component/arc.rs +++ b/pagetop/src/core/component/arc.rs @@ -1,22 +1,18 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, Markup}; -use crate::{new_handle, Handle, Weight}; +use crate::{impl_handle, Handle, Weight}; use std::sync::{Arc, RwLock, RwLockReadGuard}; -new_handle!(COMPONENT_NULL for Crate); - #[derive(Default)] struct ComponentNull; +impl_handle!(COMPONENT_NULL for ComponentNull); + impl ComponentTrait for ComponentNull { fn new() -> Self { ComponentNull::default() } - - fn handle(&self) -> Handle { - COMPONENT_NULL - } } #[derive(Clone)] diff --git a/pagetop/src/core/component/definition.rs b/pagetop/src/core/component/definition.rs index 0ef4be30..60b7ab46 100644 --- a/pagetop/src/core/component/definition.rs +++ b/pagetop/src/core/component/definition.rs @@ -1,6 +1,6 @@ use crate::core::component::Context; use crate::html::{html, Markup, PrepareMarkup}; -use crate::{util, Handle, Weight}; +use crate::{util, HasHandle, Weight}; use std::any::Any; @@ -12,13 +12,11 @@ pub trait ComponentBase: Any { fn as_mut_any(&mut self) -> &mut dyn Any; } -pub trait ComponentTrait: ComponentBase + Send + Sync { +pub trait ComponentTrait: ComponentBase + HasHandle + Send + Sync { fn new() -> Self where Self: Sized; - fn handle(&self) -> Handle; - fn name(&self) -> String { util::single_type_name::().to_owned() } diff --git a/pagetop/src/core/module/definition.rs b/pagetop/src/core/module/definition.rs index dd7f9d30..775b4b5a 100644 --- a/pagetop/src/core/module/definition.rs +++ b/pagetop/src/core/module/definition.rs @@ -1,7 +1,7 @@ use crate::core::action::Action; use crate::core::theme::ThemeRef; use crate::locale::L10n; -use crate::{actions, service, util, Handle}; +use crate::{actions, service, util, HasHandle}; #[cfg(feature = "database")] use crate::{db::MigrationItem, migrations}; @@ -13,9 +13,7 @@ pub trait ModuleBase { } /// Los módulos deben implementar este *trait*. -pub trait ModuleTrait: ModuleBase + Send + Sync { - fn handle(&self) -> Handle; - +pub trait ModuleTrait: HasHandle + ModuleBase + Send + Sync { fn name(&self) -> L10n { L10n::n(self.single_name()) } diff --git a/pagetop/src/lib.rs b/pagetop/src/lib.rs index 8dda95ed..c6569696 100644 --- a/pagetop/src/lib.rs +++ b/pagetop/src/lib.rs @@ -44,7 +44,7 @@ //! ```rust //! use pagetop::prelude::*; //! -//! new_handle!(APP_HELLO_WORLD); +//! impl_handle!(APP_HELLO_WORLD for HelloWorld); //! //! struct HelloWorld; //! @@ -119,6 +119,15 @@ pub use once_cell::sync::Lazy as LazyStatic; pub use static_files::Resource as StaticResource; pub type Handle = u64; + +pub trait HasHandle { + fn static_handle() -> Handle + where + Self: Sized; + + fn handle(&self) -> Handle; +} + pub type Weight = i8; pub type HashMapResources = std::collections::HashMap<&'static str, StaticResource>; diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index 440ccd3f..bda89011 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -8,7 +8,7 @@ pub use crate::{Handle, HashMapResources, LazyStatic, Weight}; // Functions and macro helpers. pub use crate::util; -pub use crate::{kv, new_handle}; +pub use crate::{impl_handle, kv}; // MACROS. @@ -20,8 +20,6 @@ pub use crate::new_static_locales; pub use crate::{new_static_files, service_for_static_files}; // crate::core::actions pub use crate::actions; -// crate::base::action::component -pub use crate::actions_for_component; // API. diff --git a/pagetop/src/response/fatal_error/error403.rs b/pagetop/src/response/fatal_error/error403.rs index d21e3b23..aa9cc41a 100644 --- a/pagetop/src/response/fatal_error/error403.rs +++ b/pagetop/src/response/fatal_error/error403.rs @@ -1,20 +1,16 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, PrepareMarkup}; -use crate::{new_handle, Handle}; - -new_handle!(ERROR_403); +use crate::impl_handle; pub struct Error403; +impl_handle!(ERROR_403 for Error403); + impl ComponentTrait for Error403 { fn new() -> Self { Self } - fn handle(&self) -> Handle { - ERROR_403 - } - fn prepare_component(&self, _cx: &mut Context) -> PrepareMarkup { PrepareMarkup::With(html! { div { diff --git a/pagetop/src/response/fatal_error/error404.rs b/pagetop/src/response/fatal_error/error404.rs index e1f7827b..e3a77aac 100644 --- a/pagetop/src/response/fatal_error/error404.rs +++ b/pagetop/src/response/fatal_error/error404.rs @@ -1,20 +1,16 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, PrepareMarkup}; -use crate::{new_handle, Handle}; - -new_handle!(ERROR_404); +use crate::impl_handle; pub struct Error404; +impl_handle!(ERROR_404 for Error404); + impl ComponentTrait for Error404 { fn new() -> Self { Self } - fn handle(&self) -> Handle { - ERROR_404 - } - fn prepare_component(&self, _cx: &mut Context) -> PrepareMarkup { PrepareMarkup::With(html! { div { diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index a2081b15..3cf4301a 100644 --- a/pagetop/src/util.rs +++ b/pagetop/src/util.rs @@ -92,6 +92,44 @@ pub fn absolute_dir( // MACRO HELPERS. // ************************************************************************************************* +#[macro_export] +macro_rules! impl_handle { + ( $HANDLE:ident for $Element:ident ) => { + /// Constant handle to represent a unique PageTop building element. + pub const $HANDLE: $crate::Handle = + $crate::util::handle(module_path!(), file!(), line!(), column!()); + + impl $crate::HasHandle for $Element { + #[inline] + fn static_handle() -> $crate::Handle { + $HANDLE + } + + #[inline] + fn handle(&self) -> $crate::Handle { + $HANDLE + } + } + }; + ( $HANDLE:ident for $Element:ident<$Implement:ident> ) => { + /// Constant handle to represent a unique PageTop building element. + pub const $HANDLE: $crate::Handle = + $crate::util::handle(module_path!(), file!(), line!(), column!()); + + impl $crate::HasHandle for $Element { + #[inline] + fn static_handle() -> $crate::Handle { + $HANDLE + } + + #[inline] + fn handle(&self) -> $crate::Handle { + $HANDLE + } + } + }; +} + #[macro_export] /// Macro para construir grupos de pares clave-valor. /// @@ -111,17 +149,3 @@ macro_rules! kv { a }}; } - -#[macro_export] -macro_rules! new_handle { - ( $HANDLE:ident ) => { - /// Public constant handle to represent a unique PageTop building element. - pub const $HANDLE: $crate::Handle = - $crate::util::handle(module_path!(), file!(), line!(), column!()); - }; - ( $HANDLE:ident for Crate ) => { - /// Local constant handle to represent a unique PageTop building element. - pub(crate) const $HANDLE: $crate::Handle = - $crate::util::handle(module_path!(), file!(), line!(), column!()); - }; -}