From d80a594cf5b828015537e89a5fe554f3f8882b68 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Mon, 4 Mar 2024 22:21:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20smart=20pointers?= =?UTF-8?q?=20components=20for=20clarity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/component/block.rs | 8 +- src/base/component/flex/container.rs | 8 +- src/base/component/flex/item.rs | 8 +- src/base/component/form/form_main.rs | 8 +- src/base/component/menu/element.rs | 8 +- src/base/component/menu/group.rs | 8 +- src/base/component/menu/item.rs | 12 +-- src/base/component/menu/megamenu.rs | 8 +- src/base/component/menu/menu_main.rs | 8 +- src/base/component/menu/submenu.rs | 8 +- src/base/component/paragraph.rs | 10 +-- src/base/component/wrapper.rs | 8 +- src/core/component.rs | 10 +-- src/core/component/{arc_any.rs => arc_one.rs} | 78 +++++++++---------- src/core/component/arc_typed.rs | 66 ++++++++-------- src/core/theme/regions.rs | 33 ++++---- src/html/opt_component.rs | 8 +- src/response/page.rs | 9 +-- 18 files changed, 151 insertions(+), 155 deletions(-) rename src/core/component/{arc_any.rs => arc_one.rs} (56%) diff --git a/src/base/component/block.rs b/src/base/component/block.rs index 0e147698..bc0d4359 100644 --- a/src/base/component/block.rs +++ b/src/base/component/block.rs @@ -8,7 +8,7 @@ pub struct Block { renderable: Renderable, classes : OptionClasses, title : OptionTranslated, - stuff : AnyComponents, + stuff : MixedComponents, } impl ComponentTrait for Block { @@ -78,12 +78,12 @@ impl Block { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); + self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { + pub fn alter_components(&mut self, op: OneOp) -> &mut Self { self.stuff.alter_value(op); self } @@ -94,7 +94,7 @@ impl Block { &self.title } - pub fn components(&self) -> &AnyComponents { + pub fn components(&self) -> &MixedComponents { &self.stuff } } diff --git a/src/base/component/flex/container.rs b/src/base/component/flex/container.rs index 6ada35c0..727f7186 100644 --- a/src/base/component/flex/container.rs +++ b/src/base/component/flex/container.rs @@ -7,7 +7,7 @@ pub struct Container { weight : Weight, renderable : Renderable, classes : OptionClasses, - items : TypedComponents, + items : VectorComponents, direction : flex::Direction, wrap_align : flex::WrapAlign, content_justify: flex::ContentJustify, @@ -84,12 +84,12 @@ impl Container { #[rustfmt::skip] pub fn add_item(mut self, item: flex::Item) -> Self { - self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item))); + self.items.alter_value(TypedOp::Add(TypedComponent::with(item))); self } #[fn_builder] - pub fn alter_items(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_items(&mut self, op: TypedOp) -> &mut Self { self.items.alter_value(op); self } @@ -126,7 +126,7 @@ impl Container { // Container GETTERS. - pub fn items(&self) -> &TypedComponents { + pub fn items(&self) -> &VectorComponents { &self.items } diff --git a/src/base/component/flex/item.rs b/src/base/component/flex/item.rs index 335f1e11..2996769f 100644 --- a/src/base/component/flex/item.rs +++ b/src/base/component/flex/item.rs @@ -12,7 +12,7 @@ pub struct Item { item_size : flex::ItemSize, item_offset : flex::ItemOffset, item_align : flex::ItemAlign, - stuff : AnyComponents, + stuff : MixedComponents, } impl ComponentTrait for Item { @@ -115,12 +115,12 @@ impl Item { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); + self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { + pub fn alter_components(&mut self, op: OneOp) -> &mut Self { self.stuff.alter_value(op); self } @@ -147,7 +147,7 @@ impl Item { &self.item_align } - pub fn components(&self) -> &AnyComponents { + pub fn components(&self) -> &MixedComponents { &self.stuff } } diff --git a/src/base/component/form/form_main.rs b/src/base/component/form/form_main.rs index 6e5c4647..edab5f66 100644 --- a/src/base/component/form/form_main.rs +++ b/src/base/component/form/form_main.rs @@ -17,7 +17,7 @@ pub struct Form { action : OptionString, charset : OptionString, method : FormMethod, - stuff : AnyComponents, + stuff : MixedComponents, } impl ComponentTrait for Form { @@ -99,12 +99,12 @@ impl Form { #[rustfmt::skip] pub fn add_element(mut self, element: impl ComponentTrait) -> Self { - self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(element))); + self.stuff.alter_value(OneOp::Add(OneComponent::with(element))); self } #[fn_builder] - pub fn alter_elements(&mut self, op: ArcAnyOp) -> &mut Self { + pub fn alter_elements(&mut self, op: OneOp) -> &mut Self { self.stuff.alter_value(op); self } @@ -123,7 +123,7 @@ impl Form { &self.method } - pub fn elements(&self) -> &AnyComponents { + pub fn elements(&self) -> &MixedComponents { &self.stuff } } diff --git a/src/base/component/menu/element.rs b/src/base/component/menu/element.rs index 36041554..a6a7d731 100644 --- a/src/base/component/menu/element.rs +++ b/src/base/component/menu/element.rs @@ -2,8 +2,8 @@ use crate::prelude::*; use super::Submenu; -type Content = ArcTypedComponent; -type SubmenuItems = ArcTypedComponent; +type Content = TypedComponent; +type SubmenuItems = TypedComponent; #[derive(AutoDefault)] pub enum ElementType { @@ -52,14 +52,14 @@ impl ComponentTrait for Element { impl Element { pub fn html(content: Html) -> Self { Element { - element_type: ElementType::Html(Content::new(content)), + element_type: ElementType::Html(Content::with(content)), ..Default::default() } } pub fn submenu(submenu: Submenu) -> Self { Element { - element_type: ElementType::Submenu(SubmenuItems::new(submenu)), + element_type: ElementType::Submenu(SubmenuItems::with(submenu)), ..Default::default() } } diff --git a/src/base/component/menu/group.rs b/src/base/component/menu/group.rs index 26b6b180..8298629f 100644 --- a/src/base/component/menu/group.rs +++ b/src/base/component/menu/group.rs @@ -8,7 +8,7 @@ pub struct Group { id : OptionId, weight : Weight, renderable: Renderable, - elements : TypedComponents, + elements : VectorComponents, } impl ComponentTrait for Group { @@ -60,19 +60,19 @@ impl Group { #[rustfmt::skip] pub fn add_element(mut self, element: Element) -> Self { - self.elements.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(element))); + self.elements.alter_value(TypedOp::Add(TypedComponent::with(element))); self } #[fn_builder] - pub fn alter_elements(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_elements(&mut self, op: TypedOp) -> &mut Self { self.elements.alter_value(op); self } // Group GETTERS. - pub fn elements(&self) -> &TypedComponents { + pub fn elements(&self) -> &VectorComponents { &self.elements } } diff --git a/src/base/component/menu/item.rs b/src/base/component/menu/item.rs index 94c0b951..bf11bf9a 100644 --- a/src/base/component/menu/item.rs +++ b/src/base/component/menu/item.rs @@ -3,9 +3,9 @@ use crate::prelude::*; use super::{Megamenu, Submenu}; type Label = L10n; -type Content = ArcTypedComponent; -type SubmenuItems = ArcTypedComponent; -type MegamenuGroups = ArcTypedComponent; +type Content = TypedComponent; +type SubmenuItems = TypedComponent; +type MegamenuGroups = TypedComponent; #[derive(AutoDefault)] pub enum ItemType { @@ -135,21 +135,21 @@ impl Item { pub fn html(content: Html) -> Self { Item { - item_type: ItemType::Html(Content::new(content)), + item_type: ItemType::Html(Content::with(content)), ..Default::default() } } pub fn submenu(label: L10n, submenu: Submenu) -> Self { Item { - item_type: ItemType::Submenu(label, SubmenuItems::new(submenu)), + item_type: ItemType::Submenu(label, SubmenuItems::with(submenu)), ..Default::default() } } pub fn megamenu(label: L10n, megamenu: Megamenu) -> Self { Item { - item_type: ItemType::Megamenu(label, MegamenuGroups::new(megamenu)), + item_type: ItemType::Megamenu(label, MegamenuGroups::with(megamenu)), ..Default::default() } } diff --git a/src/base/component/menu/megamenu.rs b/src/base/component/menu/megamenu.rs index 51453974..8a186bbb 100644 --- a/src/base/component/menu/megamenu.rs +++ b/src/base/component/menu/megamenu.rs @@ -8,7 +8,7 @@ pub struct Megamenu { id : OptionId, weight : Weight, renderable: Renderable, - groups : TypedComponents, + groups : VectorComponents, } impl ComponentTrait for Megamenu { @@ -60,19 +60,19 @@ impl Megamenu { #[rustfmt::skip] pub fn add_group(mut self, group: Group) -> Self { - self.groups.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(group))); + self.groups.alter_value(TypedOp::Add(TypedComponent::with(group))); self } #[fn_builder] - pub fn alter_groups(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_groups(&mut self, op: TypedOp) -> &mut Self { self.groups.alter_value(op); self } // Megamenu GETTERS. - pub fn groups(&self) -> &TypedComponents { + pub fn groups(&self) -> &VectorComponents { &self.groups } } diff --git a/src/base/component/menu/menu_main.rs b/src/base/component/menu/menu_main.rs index 24b49675..7faa9b19 100644 --- a/src/base/component/menu/menu_main.rs +++ b/src/base/component/menu/menu_main.rs @@ -8,7 +8,7 @@ pub struct Menu { id : OptionId, weight : Weight, renderable: Renderable, - items : TypedComponents, + items : VectorComponents, } impl ComponentTrait for Menu { @@ -88,19 +88,19 @@ impl Menu { #[rustfmt::skip] pub fn add_item(mut self, item: Item) -> Self { - self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item))); + self.items.alter_value(TypedOp::Add(TypedComponent::with(item))); self } #[fn_builder] - pub fn alter_items(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_items(&mut self, op: TypedOp) -> &mut Self { self.items.alter_value(op); self } // Menu GETTERS. - pub fn items(&self) -> &TypedComponents { + pub fn items(&self) -> &VectorComponents { &self.items } } diff --git a/src/base/component/menu/submenu.rs b/src/base/component/menu/submenu.rs index 0f1328da..2e9bdfcd 100644 --- a/src/base/component/menu/submenu.rs +++ b/src/base/component/menu/submenu.rs @@ -9,7 +9,7 @@ pub struct Submenu { weight : Weight, renderable: Renderable, title : OptionTranslated, - items : TypedComponents, + items : VectorComponents, } impl ComponentTrait for Submenu { @@ -72,12 +72,12 @@ impl Submenu { #[rustfmt::skip] pub fn add_item(mut self, item: Item) -> Self { - self.items.alter_value(ArcTypedOp::Add(ArcTypedComponent::new(item))); + self.items.alter_value(TypedOp::Add(TypedComponent::with(item))); self } #[fn_builder] - pub fn alter_items(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_items(&mut self, op: TypedOp) -> &mut Self { self.items.alter_value(op); self } @@ -88,7 +88,7 @@ impl Submenu { &self.title } - pub fn items(&self) -> &TypedComponents { + pub fn items(&self) -> &VectorComponents { &self.items } } diff --git a/src/base/component/paragraph.rs b/src/base/component/paragraph.rs index ab47fe7d..5ef28c23 100644 --- a/src/base/component/paragraph.rs +++ b/src/base/component/paragraph.rs @@ -8,7 +8,7 @@ pub struct Paragraph { renderable: Renderable, classes : OptionClasses, font_size : FontSize, - stuff : AnyComponents, + stuff : MixedComponents, } impl ComponentTrait for Paragraph { @@ -81,18 +81,18 @@ impl Paragraph { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); + self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); self } #[rustfmt::skip] pub fn add_translated(mut self, l10n: L10n) -> Self { - self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(Translate::with(l10n)))); + self.stuff.alter_value(OneOp::Add(OneComponent::with(Translate::with(l10n)))); self } #[fn_builder] - pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { + pub fn alter_components(&mut self, op: OneOp) -> &mut Self { self.stuff.alter_value(op); self } @@ -103,7 +103,7 @@ impl Paragraph { &self.font_size } - pub fn components(&self) -> &AnyComponents { + pub fn components(&self) -> &MixedComponents { &self.stuff } } diff --git a/src/base/component/wrapper.rs b/src/base/component/wrapper.rs index 54c73f12..9bfefa06 100644 --- a/src/base/component/wrapper.rs +++ b/src/base/component/wrapper.rs @@ -18,7 +18,7 @@ pub struct Wrapper { renderable : Renderable, classes : OptionClasses, wrapper_type : WrapperType, - stuff : AnyComponents, + stuff : MixedComponents, } impl ComponentTrait for Wrapper { @@ -126,12 +126,12 @@ impl Wrapper { #[rustfmt::skip] pub fn add_component(mut self, component: impl ComponentTrait) -> Self { - self.stuff.alter_value(ArcAnyOp::Add(ArcAnyComponent::new(component))); + self.stuff.alter_value(OneOp::Add(OneComponent::with(component))); self } #[fn_builder] - pub fn alter_components(&mut self, op: ArcAnyOp) -> &mut Self { + pub fn alter_components(&mut self, op: OneOp) -> &mut Self { self.stuff.alter_value(op); self } @@ -142,7 +142,7 @@ impl Wrapper { &self.wrapper_type } - pub fn components(&self) -> &AnyComponents { + pub fn components(&self) -> &MixedComponents { &self.stuff } } diff --git a/src/core/component.rs b/src/core/component.rs index 57be6b3d..637199f7 100644 --- a/src/core/component.rs +++ b/src/core/component.rs @@ -11,10 +11,10 @@ pub use definition::{component_as_mut, component_as_ref, ComponentBase, Componen mod classes; pub use classes::{ComponentClasses, ComponentClassesOp}; -mod arc_any; -pub use arc_any::AnyComponents; -pub use arc_any::{ArcAnyComponent, ArcAnyOp}; +mod arc_one; +pub use arc_one::MixedComponents; +pub use arc_one::{OneComponent, OneOp}; mod arc_typed; -pub use arc_typed::TypedComponents; -pub use arc_typed::{ArcTypedComponent, ArcTypedOp}; +pub use arc_typed::VectorComponents; +pub use arc_typed::{TypedComponent, TypedOp}; diff --git a/src/core/component/arc_any.rs b/src/core/component/arc_one.rs similarity index 56% rename from src/core/component/arc_any.rs rename to src/core/component/arc_one.rs index 4f1975b7..0c078b32 100644 --- a/src/core/component/arc_any.rs +++ b/src/core/component/arc_one.rs @@ -5,20 +5,20 @@ use crate::{fn_builder, TypeId, Weight}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; #[derive(Clone)] -pub struct ArcAnyComponent(Arc>); +pub struct OneComponent(Arc>); -impl ArcAnyComponent { - pub fn new(component: impl ComponentTrait) -> Self { - ArcAnyComponent(Arc::new(RwLock::new(component))) +impl OneComponent { + pub fn with(component: impl ComponentTrait) -> Self { + OneComponent(Arc::new(RwLock::new(component))) } - // ArcAnyComponent BUILDER. + // OneComponent BUILDER. pub fn set(&mut self, component: impl ComponentTrait) { self.0 = Arc::new(RwLock::new(component)); } - // ArcAnyComponent GETTERS. + // OneComponent GETTERS. pub fn get(&self) -> RwLockReadGuard<'_, dyn ComponentTrait> { self.0.read().unwrap() @@ -28,13 +28,13 @@ impl ArcAnyComponent { self.0.write().unwrap() } - // ArcAnyComponent RENDER. + // OneComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { self.0.write().unwrap().render(cx) } - // ArcAnyComponent HELPERS. + // OneComponent HELPERS. fn type_id(&self) -> TypeId { self.0.read().unwrap().type_id() @@ -51,86 +51,86 @@ impl ArcAnyComponent { // ************************************************************************************************* -pub enum ArcAnyOp { - Add(ArcAnyComponent), - AddAfterId(&'static str, ArcAnyComponent), - AddBeforeId(&'static str, ArcAnyComponent), - Prepend(ArcAnyComponent), +pub enum OneOp { + Add(OneComponent), + AddAfterId(&'static str, OneComponent), + AddBeforeId(&'static str, OneComponent), + Prepend(OneComponent), RemoveById(&'static str), - ReplaceById(&'static str, ArcAnyComponent), + ReplaceById(&'static str, OneComponent), Reset, } #[derive(Clone, Default)] -pub struct AnyComponents(Vec); +pub struct MixedComponents(Vec); -impl AnyComponents { - pub fn new(arc: ArcAnyComponent) -> Self { - AnyComponents::default().with_value(ArcAnyOp::Add(arc)) +impl MixedComponents { + pub fn new(any: OneComponent) -> Self { + MixedComponents::default().with_value(OneOp::Add(any)) } - pub(crate) fn merge(mixes: &[Option<&AnyComponents>]) -> Self { - let mut opt = AnyComponents::default(); + pub(crate) fn merge(mixes: &[Option<&MixedComponents>]) -> Self { + let mut opt = MixedComponents::default(); for m in mixes.iter().flatten() { opt.0.append(&mut m.0.clone()); } opt } - // AnyComponents BUILDER. + // MixedComponents BUILDER. #[fn_builder] - pub fn alter_value(&mut self, op: ArcAnyOp) -> &mut Self { + pub fn alter_value(&mut self, op: OneOp) -> &mut Self { match op { - ArcAnyOp::Add(arc) => self.0.push(arc), - ArcAnyOp::AddAfterId(id, arc) => match self.0.iter().position(|c| c.id() == id) { - Some(index) => self.0.insert(index + 1, arc), - _ => self.0.push(arc), + OneOp::Add(any) => self.0.push(any), + OneOp::AddAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) { + Some(index) => self.0.insert(index + 1, any), + _ => self.0.push(any), }, - ArcAnyOp::AddBeforeId(id, arc) => match self.0.iter().position(|c| c.id() == id) { - Some(index) => self.0.insert(index, arc), - _ => self.0.insert(0, arc), + OneOp::AddBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) { + Some(index) => self.0.insert(index, any), + _ => self.0.insert(0, any), }, - ArcAnyOp::Prepend(arc) => self.0.insert(0, arc), - ArcAnyOp::RemoveById(id) => { + OneOp::Prepend(any) => self.0.insert(0, any), + OneOp::RemoveById(id) => { if let Some(index) = self.0.iter().position(|c| c.id() == id) { self.0.remove(index); } } - ArcAnyOp::ReplaceById(id, arc) => { + OneOp::ReplaceById(id, any) => { for c in self.0.iter_mut() { if c.id() == id { - *c = arc; + *c = any; break; } } } - ArcAnyOp::Reset => self.0.clear(), + OneOp::Reset => self.0.clear(), } self } - // AnyComponents GETTERS. + // MixedComponents GETTERS. pub fn is_empty(&self) -> bool { self.0.is_empty() } - pub fn get_by_id(&self, id: impl Into) -> Option<&ArcAnyComponent> { + pub fn get_by_id(&self, id: impl Into) -> Option<&OneComponent> { let id = id.into(); self.0.iter().find(|c| c.id() == id) } - pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { + pub fn iter_by_id(&self, id: impl Into) -> impl Iterator { 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 { + pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator { self.0.iter().filter(move |&c| c.type_id() == type_id) } - // AnyComponents RENDER. + // MixedComponents RENDER. pub fn render(&self, cx: &mut Context) -> Markup { let mut components = self.0.clone(); diff --git a/src/core/component/arc_typed.rs b/src/core/component/arc_typed.rs index d288f702..df772730 100644 --- a/src/core/component/arc_typed.rs +++ b/src/core/component/arc_typed.rs @@ -4,26 +4,26 @@ use crate::{fn_builder, TypeId, Weight}; use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; -pub struct ArcTypedComponent(Arc>); +pub struct TypedComponent(Arc>); -impl Clone for ArcTypedComponent { +impl Clone for TypedComponent { fn clone(&self) -> Self { Self(self.0.clone()) } } -impl ArcTypedComponent { - pub fn new(component: C) -> Self { - ArcTypedComponent(Arc::new(RwLock::new(component))) +impl TypedComponent { + pub fn with(component: C) -> Self { + TypedComponent(Arc::new(RwLock::new(component))) } - // ArcTypedComponent BUILDER. + // TypedComponent BUILDER. pub fn set(&mut self, component: C) { self.0 = Arc::new(RwLock::new(component)); } - // ArcTypedComponent GETTERS. + // TypedComponent GETTERS. pub fn get(&self) -> RwLockReadGuard<'_, C> { self.0.read().unwrap() @@ -33,13 +33,13 @@ impl ArcTypedComponent { self.0.write().unwrap() } - // ArcTypedComponent RENDER. + // TypedComponent RENDER. pub fn render(&self, cx: &mut Context) -> Markup { self.0.write().unwrap().render(cx) } - // ArcTypedComponent HELPERS. + // TypedComponent HELPERS. fn type_id(&self) -> TypeId { self.0.read().unwrap().type_id() @@ -56,45 +56,45 @@ impl ArcTypedComponent { // ************************************************************************************************* -pub enum ArcTypedOp { - Add(ArcTypedComponent), - AddAfterId(&'static str, ArcTypedComponent), - AddBeforeId(&'static str, ArcTypedComponent), - Prepend(ArcTypedComponent), +pub enum TypedOp { + Add(TypedComponent), + AddAfterId(&'static str, TypedComponent), + AddBeforeId(&'static str, TypedComponent), + Prepend(TypedComponent), RemoveById(&'static str), - ReplaceById(&'static str, ArcTypedComponent), + ReplaceById(&'static str, TypedComponent), Reset, } #[derive(Clone, Default)] -pub struct TypedComponents(Vec>); +pub struct VectorComponents(Vec>); -impl TypedComponents { - pub fn new(arc: ArcTypedComponent) -> Self { - TypedComponents::default().with_value(ArcTypedOp::Add(arc)) +impl VectorComponents { + pub fn new(one: TypedComponent) -> Self { + VectorComponents::default().with_value(TypedOp::Add(one)) } - // TypedComponents BUILDER. + // VectorComponents BUILDER. #[fn_builder] - pub fn alter_value(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_value(&mut self, op: TypedOp) -> &mut Self { match op { - ArcTypedOp::Add(one) => self.0.push(one), - ArcTypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) { + TypedOp::Add(one) => self.0.push(one), + TypedOp::AddAfterId(id, one) => match self.0.iter().position(|c| c.id() == id) { Some(index) => self.0.insert(index + 1, one), _ => self.0.push(one), }, - ArcTypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) { + TypedOp::AddBeforeId(id, one) => match self.0.iter().position(|c| c.id() == id) { Some(index) => self.0.insert(index, one), _ => self.0.insert(0, one), }, - ArcTypedOp::Prepend(one) => self.0.insert(0, one), - ArcTypedOp::RemoveById(id) => { + 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); } } - ArcTypedOp::ReplaceById(id, one) => { + TypedOp::ReplaceById(id, one) => { for c in self.0.iter_mut() { if c.id() == id { *c = one; @@ -102,32 +102,32 @@ impl TypedComponents { } } } - ArcTypedOp::Reset => self.0.clear(), + TypedOp::Reset => self.0.clear(), } self } - // TypedComponents GETTERS. + // VectorComponents GETTERS. pub fn is_empty(&self) -> bool { self.0.is_empty() } - pub fn get_by_id(&self, id: impl Into) -> Option<&ArcTypedComponent> { + pub fn get_by_id(&self, id: impl Into) -> Option<&TypedComponent> { let id = id.into(); self.0.iter().find(|&c| c.id() == id) } - pub fn iter_by_id(&self, id: impl Into) -> impl Iterator> { + pub fn iter_by_id(&self, id: impl Into) -> impl Iterator> { 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> { + pub fn iter_by_type_id(&self, type_id: TypeId) -> impl Iterator> { self.0.iter().filter(move |&c| c.type_id() == type_id) } - // TypedComponents RENDER. + // VectorComponents RENDER. pub fn render(&self, cx: &mut Context) -> Markup { let mut components = self.0.clone(); diff --git a/src/core/theme/regions.rs b/src/core/theme/regions.rs index 87296158..92fdaafc 100644 --- a/src/core/theme/regions.rs +++ b/src/core/theme/regions.rs @@ -1,4 +1,4 @@ -use crate::core::component::{AnyComponents, ArcAnyComponent, ArcAnyOp}; +use crate::core::component::{MixedComponents, OneComponent, OneOp}; use crate::core::theme::ThemeRef; use crate::{AutoDefault, LazyStatic, TypeId}; @@ -12,29 +12,29 @@ static COMMON_REGIONS: LazyStatic> = LazyStatic::new(|| RwLock::new(ComponentsInRegions::default())); #[derive(AutoDefault)] -pub struct ComponentsInRegions(HashMap<&'static str, AnyComponents>); +pub struct ComponentsInRegions(HashMap<&'static str, MixedComponents>); impl ComponentsInRegions { - pub fn new(region: &'static str, arc: ArcAnyComponent) -> Self { + pub fn new(region: &'static str, one: OneComponent) -> Self { let mut regions = ComponentsInRegions::default(); - regions.add_component_in(region, arc); + regions.add_in(region, one); regions } - pub fn add_component_in(&mut self, region: &'static str, arc: ArcAnyComponent) { + pub fn add_in(&mut self, region: &'static str, one: OneComponent) { if let Some(region) = self.0.get_mut(region) { - region.alter_value(ArcAnyOp::Add(arc)); + region.alter_value(OneOp::Add(one)); } else { - self.0.insert(region, AnyComponents::new(arc)); + self.0.insert(region, MixedComponents::new(one)); } } - pub fn get_components(&self, theme: ThemeRef, region: &str) -> AnyComponents { + pub fn get_components(&self, theme: ThemeRef, region: &str) -> MixedComponents { let common = COMMON_REGIONS.read().unwrap(); if let Some(r) = THEME_REGIONS.read().unwrap().get(&theme.type_id()) { - AnyComponents::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)]) + MixedComponents::merge(&[common.0.get(region), self.0.get(region), r.0.get(region)]) } else { - AnyComponents::merge(&[common.0.get(region), self.0.get(region)]) + MixedComponents::merge(&[common.0.get(region), self.0.get(region)]) } } } @@ -46,23 +46,20 @@ pub enum InRegion { } impl InRegion { - pub fn add_component(&self, arc: ArcAnyComponent) -> &Self { + pub fn add(&self, one: OneComponent) -> &Self { match self { InRegion::Content => { - COMMON_REGIONS - .write() - .unwrap() - .add_component_in("content", arc); + COMMON_REGIONS.write().unwrap().add_in("content", one); } InRegion::Named(name) => { - COMMON_REGIONS.write().unwrap().add_component_in(name, arc); + COMMON_REGIONS.write().unwrap().add_in(name, one); } InRegion::OfTheme(region, theme) => { let mut regions = THEME_REGIONS.write().unwrap(); if let Some(r) = regions.get_mut(&theme.type_id()) { - r.add_component_in(region, arc); + r.add_in(region, one); } else { - regions.insert(theme.type_id(), ComponentsInRegions::new(region, arc)); + regions.insert(theme.type_id(), ComponentsInRegions::new(region, one)); } } } diff --git a/src/html/opt_component.rs b/src/html/opt_component.rs index 059f2e3e..9bb4523c 100644 --- a/src/html/opt_component.rs +++ b/src/html/opt_component.rs @@ -1,8 +1,8 @@ -use crate::core::component::{ArcTypedComponent, ComponentTrait, Context}; +use crate::core::component::{ComponentTrait, Context, TypedComponent}; use crate::fn_builder; use crate::html::{html, Markup}; -pub struct OptionComponent(Option>); +pub struct OptionComponent(Option>); impl Default for OptionComponent { fn default() -> Self { @@ -20,7 +20,7 @@ impl OptionComponent { #[fn_builder] pub fn alter_value(&mut self, component: Option) -> &mut Self { if let Some(component) = component { - self.0 = Some(ArcTypedComponent::new(component)); + self.0 = Some(TypedComponent::with(component)); } else { self.0 = None; } @@ -29,7 +29,7 @@ impl OptionComponent { // OptionComponent GETTERS. - pub fn get(&self) -> Option> { + pub fn get(&self) -> Option> { if let Some(value) = &self.0 { return Some(value.clone()); } diff --git a/src/response/page.rs b/src/response/page.rs index 48d47c37..6bfd8c0c 100644 --- a/src/response/page.rs +++ b/src/response/page.rs @@ -4,7 +4,7 @@ pub use error::ErrorPage; pub use actix_web::Result as ResultPage; use crate::base::action; -use crate::core::component::{AnyComponents, ArcAnyComponent, ComponentTrait}; +use crate::core::component::{ComponentTrait, MixedComponents, OneComponent}; use crate::core::component::{Context, ContextOp}; use crate::core::theme::ComponentsInRegions; use crate::fn_builder; @@ -107,7 +107,7 @@ impl Page { #[fn_builder] pub fn alter_component(&mut self, component: impl ComponentTrait) -> &mut Self { self.regions - .add_component_in("content", ArcAnyComponent::new(component)); + .add_in("content", OneComponent::with(component)); self } @@ -117,8 +117,7 @@ impl Page { region: &'static str, component: impl ComponentTrait, ) -> &mut Self { - self.regions - .add_component_in(region, ArcAnyComponent::new(component)); + self.regions.add_in(region, OneComponent::with(component)); self } @@ -166,7 +165,7 @@ impl Page { &self.skip_to } - pub fn components_in(&self, region: &str) -> AnyComponents { + pub fn components_in(&self, region: &str) -> MixedComponents { self.regions.get_components(self.context.theme(), region) }