🧑‍💻 Simplify component vector handling

This commit is contained in:
Manuel Cillero 2024-03-16 21:41:04 +01:00
parent 400a492311
commit d7acf0c3d1
17 changed files with 154 additions and 221 deletions

View file

@ -90,14 +90,14 @@ impl Block {
}
#[fn_builder]
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(component)));
self
}

View file

@ -139,14 +139,14 @@ impl Item {
}
#[fn_builder]
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(component)));
self
}

View file

@ -97,15 +97,15 @@ impl Form {
self
}
#[rustfmt::skip]
pub fn add_element(mut self, element: impl ComponentTrait) -> Self {
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(element)));
#[fn_builder]
pub fn alter_elements(&mut self, op: AnyOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
#[fn_builder]
pub fn alter_elements(&mut self, op: MixedOp) -> &mut Self {
self.mixed.alter_value(op);
#[rustfmt::skip]
pub fn add_element(mut self, element: impl ComponentTrait) -> Self {
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(element)));
self
}

View file

@ -2,8 +2,8 @@ use crate::prelude::*;
use super::Submenu;
type Content = OneComponent<Html>;
type SubmenuItems = OneComponent<Submenu>;
type Content = TypedComponent<Html>;
type SubmenuItems = TypedComponent<Submenu>;
#[derive(AutoDefault)]
pub enum ElementType {

View file

@ -8,7 +8,7 @@ pub struct Group {
id : OptionId,
weight : Weight,
renderable: Renderable,
elements : TypedComponents<Element>,
elements : MixedComponents,
}
impl ComponentTrait for Group {
@ -58,21 +58,21 @@ impl Group {
self
}
#[rustfmt::skip]
pub fn add_element(mut self, element: Element) -> Self {
self.elements.alter_value(TypedOp::Add(OneComponent::with(element)));
#[fn_builder]
pub fn alter_elements(&mut self, op: TypedOp<Element>) -> &mut Self {
self.elements.alter_typed(op);
self
}
#[fn_builder]
pub fn alter_elements(&mut self, op: TypedOp<Element>) -> &mut Self {
self.elements.alter_value(op);
#[rustfmt::skip]
pub fn add_element(mut self, element: Element) -> Self {
self.elements.alter_value(AnyOp::Add(AnyComponent::with(element)));
self
}
// Group GETTERS.
pub fn elements(&self) -> &TypedComponents<Element> {
pub fn elements(&self) -> &MixedComponents {
&self.elements
}
}

View file

@ -3,9 +3,9 @@ use crate::prelude::*;
use super::{Megamenu, Submenu};
type Label = L10n;
type Content = OneComponent<Html>;
type SubmenuItems = OneComponent<Submenu>;
type MegamenuGroups = OneComponent<Megamenu>;
type Content = TypedComponent<Html>;
type SubmenuItems = TypedComponent<Submenu>;
type MegamenuGroups = TypedComponent<Megamenu>;
#[derive(AutoDefault)]
pub enum ItemType {

View file

@ -8,7 +8,7 @@ pub struct Megamenu {
id : OptionId,
weight : Weight,
renderable: Renderable,
groups : TypedComponents<Group>,
groups : MixedComponents,
}
impl ComponentTrait for Megamenu {
@ -58,21 +58,21 @@ impl Megamenu {
self
}
#[rustfmt::skip]
pub fn add_group(mut self, group: Group) -> Self {
self.groups.alter_value(TypedOp::Add(OneComponent::with(group)));
#[fn_builder]
pub fn alter_groups(&mut self, op: TypedOp<Group>) -> &mut Self {
self.groups.alter_typed(op);
self
}
#[fn_builder]
pub fn alter_groups(&mut self, op: TypedOp<Group>) -> &mut Self {
self.groups.alter_value(op);
#[rustfmt::skip]
pub fn add_group(mut self, group: Group) -> Self {
self.groups.alter_value(AnyOp::Add(AnyComponent::with(group)));
self
}
// Megamenu GETTERS.
pub fn groups(&self) -> &TypedComponents<Group> {
pub fn groups(&self) -> &MixedComponents {
&self.groups
}
}

View file

@ -8,7 +8,7 @@ pub struct Menu {
id : OptionId,
weight : Weight,
renderable: Renderable,
items : TypedComponents<Item>,
items : MixedComponents,
}
impl ComponentTrait for Menu {
@ -86,21 +86,21 @@ impl Menu {
self
}
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
#[fn_builder]
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
self.items.alter_typed(op);
self
}
#[fn_builder]
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
self.items.alter_value(op);
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(AnyOp::Add(AnyComponent::with(item)));
self
}
// Menu GETTERS.
pub fn items(&self) -> &TypedComponents<Item> {
pub fn items(&self) -> &MixedComponents {
&self.items
}
}

View file

@ -9,7 +9,7 @@ pub struct Submenu {
weight : Weight,
renderable: Renderable,
title : OptionTranslated,
items : TypedComponents<Item>,
items : MixedComponents,
}
impl ComponentTrait for Submenu {
@ -70,15 +70,15 @@ impl Submenu {
self
}
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(TypedOp::Add(OneComponent::with(item)));
#[fn_builder]
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
self.items.alter_typed(op);
self
}
#[fn_builder]
pub fn alter_items(&mut self, op: TypedOp<Item>) -> &mut Self {
self.items.alter_value(op);
#[rustfmt::skip]
pub fn add_item(mut self, item: Item) -> Self {
self.items.alter_value(AnyOp::Add(AnyComponent::with(item)));
self
}
@ -88,7 +88,7 @@ impl Submenu {
&self.title
}
pub fn items(&self) -> &TypedComponents<Item> {
pub fn items(&self) -> &MixedComponents {
&self.items
}
}

View file

@ -80,14 +80,14 @@ impl Paragraph {
}
#[fn_builder]
pub fn alter_components(&mut self, op: MixedOp) -> &mut Self {
pub fn alter_components(&mut self, op: AnyOp) -> &mut Self {
self.mixed.alter_value(op);
self
}
#[rustfmt::skip]
pub fn add_component(mut self, component: impl ComponentTrait) -> Self {
self.mixed.alter_value(MixedOp::Add(AnyComponent::with(component)));
self.mixed.alter_value(AnyOp::Add(AnyComponent::with(component)));
self
}

View file

@ -11,8 +11,7 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen
mod classes;
pub use classes::{ComponentClasses, ComponentClassesOp};
mod arc_mixed;
pub use arc_mixed::{AnyComponent, MixedComponents, MixedOp};
mod arc_typed;
pub use arc_typed::{OneComponent, TypedComponents, TypedOp};
mod mixed;
pub use mixed::MixedComponents;
pub use mixed::{AnyComponent, AnyOp};
pub use mixed::{TypedComponent, TypedOp};

View file

@ -1,141 +0,0 @@
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, Markup};
use crate::{fn_builder, TypeId, Weight};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub struct OneComponent<C: ComponentTrait>(Arc<RwLock<C>>);
impl<C: ComponentTrait> Clone for OneComponent<C> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<C: ComponentTrait> OneComponent<C> {
pub fn with(component: C) -> Self {
OneComponent(Arc::new(RwLock::new(component)))
}
// OneComponent BUILDER.
pub fn set(&mut self, component: C) {
self.0 = Arc::new(RwLock::new(component));
}
// OneComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.0.read().unwrap()
}
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
self.0.write().unwrap()
}
// OneComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
// OneComponent HELPERS.
fn type_id(&self) -> TypeId {
self.0.read().unwrap().type_id()
}
fn id(&self) -> String {
self.0.read().unwrap().id().unwrap_or_default()
}
fn weight(&self) -> Weight {
self.0.read().unwrap().weight()
}
}
// *************************************************************************************************
pub enum TypedOp<C: ComponentTrait> {
Add(OneComponent<C>),
InsertAfterId(&'static str, OneComponent<C>),
InsertBeforeId(&'static str, OneComponent<C>),
Prepend(OneComponent<C>),
RemoveById(&'static str),
ReplaceById(&'static str, OneComponent<C>),
Reset,
}
#[derive(Clone, Default)]
pub struct TypedComponents<C: ComponentTrait>(Vec<OneComponent<C>>);
impl<C: ComponentTrait + Default> TypedComponents<C> {
pub fn new(one: OneComponent<C>) -> Self {
TypedComponents::default().with_value(TypedOp::Add(one))
}
// TypedComponents BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: TypedOp<C>) -> &mut Self {
match op {
TypedOp::Add(one) => self.0.push(one),
TypedOp::InsertAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, one),
_ => self.0.push(one),
},
TypedOp::InsertBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, one),
_ => self.0.insert(0, one),
},
TypedOp::Prepend(one) => self.0.insert(0, one),
TypedOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index);
}
}
TypedOp::ReplaceById(id, one) => {
for c in self.0.iter_mut() {
if c.id() == id {
*c = one;
break;
}
}
}
TypedOp::Reset => self.0.clear(),
}
self
}
// TypedComponents GETTERS.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn get_by_id(&self, id: impl Into<String>) -> Option<&OneComponent<C>> {
let id = id.into();
self.0.iter().find(|&c| c.id() == id)
}
pub fn iter_by_id(&self, id: impl Into<String>) -> impl Iterator<Item = &OneComponent<C>> {
let id = id.into();
self.0.iter().filter(move |&c| c.id() == id)
}
pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator<Item = &OneComponent<C>> {
self.0.iter().filter(move |&c| c.type_id() == type_id)
}
// TypedComponents RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
let mut components = self.0.clone();
components.sort_by_key(|c| c.weight());
html! {
@for c in components.iter() {
(c.render(cx))
}
}
}
}

View file

@ -1,5 +1,5 @@
use crate::base::component::add_base_assets;
use crate::core::component::MixedOp;
use crate::core::component::AnyOp;
use crate::core::theme::all::{theme_by_single_name, THEME_DEFAULT};
use crate::core::theme::{ComponentsInRegions, ThemeRef};
use crate::html::{html, Assets, HeadScript, HeadStyles, JavaScript, Markup, StyleSheet};
@ -95,7 +95,7 @@ impl Context {
self
}
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
pub fn alter_regions(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
self.regions.alter_components(region, op);
self
}

View file

@ -51,7 +51,43 @@ impl AnyComponent {
// *************************************************************************************************
pub enum MixedOp {
pub struct TypedComponent<C: ComponentTrait>(Arc<RwLock<C>>);
impl<C: ComponentTrait> Clone for TypedComponent<C> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<C: ComponentTrait> TypedComponent<C> {
pub fn with(component: C) -> Self {
TypedComponent(Arc::new(RwLock::new(component)))
}
// TypedComponent GETTERS.
pub fn get(&self) -> RwLockReadGuard<'_, C> {
self.0.read().unwrap()
}
pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> {
self.0.write().unwrap()
}
fn to_any(&self) -> AnyComponent {
AnyComponent(self.0.clone())
}
// TypedComponent RENDER.
pub fn render(&self, cx: &mut Context) -> Markup {
self.0.write().unwrap().render(cx)
}
}
// *************************************************************************************************
pub enum AnyOp {
Add(AnyComponent),
InsertAfterId(&'static str, AnyComponent),
InsertBeforeId(&'static str, AnyComponent),
@ -61,6 +97,16 @@ pub enum MixedOp {
Reset,
}
pub enum TypedOp<C: ComponentTrait> {
Add(TypedComponent<C>),
InsertAfterId(&'static str, TypedComponent<C>),
InsertBeforeId(&'static str, TypedComponent<C>),
Prepend(TypedComponent<C>),
RemoveById(&'static str),
ReplaceById(&'static str, TypedComponent<C>),
Reset,
}
#[derive(Clone, Default)]
pub struct MixedComponents(Vec<AnyComponent>);
@ -70,7 +116,7 @@ impl MixedComponents {
}
pub fn with(any: AnyComponent) -> Self {
MixedComponents::default().with_value(MixedOp::Add(any))
MixedComponents::default().with_value(AnyOp::Add(any))
}
pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self {
@ -84,24 +130,24 @@ impl MixedComponents {
// MixedComponents BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: MixedOp) -> &mut Self {
pub fn alter_value(&mut self, op: AnyOp) -> &mut Self {
match op {
MixedOp::Add(any) => self.0.push(any),
MixedOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
AnyOp::Add(any) => self.0.push(any),
AnyOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, any),
_ => self.0.push(any),
},
MixedOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
AnyOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, any),
_ => self.0.insert(0, any),
},
MixedOp::Prepend(any) => self.0.insert(0, any),
MixedOp::RemoveById(id) => {
AnyOp::Prepend(any) => self.0.insert(0, any),
AnyOp::RemoveById(id) => {
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index);
}
}
MixedOp::ReplaceById(id, any) => {
AnyOp::ReplaceById(id, any) => {
for c in self.0.iter_mut() {
if c.id() == id {
*c = any;
@ -109,11 +155,40 @@ impl MixedComponents {
}
}
}
MixedOp::Reset => self.0.clear(),
AnyOp::Reset => self.0.clear(),
}
self
}
#[fn_builder]
#[rustfmt::skip]
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
match op {
TypedOp::Add(typed) => {
self.alter_value(AnyOp::Add(typed.to_any()))
}
TypedOp::InsertAfterId(id, typed) => {
self.alter_value(AnyOp::InsertAfterId(id, typed.to_any()))
}
TypedOp::InsertBeforeId(id, typed) => {
self.alter_value(AnyOp::InsertBeforeId(id, typed.to_any()))
}
TypedOp::Prepend(typed) => {
self.alter_value(AnyOp::Prepend(typed.to_any()))
}
TypedOp::RemoveById(id) => {
self.alter_value(AnyOp::RemoveById(id))
}
TypedOp::ReplaceById(id, typed) => {
self.alter_value(AnyOp::ReplaceById(id, typed.to_any()))
}
TypedOp::Reset => {
self.alter_value(AnyOp::Reset)
}
};
self
}
// MixedComponents GETTERS.
pub fn is_empty(&self) -> bool {

View file

@ -1,4 +1,4 @@
use crate::core::component::{AnyComponent, MixedComponents, MixedOp};
use crate::core::component::{AnyComponent, AnyOp, MixedComponents};
use crate::core::theme::ThemeRef;
use crate::{fn_builder, AutoDefault, LazyStatic, TypeId};
@ -16,11 +16,11 @@ pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>);
impl ComponentsInRegions {
pub fn new(region: &'static str, any: AnyComponent) -> Self {
ComponentsInRegions::default().with_components(region, MixedOp::Add(any))
ComponentsInRegions::default().with_components(region, AnyOp::Add(any))
}
#[fn_builder]
pub fn alter_components(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
pub fn alter_components(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
if let Some(region) = self.0.get_mut(region) {
region.alter_value(op);
} else {
@ -52,18 +52,18 @@ impl InRegion {
COMMON_REGIONS
.write()
.unwrap()
.alter_components("content", MixedOp::Add(any));
.alter_components("content", AnyOp::Add(any));
}
InRegion::Named(name) => {
COMMON_REGIONS
.write()
.unwrap()
.alter_components(name, MixedOp::Add(any));
.alter_components(name, AnyOp::Add(any));
}
InRegion::OfTheme(region, theme) => {
let mut regions = THEME_REGIONS.write().unwrap();
if let Some(r) = regions.get_mut(&theme.type_id()) {
r.alter_components(region, MixedOp::Add(any));
r.alter_components(region, AnyOp::Add(any));
} else {
regions.insert(theme.type_id(), ComponentsInRegions::new(region, any));
}

View file

@ -1,8 +1,8 @@
use crate::core::component::{ComponentTrait, Context, OneComponent};
use crate::core::component::{ComponentTrait, Context, TypedComponent};
use crate::fn_builder;
use crate::html::{html, Markup};
pub struct OptionComponent<C: ComponentTrait>(Option<OneComponent<C>>);
pub struct OptionComponent<C: ComponentTrait>(Option<TypedComponent<C>>);
impl<C: ComponentTrait> Default for OptionComponent<C> {
fn default() -> Self {
@ -20,7 +20,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
#[fn_builder]
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
if let Some(component) = component {
self.0 = Some(OneComponent::with(component));
self.0 = Some(TypedComponent::with(component));
} else {
self.0 = None;
}
@ -29,7 +29,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
// OptionComponent GETTERS.
pub fn get(&self) -> Option<OneComponent<C>> {
pub fn get(&self) -> Option<TypedComponent<C>> {
if let Some(value) = &self.0 {
return Some(value.clone());
}

View file

@ -4,7 +4,7 @@ pub use error::ErrorPage;
pub use actix_web::Result as ResultPage;
use crate::base::action;
use crate::core::component::{AnyComponent, ComponentTrait, MixedOp};
use crate::core::component::{AnyComponent, AnyOp, ComponentTrait};
use crate::core::component::{AssetsOp, Context};
use crate::fn_builder;
use crate::html::{html, Markup, DOCTYPE};
@ -106,14 +106,14 @@ impl Page {
}
#[fn_builder]
pub fn alter_regions(&mut self, region: &'static str, op: MixedOp) -> &mut Self {
pub fn alter_regions(&mut self, region: &'static str, op: AnyOp) -> &mut Self {
self.context.alter_regions(region, op);
self
}
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
self.context
.alter_regions("content", MixedOp::Add(AnyComponent::with(component)));
.alter_regions("content", AnyOp::Add(AnyComponent::with(component)));
self
}
@ -123,7 +123,7 @@ impl Page {
component: impl ComponentTrait,
) -> Self {
self.context
.alter_regions(region, MixedOp::Add(AnyComponent::with(component)));
.alter_regions(region, AnyOp::Add(AnyComponent::with(component)));
self
}