From 3827c859e96eff7724ddfc63d5f335f7c5eaec77 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 30 Nov 2024 18:53:35 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20Improve=20registering=20packages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.rs | 12 ++++++++++-- src/core/package/all.rs | 25 +++++++++++-------------- src/core/package/definition.rs | 2 +- src/core/theme/definition.rs | 31 ++++++++++++++++++++++++++++++- src/locale.rs | 4 ---- 5 files changed, 52 insertions(+), 22 deletions(-) diff --git a/src/core.rs b/src/core.rs index cd076eb0..2d65eadf 100644 --- a/src/core.rs +++ b/src/core.rs @@ -4,14 +4,18 @@ use crate::util::TypeInfo; use std::any::Any; -// Common definitions for core types. +/// A base trait that extends `Any` to provide metadata and dynamic type casting capabilities. pub trait AnyBase: Any { + /// Returns the full name of the type. fn type_name(&self) -> &'static str; + /// Returns a short name for the type. fn short_name(&self) -> &'static str; + /// Returns a reference to `dyn Any` for dynamic type casting. fn as_any_ref(&self) -> &dyn Any; + /// Returns a mutable reference to `dyn Any` for dynamic type casting. fn as_any_mut(&mut self) -> &mut dyn Any; } @@ -38,7 +42,9 @@ impl AnyBase for T { } } +/// A trait for advanced dynamic type manipulation and downcasting. pub trait AnyTo: AnyBase { + /// Checks if the type is of the specified type `T`. #[inline] fn is(&self) -> bool where @@ -47,6 +53,7 @@ pub trait AnyTo: AnyBase { self.as_any_ref().is::() } + /// Attempts to downcast a reference to the specified type `T`. #[inline] fn downcast_ref(&self) -> Option<&T> where @@ -55,6 +62,7 @@ pub trait AnyTo: AnyBase { self.as_any_ref().downcast_ref() } + /// Attempts to downcast a mutable reference to the specified type `T`. #[inline] fn downcast_mut(&mut self) -> Option<&mut T> where @@ -66,7 +74,7 @@ pub trait AnyTo: AnyBase { impl AnyTo for T {} -// API to define functions that alter the behavior of PageTop core. +// API to define functions that alter the predefined behavior of the code. pub mod action; // API to build new components. diff --git a/src/core/package/all.rs b/src/core/package/all.rs index 3d833151..e41d62df 100644 --- a/src/core/package/all.rs +++ b/src/core/package/all.rs @@ -5,8 +5,6 @@ use crate::{global, include_files, include_files_service, service, trace}; use std::sync::{LazyLock, RwLock}; -include_files!(assets); - // PACKAGES **************************************************************************************** static ENABLED_PACKAGES: LazyLock>> = @@ -24,15 +22,14 @@ pub fn register_packages(root_package: Option) { // Add default theme to the enabled list. add_to_enabled(&mut enabled_list, &crate::base::theme::Basic); - // Add default welcome page package to the enabled list. - add_to_enabled(&mut enabled_list, &crate::base::package::Welcome); - // If a root package is provided, add it to the enabled list. if let Some(package) = root_package { add_to_enabled(&mut enabled_list, package); } - // Reverse the order to ensure packages are sorted from none to most dependencies. - enabled_list.reverse(); + + // Add default welcome page package to the enabled list. + add_to_enabled(&mut enabled_list, &crate::base::package::Welcome); + // Save the final list of enabled packages. ENABLED_PACKAGES.write().unwrap().append(&mut enabled_list); @@ -49,16 +46,14 @@ pub fn register_packages(root_package: Option) { fn add_to_enabled(list: &mut Vec, package: PackageRef) { // Check if the package is not already in the enabled list to avoid duplicates. if !list.iter().any(|p| p.type_id() == package.type_id()) { - // Add the package to the enabled list. - list.push(package); - - // Reverse dependencies to add them in correct order (dependencies first). - let mut dependencies = package.dependencies(); - dependencies.reverse(); - for d in &dependencies { + // Add the package dependencies in reverse order first. + for d in package.dependencies().iter().rev() { add_to_enabled(list, *d); } + // Add the package itself to the enabled list. + list.push(package); + // Check if the package has an associated theme to register. if let Some(theme) = package.theme() { let mut registered_themes = THEMES.write().unwrap(); @@ -127,6 +122,8 @@ pub fn init_packages() { // CONFIGURE SERVICES ****************************************************************************** +include_files!(assets); + pub fn configure_services(scfg: &mut service::web::ServiceConfig) { for m in ENABLED_PACKAGES.read().unwrap().iter() { m.configure_service(scfg); diff --git a/src/core/package/definition.rs b/src/core/package/definition.rs index 3506a929..b1d0a8c9 100644 --- a/src/core/package/definition.rs +++ b/src/core/package/definition.rs @@ -13,7 +13,7 @@ pub trait PackageTrait: AnyBase + Send + Sync { } fn description(&self) -> L10n { - L10n::none() + L10n::default() } fn theme(&self) -> Option { diff --git a/src/core/theme/definition.rs b/src/core/theme/definition.rs index 7397913f..6f474516 100644 --- a/src/core/theme/definition.rs +++ b/src/core/theme/definition.rs @@ -79,7 +79,7 @@ pub trait ThemeTrait: PackageTrait + Send + Sync { meta charset="utf-8"; @if let Some(title) = page.title() { - title { (global::SETTINGS.app.name) (" - ") (title) } + title { (global::SETTINGS.app.name) (" | ") (title) } } @else { title { (global::SETTINGS.app.name) } } @@ -102,4 +102,33 @@ pub trait ThemeTrait: PackageTrait + Send + Sync { } }) } + /* + fn prepare_page_body(&self, page: &mut Page) -> PrepareMarkup { + PrepareMarkup::With(html! { + body id=[page.body_id().get()] class=[page.body_classes().get()] { + (page.body_content().render()) + } + }) + } + + fn error_403(&self, request: service::HttpRequest) -> Page { + Page::new(request) + .with_title(L10n::n("Error FORBIDDEN")) + .with_body(PrepareMarkup::With(html! { + div { + h1 { ("FORBIDDEN ACCESS") } + } + })) + } + + fn error_404(&self, request: service::HttpRequest) -> Page { + Page::new(request) + .with_title(L10n::n("Error RESOURCE NOT FOUND")) + .with_body(PrepareMarkup::With(html! { + div { + h1 { ("RESOURCE NOT FOUND") } + } + })) + } + */ } diff --git a/src/locale.rs b/src/locale.rs index 5b2d0565..513c7f70 100644 --- a/src/locale.rs +++ b/src/locale.rs @@ -182,10 +182,6 @@ pub struct L10n { } impl L10n { - pub fn none() -> Self { - L10n::default() - } - pub fn n(text: impl Into) -> Self { L10n { op: L10nOp::Text(text.into()),