Modifica la forma de identificar temas y módulos

Cada módulo y cada tema requerirá a partir de ahora un identificador que
debería ser único y con alguna sintaxis particular aún por definir (por
ejemplo, admitiendo sólo minúsculas y sin espacios).
This commit is contained in:
Manuel Cillero 2022-02-26 21:48:39 +01:00
parent 3764f707da
commit 83fd12b5cc
16 changed files with 65 additions and 30 deletions

View file

@ -1,17 +0,0 @@
use crate::prelude::*;
use crate::base::module::admin::summary::summary;
pub struct AdminModule;
impl Module for AdminModule {
fn name(&self) -> String {
"PageTop Admin".to_string()
}
fn configure_module(&self, cfg: &mut server::web::ServiceConfig) {
cfg.service(
server::web::scope("/admin")
.route("", server::web::get().to(summary))
);
}
}

View file

@ -0,0 +1,2 @@
module_name = Admin module
module_desc = Administration module.

View file

@ -0,0 +1,2 @@
module_name = Admin module
module_desc = Módulo de administración.

View file

@ -1,4 +1,28 @@
mod configure; use crate::prelude::*;
pub use configure::AdminModule;
localize!("en-US", "src/base/module/admin/locales");
mod summary; mod summary;
pub struct AdminModule;
impl Module for AdminModule {
fn id(&self) -> &'static str {
"admin"
}
fn name(&self) -> String {
l("module_name")
}
fn description(&self) -> String {
l("module_desc")
}
fn configure_module(&self, cfg: &mut server::web::ServiceConfig) {
cfg.service(
server::web::scope("/admin")
.route("", server::web::get().to(summary::summary))
);
}
}

View file

@ -33,7 +33,7 @@ pub async fn summary() -> server::Result<Markup> {
Page::prepare() Page::prepare()
.using_theme(&bootsier::BootsierTheme) .using_theme("bootsier")
.with_title("Admin") .with_title("Admin")

View file

@ -5,6 +5,10 @@ localize!("en-US", "src/base/module/homepage/locales");
pub struct HomepageModule; pub struct HomepageModule;
impl Module for HomepageModule { impl Module for HomepageModule {
fn id(&self) -> &'static str {
"homepage"
}
fn name(&self) -> String { fn name(&self) -> String {
l("module_name") l("module_name")
} }

View file

@ -5,6 +5,10 @@ localize!("en-US", "src/base/module/user/locales");
pub struct UserModule; pub struct UserModule;
impl Module for UserModule { impl Module for UserModule {
fn id(&self) -> &'static str {
"user"
}
fn name(&self) -> String { fn name(&self) -> String {
l("module_name") l("module_name")
} }

View file

@ -5,6 +5,10 @@ include!(concat!(env!("OUT_DIR"), "/aliner.rs"));
pub struct AlinerTheme; pub struct AlinerTheme;
impl Theme for AlinerTheme { impl Theme for AlinerTheme {
fn id(&self) -> &'static str {
"aliner"
}
fn name(&self) -> String { fn name(&self) -> String {
"Aliner".to_string() "Aliner".to_string()
} }

View file

@ -7,6 +7,10 @@ localize!("en-US", "src/base/theme/bootsier/locales");
pub struct BootsierTheme; pub struct BootsierTheme;
impl Theme for BootsierTheme { impl Theme for BootsierTheme {
fn id(&self) -> &'static str {
"bootsier"
}
fn name(&self) -> String { fn name(&self) -> String {
"Bootsier".to_string() "Bootsier".to_string()
} }

View file

@ -3,6 +3,10 @@ use crate::prelude::*;
pub struct MinimalTheme; pub struct MinimalTheme;
impl Theme for MinimalTheme { impl Theme for MinimalTheme {
fn id(&self) -> &'static str {
"minimal"
}
fn name(&self) -> String { fn name(&self) -> String {
"Minimal".to_string() "Minimal".to_string()
} }

View file

@ -2,6 +2,8 @@ use crate::core::server;
/// Los módulos deben implementar este *trait*. /// Los módulos deben implementar este *trait*.
pub trait Module: Send + Sync { pub trait Module: Send + Sync {
fn id(&self) -> &'static str;
fn name(&self) -> String; fn name(&self) -> String;
fn description(&self) -> String { fn description(&self) -> String {

View file

@ -7,9 +7,9 @@ pub fn register_module(m: &'static (dyn Module + 'static)) {
MODULES.write().unwrap().push(m); MODULES.write().unwrap().push(m);
} }
pub fn find_module(name: &str) -> Option<&'static (dyn Module + 'static)> { pub fn find_module(id: &str) -> Option<&'static (dyn Module + 'static)> {
let modules = MODULES.write().unwrap(); let modules = MODULES.write().unwrap();
match modules.iter().find(|t| t.name() == name) { match modules.iter().find(|t| t.id() == id) {
Some(module) => Some(*module), Some(module) => Some(*module),
_ => None, _ => None,
} }

View file

@ -1,5 +1,5 @@
use crate::core::all::DEFAULT_THEME; use crate::core::all::DEFAULT_THEME;
use crate::core::theme::{Markup, PreEscaped, Theme, html}; use crate::core::theme::{Markup, PreEscaped, Theme, find_theme, html};
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Favicon. // Favicon.
@ -195,8 +195,8 @@ impl Assets {
} }
} }
pub fn using_theme(&mut self, theme: &'static dyn Theme) -> &mut Self { pub fn using_theme(&mut self, theme_id: &str) -> &mut Self {
self.theme = theme; self.theme = find_theme(theme_id).unwrap_or(*DEFAULT_THEME);
self self
} }

View file

@ -1,7 +1,7 @@
use crate::config::SETTINGS; use crate::config::SETTINGS;
use crate::core::server; use crate::core::server;
use crate::core::all::COMPONENTS; use crate::core::all::COMPONENTS;
use crate::core::theme::{DOCTYPE, Markup, Theme, html}; use crate::core::theme::{DOCTYPE, Markup, html};
use crate::core::response::page::{PageAssets, PageComponent, PageContainer}; use crate::core::response::page::{PageAssets, PageComponent, PageContainer};
use std::borrow::Cow; use std::borrow::Cow;
@ -159,8 +159,8 @@ impl<'a> Page<'a> {
// Page EXTRAS. // Page EXTRAS.
pub fn using_theme(&mut self, theme: &'static dyn Theme) -> &mut Self { pub fn using_theme(&mut self, theme_id: &str) -> &mut Self {
self.assets.using_theme(theme); self.assets.using_theme(theme_id);
self self
} }
} }

View file

@ -5,6 +5,8 @@ use crate::base::component::Chunck;
/// Los temas deben implementar este "trait". /// Los temas deben implementar este "trait".
pub trait Theme: Send + Sync { pub trait Theme: Send + Sync {
fn id(&self) -> &'static str;
fn name(&self) -> String; fn name(&self) -> String;
fn description(&self) -> String { fn description(&self) -> String {

View file

@ -9,9 +9,9 @@ pub fn register_theme(t: &'static (dyn Theme + 'static)) {
THEMES.write().unwrap().push(t); THEMES.write().unwrap().push(t);
} }
pub fn find_theme(name: &str) -> Option<&'static (dyn Theme + 'static)> { pub fn find_theme(id: &str) -> Option<&'static (dyn Theme + 'static)> {
let themes = THEMES.write().unwrap(); let themes = THEMES.write().unwrap();
match themes.iter().find(|t| t.name() == name) { match themes.iter().find(|t| t.id() == id) {
Some(theme) => Some(*theme), Some(theme) => Some(*theme),
_ => None, _ => None,
} }