diff --git a/pagetop-admin/src/lib.rs b/pagetop-admin/src/lib.rs index ddd03cb1..4ddd3279 100644 --- a/pagetop-admin/src/lib.rs +++ b/pagetop-admin/src/lib.rs @@ -1,6 +1,6 @@ use pagetop::prelude::*; -pub const MODULE_ADMIN: &str = "pagetop-admin::module::admin"; +pub_const_handler!(MODULE_ADMIN); localize!("src/locales"); @@ -9,7 +9,7 @@ mod summary; pub struct Admin; impl ModuleTrait for Admin { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { MODULE_ADMIN } diff --git a/pagetop-node/src/lib.rs b/pagetop-node/src/lib.rs index 343a467d..ff4755fd 100644 --- a/pagetop-node/src/lib.rs +++ b/pagetop-node/src/lib.rs @@ -1,6 +1,6 @@ use pagetop::prelude::*; -pub const MODULE_NODE: &str = "pagetop-node::module::node"; +pub_const_handler!(MODULE_NODE); localize!("src/locales"); @@ -10,7 +10,7 @@ mod migration; pub struct Node; impl ModuleTrait for Node { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { MODULE_NODE } diff --git a/pagetop-user/src/lib.rs b/pagetop-user/src/lib.rs index ee49fa8d..22c14c41 100644 --- a/pagetop-user/src/lib.rs +++ b/pagetop-user/src/lib.rs @@ -1,6 +1,6 @@ use pagetop::prelude::*; -pub const MODULE_USER: &str = "pagetop-user::module::user"; +pub_const_handler!(MODULE_USER); localize!("src/locales"); @@ -9,7 +9,7 @@ mod migration; pub struct User; impl ModuleTrait for User { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { MODULE_USER } diff --git a/pagetop/src/base/component/anchor.rs b/pagetop/src/base/component/anchor.rs index 22ab34a0..78febf27 100644 --- a/pagetop/src/base/component/anchor.rs +++ b/pagetop/src/base/component/anchor.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_ANCHOR: &str = "pagetop::component::anchor"; +pub_const_handler!(COMPONENT_ANCHOR); pub enum AnchorType { Button, @@ -49,7 +49,7 @@ impl ComponentTrait for Anchor { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_ANCHOR } diff --git a/pagetop/src/base/component/block.rs b/pagetop/src/base/component/block.rs index 4b77ef31..9b0636c1 100644 --- a/pagetop/src/base/component/block.rs +++ b/pagetop/src/base/component/block.rs @@ -1,11 +1,8 @@ use crate::prelude::*; -pub const COMPONENT_BLOCK: &str = "pagetop::component::block"; +pub_const_handler!(COMPONENT_BLOCK); -hook_before_render_component!( - HOOK_BEFORE_RENDER_BLOCK = "pagetop::action::before_render_block", - Block -); +hook_before_render_component!(HOOK_BEFORE_RENDER_BLOCK, Block); pub struct Block { weight : isize, @@ -30,7 +27,7 @@ impl ComponentTrait for Block { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_BLOCK } diff --git a/pagetop/src/base/component/container.rs b/pagetop/src/base/component/container.rs index 70271eb1..ad7be745 100644 --- a/pagetop/src/base/component/container.rs +++ b/pagetop/src/base/component/container.rs @@ -1,11 +1,8 @@ use crate::prelude::*; -pub const COMPONENT_CONTAINER: &str = "pagetop::component::container"; +pub_const_handler!(COMPONENT_CONTAINER); -hook_before_render_component!( - HOOK_BEFORE_RENDER_CONTAINER = "pagetop::action::before_render_container", - Container -); +hook_before_render_component!(HOOK_BEFORE_RENDER_CONTAINER, Container); pub enum ContainerType { Header, @@ -40,7 +37,7 @@ impl ComponentTrait for Container { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_CONTAINER } diff --git a/pagetop/src/base/component/form_element/button.rs b/pagetop/src/base/component/form_element/button.rs index cddb6bdb..f349ea5a 100644 --- a/pagetop/src/base/component/form_element/button.rs +++ b/pagetop/src/base/component/form_element/button.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_BUTTON: &str = "pagetop::component::form::button"; +pub_const_handler!(COMPONENT_BUTTON); pub enum ButtonType { Button, @@ -36,7 +36,7 @@ impl ComponentTrait for Button { .with_classes(ClassesOp::AddFirst, "form-button") } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_BUTTON } diff --git a/pagetop/src/base/component/form_element/date.rs b/pagetop/src/base/component/form_element/date.rs index 19370e57..2ce8c357 100644 --- a/pagetop/src/base/component/form_element/date.rs +++ b/pagetop/src/base/component/form_element/date.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_DATE: &str = "pagetop::component::form::date"; +pub_const_handler!(COMPONENT_DATE); pub struct Date { weight : isize, @@ -40,7 +40,7 @@ impl ComponentTrait for Date { .with_classes(ClassesOp::AddFirst, "form-type-date") } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_DATE } diff --git a/pagetop/src/base/component/form_element/form.rs b/pagetop/src/base/component/form_element/form.rs index 8b562f56..9c80c05a 100644 --- a/pagetop/src/base/component/form_element/form.rs +++ b/pagetop/src/base/component/form_element/form.rs @@ -1,11 +1,8 @@ use crate::prelude::*; -pub const COMPONENT_FORM: &str = "pagetop::component::form"; +pub_const_handler!(COMPONENT_FORM); -hook_before_render_component!( - HOOK_BEFORE_RENDER_FORM = "pagetop::action::before_render_form", - Form -); +hook_before_render_component!(HOOK_BEFORE_RENDER_FORM, Form); pub enum FormMethod { Get, @@ -39,7 +36,7 @@ impl ComponentTrait for Form { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_FORM } diff --git a/pagetop/src/base/component/form_element/hidden.rs b/pagetop/src/base/component/form_element/hidden.rs index 3da6a4c3..ed24a56f 100644 --- a/pagetop/src/base/component/form_element/hidden.rs +++ b/pagetop/src/base/component/form_element/hidden.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_HIDDEN: &str = "pagetop::component::form::hidden"; +pub_const_handler!(COMPONENT_HIDDEN); pub struct Hidden { weight: isize, @@ -17,7 +17,7 @@ impl ComponentTrait for Hidden { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_HIDDEN } diff --git a/pagetop/src/base/component/form_element/input.rs b/pagetop/src/base/component/form_element/input.rs index 8d56b739..b88d4902 100644 --- a/pagetop/src/base/component/form_element/input.rs +++ b/pagetop/src/base/component/form_element/input.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_INPUT: &str = "pagetop::component::form::input"; +pub_const_handler!(COMPONENT_INPUT); pub enum InputType { Email, @@ -57,7 +57,7 @@ impl ComponentTrait for Input { .with_classes(ClassesOp::AddFirst, "form-type-textfield") } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_INPUT } diff --git a/pagetop/src/base/component/grid/column.rs b/pagetop/src/base/component/grid/column.rs index d0b0e2ed..57b2e51a 100644 --- a/pagetop/src/base/component/grid/column.rs +++ b/pagetop/src/base/component/grid/column.rs @@ -1,11 +1,8 @@ use crate::prelude::*; -pub const COMPONENT_COLUMN: &str = "pagetop::component::grid::column"; +pub_const_handler!(COMPONENT_COLUMN); -hook_before_render_component!( - HOOK_BEFORE_RENDER_COLUMN = "pagetop::action::before_render_column", - Column -); +hook_before_render_component!(HOOK_BEFORE_RENDER_COLUMN, Column); const SIZE_DEFAULT: &str = "col-md"; const SIZE_1_OF_12: &str = "col-md-1"; @@ -59,7 +56,7 @@ impl ComponentTrait for Column { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_COLUMN } diff --git a/pagetop/src/base/component/grid/row.rs b/pagetop/src/base/component/grid/row.rs index 117cf499..e92dfc3d 100644 --- a/pagetop/src/base/component/grid/row.rs +++ b/pagetop/src/base/component/grid/row.rs @@ -1,11 +1,8 @@ use crate::prelude::*; -pub const COMPONENT_ROW: &str = "pagetop::component::grid::row"; +pub_const_handler!(COMPONENT_ROW); -hook_before_render_component!( - HOOK_BEFORE_RENDER_ROW = "pagetop::action::before_render_row", - Row -); +hook_before_render_component!(HOOK_BEFORE_RENDER_ROW, Row); pub struct Row { weight : isize, @@ -28,7 +25,7 @@ impl ComponentTrait for Row { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_ROW } diff --git a/pagetop/src/base/component/heading.rs b/pagetop/src/base/component/heading.rs index 0dbbcdea..5d3e7652 100644 --- a/pagetop/src/base/component/heading.rs +++ b/pagetop/src/base/component/heading.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_HEADING: &str = "pagetop::component::heading"; +pub_const_handler!(COMPONENT_HEADING); pub enum HeadingType { H1, @@ -46,7 +46,7 @@ impl ComponentTrait for Heading { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_HEADING } diff --git a/pagetop/src/base/component/html.rs b/pagetop/src/base/component/html.rs index 438684a2..69e9e78d 100644 --- a/pagetop/src/base/component/html.rs +++ b/pagetop/src/base/component/html.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_HTML: &str = "pagetop::component::html"; +pub_const_handler!(COMPONENT_HTML); pub struct Html { weight : isize, @@ -19,7 +19,7 @@ impl ComponentTrait for Html { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_HTML } diff --git a/pagetop/src/base/component/icon.rs b/pagetop/src/base/component/icon.rs index 4722fe63..8bb64e3d 100644 --- a/pagetop/src/base/component/icon.rs +++ b/pagetop/src/base/component/icon.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_ICON: &str = "pagetop::component::icon"; +pub_const_handler!(COMPONENT_ICON); pub struct Icon { weight : isize, @@ -19,7 +19,7 @@ impl ComponentTrait for Icon { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_ICON } diff --git a/pagetop/src/base/component/image.rs b/pagetop/src/base/component/image.rs index 06749f5b..4f8fadac 100644 --- a/pagetop/src/base/component/image.rs +++ b/pagetop/src/base/component/image.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_IMAGE: &str = "pagetop::component::image"; +pub_const_handler!(COMPONENT_IMAGE); pub struct Image { weight : isize, @@ -23,7 +23,7 @@ impl ComponentTrait for Image { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_IMAGE } diff --git a/pagetop/src/base/component/menu.rs b/pagetop/src/base/component/menu.rs index acae967a..6727b3f6 100644 --- a/pagetop/src/base/component/menu.rs +++ b/pagetop/src/base/component/menu.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_MENUITEM: &str = "pagetop::component::menu_item"; +pub_const_handler!(COMPONENT_MENUITEM); pub enum MenuItemType { Label(String), @@ -29,7 +29,7 @@ impl ComponentTrait for MenuItem { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_MENUITEM } @@ -163,12 +163,9 @@ impl MenuItem { // Menu. -pub const COMPONENT_MENU: &str = "pagetop::component::menu"; +pub_const_handler!(COMPONENT_MENU); -hook_before_render_component!( - HOOK_BEFORE_RENDER_MENU = "pagetop::action::before_render_menu", - Menu -); +hook_before_render_component!(HOOK_BEFORE_RENDER_MENU, Menu); pub struct Menu { weight : isize, @@ -191,7 +188,7 @@ impl ComponentTrait for Menu { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_MENU } diff --git a/pagetop/src/base/component/paragraph.rs b/pagetop/src/base/component/paragraph.rs index 06438b7b..bb7af142 100644 --- a/pagetop/src/base/component/paragraph.rs +++ b/pagetop/src/base/component/paragraph.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const COMPONENT_PARAGRAPH: &str = "pagetop::component::paragraph"; +pub_const_handler!(COMPONENT_PARAGRAPH); pub enum ParagraphDisplay { XxLarge, @@ -34,7 +34,7 @@ impl ComponentTrait for Paragraph { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { COMPONENT_PARAGRAPH } diff --git a/pagetop/src/base/module/homepage.rs b/pagetop/src/base/module/homepage.rs index c8635dcf..2676096a 100644 --- a/pagetop/src/base/module/homepage.rs +++ b/pagetop/src/base/module/homepage.rs @@ -1,13 +1,13 @@ use crate::prelude::*; -pub const MODULE_DEFAULT_HOMEPAGE: &str = "pagetop::module::homepage::default"; +pub_const_handler!(MODULE_DEFAULT_HOMEPAGE); localize!("src/base/module/homepage/locales"); pub struct DefaultHomePage; impl ModuleTrait for DefaultHomePage { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { MODULE_DEFAULT_HOMEPAGE } diff --git a/pagetop/src/base/theme/aliner.rs b/pagetop/src/base/theme/aliner.rs index bb6e794f..33b2a453 100644 --- a/pagetop/src/base/theme/aliner.rs +++ b/pagetop/src/base/theme/aliner.rs @@ -1,13 +1,13 @@ use crate::prelude::*; -pub const THEME_ALINER: &str = "pagetop::theme::aliner"; +pub_const_handler!(THEME_ALINER); include!(concat!(env!("OUT_DIR"), "/aliner.rs")); pub struct Aliner; impl ThemeTrait for Aliner { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { THEME_ALINER } diff --git a/pagetop/src/base/theme/bootsier.rs b/pagetop/src/base/theme/bootsier.rs index 6dcf5fd0..6edec2e1 100644 --- a/pagetop/src/base/theme/bootsier.rs +++ b/pagetop/src/base/theme/bootsier.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub const THEME_BOOTSIER: &str = "pagetop::theme::bootsier"; +pub_const_handler!(THEME_BOOTSIER); include!(concat!(env!("OUT_DIR"), "/bootsier.rs")); @@ -9,7 +9,7 @@ localize!("src/base/theme/bootsier/locales"); pub struct Bootsier; impl ThemeTrait for Bootsier { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { THEME_BOOTSIER } diff --git a/pagetop/src/base/theme/bulmix.rs b/pagetop/src/base/theme/bulmix.rs index 200c2b63..b0f81c4e 100644 --- a/pagetop/src/base/theme/bulmix.rs +++ b/pagetop/src/base/theme/bulmix.rs @@ -1,13 +1,13 @@ use crate::prelude::*; -pub const THEME_BULMIX: &str = "pagetop::theme::bulmix"; +pub_const_handler!(THEME_BULMIX); include!(concat!(env!("OUT_DIR"), "/bulmix.rs")); pub struct Bulmix; impl ThemeTrait for Bulmix { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { THEME_BULMIX } diff --git a/pagetop/src/base/theme/minimal.rs b/pagetop/src/base/theme/minimal.rs index bcdbe8cf..63c90976 100644 --- a/pagetop/src/base/theme/minimal.rs +++ b/pagetop/src/base/theme/minimal.rs @@ -1,11 +1,11 @@ use crate::prelude::*; -pub const THEME_MINIMAL: &str = "pagetop::theme::minimal"; +pub_const_handler!(THEME_MINIMAL); pub struct Minimal; impl ThemeTrait for Minimal { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { THEME_MINIMAL } } diff --git a/pagetop/src/core/component/definition.rs b/pagetop/src/core/component/definition.rs index d43b8f70..0c69f90e 100644 --- a/pagetop/src/core/component/definition.rs +++ b/pagetop/src/core/component/definition.rs @@ -1,6 +1,6 @@ use super::InContext; use crate::html::{html, Markup}; -use crate::util; +use crate::util::{single_type_name, Handler}; pub use std::any::Any as AnyComponent; @@ -13,10 +13,10 @@ pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync { where Self: Sized; - fn handler(&self) -> &'static str; + fn handler(&self) -> Handler; fn name(&self) -> String { - util::single_type_name::().to_owned() + single_type_name::().to_owned() } fn description(&self) -> Option { @@ -73,9 +73,9 @@ pub fn component_mut(component: &mut dyn ComponentTrait) -> &mut C { #[macro_export] macro_rules! hook_before_render_component { - ( $ACTION_HANDLER:ident = $handler:literal, $Component:ty ) => { + ( $ACTION_HANDLER:ident, $Component:ty ) => { paste::paste! { - const $ACTION_HANDLER: &str = $handler; + $crate::pub_const_handler!($ACTION_HANDLER); type Action = fn(&$Component, &mut InContext); @@ -92,7 +92,7 @@ macro_rules! hook_before_render_component { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { $ACTION_HANDLER } diff --git a/pagetop/src/core/hook/all.rs b/pagetop/src/core/hook/all.rs index ab562311..e98d5ca3 100644 --- a/pagetop/src/core/hook/all.rs +++ b/pagetop/src/core/hook/all.rs @@ -1,29 +1,30 @@ use super::{HookAction, ActionsHolder}; use crate::LazyStatic; +use crate::util::Handler; use std::collections::HashMap; use std::sync::RwLock; // Registered actions. -static ACTIONS: LazyStatic>> = LazyStatic::new(|| +static ACTIONS: LazyStatic>> = LazyStatic::new(|| RwLock::new(HashMap::new()) ); pub fn add_action(action: HookAction) { let mut actions = ACTIONS.write().unwrap(); let action_handler = action.handler(); - if let Some(holder) = actions.get_mut(action_handler) { + if let Some(holder) = actions.get_mut(&action_handler) { holder.add(action); } else { actions.insert(action_handler, ActionsHolder::new_with(action)); } } -pub fn run_actions(action_handler: &str, f: F) +pub fn run_actions(action_handler: Handler, f: F) where F: FnMut(&HookAction) -> B, { - if let Some(holder) = ACTIONS.read().unwrap().get(action_handler) { + if let Some(holder) = ACTIONS.read().unwrap().get(&action_handler) { holder.iter_map(f) } } diff --git a/pagetop/src/core/hook/definition.rs b/pagetop/src/core/hook/definition.rs index ae739fa6..5808b6c9 100644 --- a/pagetop/src/core/hook/definition.rs +++ b/pagetop/src/core/hook/definition.rs @@ -1,3 +1,5 @@ +use crate::util::Handler; + pub use std::any::Any as AnyHookAction; pub trait HookActionTrait: AnyHookAction + Send + Sync { @@ -5,7 +7,7 @@ pub trait HookActionTrait: AnyHookAction + Send + Sync { where Self: Sized; - fn handler(&self) -> &'static str; + fn handler(&self) -> Handler; fn weight(&self) -> isize { 0 diff --git a/pagetop/src/core/module/definition.rs b/pagetop/src/core/module/definition.rs index 59aec809..e4492ab7 100644 --- a/pagetop/src/core/module/definition.rs +++ b/pagetop/src/core/module/definition.rs @@ -1,5 +1,6 @@ +use crate::app; use crate::core::hook::HookAction; -use crate::{app, util}; +use crate::util::{single_type_name, Handler}; #[cfg(feature = "database")] use crate::db::MigrationItem; @@ -10,7 +11,7 @@ pub trait BaseModule { /// Los módulos deben implementar este *trait*. pub trait ModuleTrait: BaseModule + Send + Sync { - fn handler(&self) -> &'static str; + fn handler(&self) -> Handler; fn name(&self) -> String { self.single_name().to_owned() @@ -40,6 +41,6 @@ pub trait ModuleTrait: BaseModule + Send + Sync { impl BaseModule for M { fn single_name(&self) -> &'static str { - util::single_type_name::() + single_type_name::() } } diff --git a/pagetop/src/core/theme/definition.rs b/pagetop/src/core/theme/definition.rs index 7f64258b..eeaaad2d 100644 --- a/pagetop/src/core/theme/definition.rs +++ b/pagetop/src/core/theme/definition.rs @@ -1,10 +1,11 @@ use crate::app; use crate::base::component::{Container, Html}; +use crate::concat_string; use crate::config::SETTINGS; use crate::core::component::{ComponentTrait, InContext, InContextOp}; use crate::html::{html, Favicon, Markup}; use crate::response::page::Page; -use crate::{concat_string, util}; +use crate::util::{single_type_name, Handler}; pub trait BaseTheme { fn single_name(&self) -> &'static str; @@ -12,7 +13,7 @@ pub trait BaseTheme { /// Los temas deben implementar este "trait". pub trait ThemeTrait: BaseTheme + Send + Sync { - fn handler(&self) -> &'static str; + fn handler(&self) -> Handler; fn name(&self) -> String { self.single_name().to_owned() @@ -144,6 +145,6 @@ pub trait ThemeTrait: BaseTheme + Send + Sync { impl BaseTheme for T { fn single_name(&self) -> &'static str { - util::single_type_name::() + single_type_name::() } } diff --git a/pagetop/src/prelude.rs b/pagetop/src/prelude.rs index b1c29c63..1420f1d6 100644 --- a/pagetop/src/prelude.rs +++ b/pagetop/src/prelude.rs @@ -1,7 +1,15 @@ //! Re-exporta recursos comunes. // Global macros and helpers. -pub use crate::{args, concat_string, theme_static_files, util, LazyStatic}; +pub use crate::{ + args, + concat_string, + pub_const_handler, + theme_static_files, + LazyStatic +}; +pub use crate::util; +pub use crate::util::Handler; pub use crate::config::SETTINGS; diff --git a/pagetop/src/response/page/hook.rs b/pagetop/src/response/page/hook.rs index 28d6a779..09363f35 100644 --- a/pagetop/src/response/page/hook.rs +++ b/pagetop/src/response/page/hook.rs @@ -1,7 +1,9 @@ use super::Page; use crate::core::hook::{AnyHookAction, HookActionTrait}; +use crate::pub_const_handler; +use crate::util::Handler; -pub const HOOK_BEFORE_RENDER_PAGE: &str = "pagetop::hook::before_render_page"; +pub_const_handler!(HOOK_BEFORE_RENDER_PAGE); type Hook = fn(&mut Page); @@ -18,7 +20,7 @@ impl HookActionTrait for BeforeRenderPageHook { } } - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { HOOK_BEFORE_RENDER_PAGE } diff --git a/pagetop/src/util.rs b/pagetop/src/util.rs index fed318be..28a979d2 100644 --- a/pagetop/src/util.rs +++ b/pagetop/src/util.rs @@ -1,3 +1,66 @@ +pub type Handler = u64; + +// https://stackoverflow.com/a/71464396 +pub const fn handler( + module_path: &'static str, + file: &'static str, + line: u32, + column: u32, +) -> Handler { + let mut hash = 0xcbf29ce484222325; + let prime = 0x00000100000001B3; + + let mut bytes = module_path.as_bytes(); + let mut i = 0; + + while i < bytes.len() { + hash ^= bytes[i] as u64; + hash = hash.wrapping_mul(prime); + i += 1; + } + + bytes = file.as_bytes(); + i = 0; + while i < bytes.len() { + hash ^= bytes[i] as u64; + hash = hash.wrapping_mul(prime); + i += 1; + } + + hash ^= line as u64; + hash = hash.wrapping_mul(prime); + hash ^= column as u64; + hash = hash.wrapping_mul(prime); + hash +} + +#[macro_export] +macro_rules! pub_const_handler { + ( $HANDLER:ident ) => { + pub const $HANDLER: $crate::util::Handler = $crate::util::handler( + module_path!(), + file!(), + line!(), + column!(), + ); + }; +} + +pub fn partial_type_name(type_name: &'static str, last: usize) -> &'static str { + if last == 0 { + return type_name; + } + let positions: Vec<_> = type_name.rmatch_indices("::").collect(); + if positions.len() < last { + return type_name; + } + &type_name[(positions[last - 1].0 + 2)..] +} + +pub fn single_type_name() -> &'static str { + partial_type_name(std::any::type_name::(), 1) +} + #[macro_export] /// Macro para construir grupos de pares clave-valor. /// @@ -34,18 +97,3 @@ macro_rules! theme_static_files { } }}; } - -pub fn partial_type_name(type_name: &'static str, last: usize) -> &'static str { - if last == 0 { - return type_name; - } - let positions: Vec<_> = type_name.rmatch_indices("::").collect(); - if positions.len() < last { - return type_name; - } - &type_name[(positions[last - 1].0 + 2)..] -} - -pub fn single_type_name() -> &'static str { - partial_type_name(std::any::type_name::(), 1) -} diff --git a/website/src/mdbook.rs b/website/src/mdbook.rs index 65038cac..f2589515 100644 --- a/website/src/mdbook.rs +++ b/website/src/mdbook.rs @@ -11,12 +11,12 @@ static MDBOOK: LazyStatic> = LazyStatic::new(|| generate() ); -pub const MODULE_MDBOOK: &str = "pagetop::website::module::mdbook"; +pub_const_handler!(MODULE_MDBOOK); pub struct MdBook; impl ModuleTrait for MdBook { - fn handler(&self) -> &'static str { + fn handler(&self) -> Handler { MODULE_MDBOOK }