Elimina downcast_rs y codifica componentes con Any

This commit is contained in:
Manuel Cillero 2022-04-23 21:25:21 +02:00
parent 13c2e77688
commit 6de248f630
16 changed files with 60 additions and 9 deletions

View file

@ -24,7 +24,6 @@ categories = [
[dependencies]
concat-string = "1.0.1"
doc-comment = "0.3.3"
downcast-rs = "1.2.0"
figlet-rs = "0.1.3"
futures = "0.3.21"
once_cell = "1.10.0"

View file

@ -45,6 +45,10 @@ impl PageComponent for Block {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Block {

View file

@ -28,6 +28,10 @@ impl PageComponent for Chunck {
fn default_render(&self, _: &mut PageAssets) -> Markup {
html! { (*self.html()) }
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Chunck {

View file

@ -72,6 +72,10 @@ impl PageComponent for Container {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Container {

View file

@ -65,6 +65,10 @@ impl PageComponent for Button {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Button {

View file

@ -84,6 +84,10 @@ impl PageComponent for Date {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Date {

View file

@ -54,6 +54,10 @@ impl PageComponent for Form {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Form {

View file

@ -28,6 +28,10 @@ impl PageComponent for Hidden {
input type="hidden" id=[id] name=[self.name()] value=[self.value()];
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Hidden {

View file

@ -112,6 +112,10 @@ impl PageComponent for Input {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Input {

View file

@ -40,6 +40,10 @@ impl PageComponent for Column {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Column {

View file

@ -40,6 +40,10 @@ impl PageComponent for Row {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Row {

View file

@ -37,6 +37,10 @@ impl PageComponent for Image {
class=[self.classes()];
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Image {

View file

@ -67,6 +67,10 @@ impl PageComponent for MenuItem {
MenuItemType::Void => html! {},
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl MenuItem {
@ -217,6 +221,10 @@ impl PageComponent for Menu {
}
}
}
fn as_mut_any(&mut self) -> &mut dyn AnyComponent {
self
}
}
impl Menu {

View file

@ -39,11 +39,11 @@ impl ThemeTrait for BulmixTheme {
) {
match component.name() {
"GridRow" => {
let row = component.downcast_mut::<grid::Row>().unwrap();
let row = component.as_mut_any().downcast_mut::<grid::Row>().unwrap();
row.alter_classes("columns", ClassesOp::SetDefault);
},
"GridColumn" => {
let col = component.downcast_mut::<grid::Column>().unwrap();
let col = component.as_mut_any().downcast_mut::<grid::Column>().unwrap();
col.alter_classes("column", ClassesOp::SetDefault);
},
_ => {},

View file

@ -1,11 +1,11 @@
use crate::html::{Markup, html};
use crate::response::page::PageAssets;
use downcast_rs::{Downcast, impl_downcast};
use std::any::type_name;
pub trait PageComponent: Downcast + Send + Sync {
pub use std::any::Any as AnyComponent;
pub trait PageComponent: AnyComponent + Send + Sync {
fn new() -> Self where Self: Sized;
@ -41,6 +41,6 @@ pub trait PageComponent: Downcast + Send + Sync {
fn default_render(&self, assets: &mut PageAssets) -> Markup {
html! {}
}
}
impl_downcast!(PageComponent);
fn as_mut_any(&mut self) -> &mut dyn AnyComponent;
}

View file

@ -7,7 +7,7 @@ pub use assets::{
};
mod component;
pub use component::PageComponent;
pub use component::{AnyComponent, PageComponent};
mod container;
pub use container::PageContainer;