Añade y estandariza nombres de mód., tem. y comp.
This commit is contained in:
parent
37d0254055
commit
16996eeee0
9 changed files with 109 additions and 32 deletions
|
|
@ -19,7 +19,7 @@ pub fn register_module(module: &'static dyn ModuleTrait) {
|
|||
fn add_to(list: &mut Vec<&dyn ModuleTrait>, module: &'static dyn ModuleTrait) {
|
||||
if !MODULES.read().unwrap().iter().any(|m| m.name() == module.name()) {
|
||||
if !list.iter().any(|m| m.name() == module.name()) {
|
||||
trace::debug!("Registering \"{}\" module", module.name());
|
||||
trace::debug!("Registering \"{}\" module", module.single_name());
|
||||
list.push(module);
|
||||
|
||||
let mut dependencies = module.dependencies();
|
||||
|
|
|
|||
|
|
@ -1,22 +1,31 @@
|
|||
use crate::app;
|
||||
use crate::util::partial_type_name;
|
||||
|
||||
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
|
||||
use crate::db;
|
||||
|
||||
pub trait BaseModule {
|
||||
fn type_name(&self) -> &'static str;
|
||||
|
||||
fn single_name(&self) -> &'static str;
|
||||
|
||||
fn qualified_name(&self, last: usize) -> &'static str;
|
||||
}
|
||||
|
||||
/// Los módulos deben implementar este *trait*.
|
||||
pub trait ModuleTrait: Send + Sync {
|
||||
fn name(&self) -> &'static str;
|
||||
|
||||
fn fullname(&self) -> String;
|
||||
|
||||
fn dependencies(&self) -> Vec<&'static dyn ModuleTrait> {
|
||||
vec![]
|
||||
pub trait ModuleTrait: BaseModule + Send + Sync {
|
||||
fn name(&self) -> String {
|
||||
self.single_name().to_owned()
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
fn dependencies(&self) -> Vec<&'static dyn ModuleTrait> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn configure_module(&self, cfg: &mut app::web::ServiceConfig) {
|
||||
}
|
||||
|
|
@ -27,3 +36,17 @@ pub trait ModuleTrait: Send + Sync {
|
|||
vec![]
|
||||
}
|
||||
}
|
||||
|
||||
impl<M: ?Sized + ModuleTrait> BaseModule for M {
|
||||
fn type_name(&self) -> &'static str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
|
||||
fn single_name(&self) -> &'static str {
|
||||
partial_type_name(std::any::type_name::<Self>(), 1)
|
||||
}
|
||||
|
||||
fn qualified_name(&self, last: usize) -> &'static str {
|
||||
partial_type_name(std::any::type_name::<Self>(), last)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
mod definition;
|
||||
pub use definition::ModuleTrait;
|
||||
pub use definition::{
|
||||
BaseModule,
|
||||
ModuleTrait,
|
||||
};
|
||||
|
||||
pub(crate) mod all;
|
||||
pub use all::register_module;
|
||||
|
|
@ -1,24 +1,23 @@
|
|||
use crate::html::{Markup, html};
|
||||
use crate::response::page::PageAssets;
|
||||
|
||||
use std::any::type_name;
|
||||
use crate::util::partial_type_name;
|
||||
|
||||
pub use std::any::Any as AnyComponent;
|
||||
|
||||
pub trait ComponentTrait: AnyComponent + Send + Sync {
|
||||
pub trait BaseComponent {
|
||||
fn type_name(&self) -> &'static str;
|
||||
|
||||
fn single_name(&self) -> &'static str;
|
||||
|
||||
fn qualified_name(&self, last: usize) -> &'static str;
|
||||
}
|
||||
|
||||
pub trait ComponentTrait: AnyComponent + BaseComponent + Send + Sync {
|
||||
|
||||
fn new() -> Self where Self: Sized;
|
||||
|
||||
fn name(&self) -> &'static str {
|
||||
let name = type_name::<Self>();
|
||||
match name.rfind("::") {
|
||||
Some(position) => &name[(position + 2)..],
|
||||
None => name
|
||||
}
|
||||
}
|
||||
|
||||
fn fullname(&self) -> String {
|
||||
type_name::<Self>().to_owned()
|
||||
fn name(&self) -> String {
|
||||
self.single_name().to_owned()
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<String> {
|
||||
|
|
@ -47,10 +46,24 @@ pub trait ComponentTrait: AnyComponent + Send + Sync {
|
|||
fn as_mut_any(&mut self) -> &mut dyn AnyComponent;
|
||||
}
|
||||
|
||||
pub fn component_ref<T: 'static>(component: &dyn ComponentTrait) -> &T {
|
||||
component.as_ref_any().downcast_ref::<T>().unwrap()
|
||||
impl<C: ?Sized + ComponentTrait> BaseComponent for C {
|
||||
fn type_name(&self) -> &'static str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
|
||||
fn single_name(&self) -> &'static str {
|
||||
partial_type_name(std::any::type_name::<Self>(), 1)
|
||||
}
|
||||
|
||||
fn qualified_name(&self, last: usize) -> &'static str {
|
||||
partial_type_name(std::any::type_name::<Self>(), last)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn component_mut<T: 'static>(component: &mut dyn ComponentTrait) -> &mut T {
|
||||
component.as_mut_any().downcast_mut::<T>().unwrap()
|
||||
pub fn component_ref<C: 'static>(component: &dyn ComponentTrait) -> &C {
|
||||
component.as_ref_any().downcast_ref::<C>().unwrap()
|
||||
}
|
||||
|
||||
pub fn component_mut<C: 'static>(component: &mut dyn ComponentTrait) -> &mut C {
|
||||
component.as_mut_any().downcast_mut::<C>().unwrap()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ pub use assets::{
|
|||
mod component;
|
||||
pub use component::{
|
||||
AnyComponent,
|
||||
BaseComponent,
|
||||
ComponentTrait,
|
||||
component_ref,
|
||||
component_mut,
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ static THEMES: Lazy<RwLock<Vec<&dyn ThemeTrait>>> = Lazy::new(|| {
|
|||
pub fn register_theme(theme: &'static dyn ThemeTrait) {
|
||||
let mut themes = THEMES.write().unwrap();
|
||||
if !themes.iter().any(|t| t.name() == theme.name()) {
|
||||
trace::debug!("Registering \"{}\" theme", theme.name());
|
||||
trace::debug!("Registering \"{}\" theme", theme.single_name());
|
||||
themes.push(theme);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,12 +3,21 @@ use crate::config::SETTINGS;
|
|||
use crate::html::{Markup, html};
|
||||
use crate::response::page::{ComponentTrait, Favicon, Page, PageAssets};
|
||||
use crate::base::component::Chunck;
|
||||
use crate::util::partial_type_name;
|
||||
|
||||
pub trait BaseTheme {
|
||||
fn type_name(&self) -> &'static str;
|
||||
|
||||
fn single_name(&self) -> &'static str;
|
||||
|
||||
fn qualified_name(&self, last: usize) -> &'static str;
|
||||
}
|
||||
|
||||
/// Los temas deben implementar este "trait".
|
||||
pub trait ThemeTrait: Send + Sync {
|
||||
fn name(&self) -> &'static str;
|
||||
|
||||
fn fullname(&self) -> String;
|
||||
pub trait ThemeTrait: BaseTheme + Send + Sync {
|
||||
fn name(&self) -> String {
|
||||
self.single_name().to_owned()
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<String> {
|
||||
None
|
||||
|
|
@ -127,3 +136,17 @@ pub trait ThemeTrait: Send + Sync {
|
|||
.render()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + ThemeTrait> BaseTheme for T {
|
||||
fn type_name(&self) -> &'static str {
|
||||
std::any::type_name::<Self>()
|
||||
}
|
||||
|
||||
fn single_name(&self) -> &'static str {
|
||||
partial_type_name(std::any::type_name::<Self>(), 1)
|
||||
}
|
||||
|
||||
fn qualified_name(&self, last: usize) -> &'static str {
|
||||
partial_type_name(std::any::type_name::<Self>(), last)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
mod definition;
|
||||
pub use definition::ThemeTrait;
|
||||
pub use definition::{
|
||||
BaseTheme,
|
||||
ThemeTrait,
|
||||
};
|
||||
|
||||
pub(crate) mod all;
|
||||
pub use all::{
|
||||
|
|
|
|||
|
|
@ -46,3 +46,14 @@ macro_rules! theme_static_files {
|
|||
}
|
||||
}};
|
||||
}
|
||||
|
||||
pub(crate) 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)..]
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue