From 3be7b23d0a7e98037f2f0526eeebe793bc0f94f7 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 16 Nov 2023 18:13:36 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Drop=20Default,=20always=20refer?= =?UTF-8?q?=20existing=20components?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop/src/core/component/arc_any.rs | 81 +++++++++------------ pagetop/src/core/component/arc_typed.rs | 97 +++++++++++++------------ 2 files changed, 84 insertions(+), 94 deletions(-) diff --git a/pagetop/src/core/component/arc_any.rs b/pagetop/src/core/component/arc_any.rs index 680a368b..4b8759cd 100644 --- a/pagetop/src/core/component/arc_any.rs +++ b/pagetop/src/core/component/arc_any.rs @@ -1,29 +1,12 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, Markup}; -use crate::{fn_builder, impl_handle, Handle, Weight}; +use crate::{fn_builder, Handle, Weight}; -use std::sync::{Arc, RwLock, RwLockReadGuard}; - -#[derive(Default)] -struct ComponentNull; - -impl_handle!(COMPONENT_NULL for ComponentNull); - -impl ComponentTrait for ComponentNull { - fn new() -> Self { - ComponentNull::default() - } -} +use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; #[derive(Clone)] pub struct ArcAnyComponent(Arc>); -impl Default for ArcAnyComponent { - fn default() -> Self { - ArcAnyComponent(Arc::new(RwLock::new(ComponentNull))) - } -} - impl ArcAnyComponent { pub fn new(component: impl ComponentTrait) -> Self { ArcAnyComponent(Arc::new(RwLock::new(component))) @@ -41,16 +24,8 @@ impl ArcAnyComponent { self.0.read().unwrap() } - pub(crate) fn handle(&self) -> Handle { - self.0.read().unwrap().handle() - } - - pub(crate) fn id(&self) -> Option { - self.0.read().unwrap().id() - } - - pub(crate) fn weight(&self) -> Weight { - self.0.read().unwrap().weight() + pub fn get_mut(&self) -> RwLockWriteGuard<'_, dyn ComponentTrait> { + self.0.write().unwrap() } // ArcAnyComponent RENDER. @@ -58,6 +33,20 @@ impl ArcAnyComponent { pub fn render(&self, cx: &mut Context) -> Markup { self.0.write().unwrap().render(cx) } + + // ArcAnyComponent HELPERS. + + fn handle(&self) -> Handle { + self.0.read().unwrap().handle() + } + + fn id(&self) -> String { + self.0.read().unwrap().id().unwrap_or_default() + } + + fn weight(&self) -> Weight { + self.0.read().unwrap().weight() + } } // ************************************************************************************************* @@ -94,27 +83,23 @@ impl AnyComponents { pub fn alter_value(&mut self, op: ArcAnyOp) -> &mut Self { match op { ArcAnyOp::Add(arc) => self.0.push(arc), - ArcAnyOp::AddAfterId(id, arc) => { - match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { - Some(index) => self.0.insert(index + 1, arc), - _ => self.0.push(arc), - } - } - ArcAnyOp::AddBeforeId(id, arc) => { - match self.0.iter().position(|c| c.id().as_deref() == Some(id)) { - Some(index) => self.0.insert(index, arc), - _ => self.0.insert(0, 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), + }, + 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), + }, ArcAnyOp::AddFirst(arc) => self.0.insert(0, arc), ArcAnyOp::RemoveById(id) => { - if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) { + if let Some(index) = self.0.iter().position(|c| c.id() == id) { self.0.remove(index); } } ArcAnyOp::ReplaceById(id, arc) => { for c in self.0.iter_mut() { - if c.id().as_deref() == Some(id) { + if c.id() == id { *c = arc; break; } @@ -131,12 +116,14 @@ impl AnyComponents { self.0.is_empty() } - pub fn get_by_id(&self, id: &'static str) -> Option<&ArcAnyComponent> { - self.0.iter().find(|&c| c.id().as_deref() == Some(id)) + pub fn get_by_id(&self, id: impl Into) -> Option<&ArcAnyComponent> { + let id = id.into(); + self.0.iter().find(|c| c.id() == id) } - pub fn iter_by_id(&self, id: &'static str) -> impl Iterator { - self.0.iter().filter(|&c| c.id().as_deref() == Some(id)) + 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_handle(&self, handle: Handle) -> impl Iterator { diff --git a/pagetop/src/core/component/arc_typed.rs b/pagetop/src/core/component/arc_typed.rs index e2e66e56..7bce8c98 100644 --- a/pagetop/src/core/component/arc_typed.rs +++ b/pagetop/src/core/component/arc_typed.rs @@ -2,44 +2,35 @@ use crate::core::component::{ComponentTrait, Context}; use crate::html::{html, Markup}; use crate::{fn_builder, Handle, Weight}; -use std::sync::{Arc, RwLock, RwLockReadGuard}; +use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}; -#[derive(Default)] -pub struct ArcTypedComponent(Arc>); +pub struct ArcTypedComponent(Arc>); -impl Clone for ArcTypedComponent { +impl Clone for ArcTypedComponent { fn clone(&self) -> Self { Self(self.0.clone()) } } -impl ArcTypedComponent { - pub fn new(component: T) -> Self { +impl ArcTypedComponent { + pub fn new(component: C) -> Self { ArcTypedComponent(Arc::new(RwLock::new(component))) } // ArcTypedComponent BUILDER. - pub fn set(&mut self, component: T) { + pub fn set(&mut self, component: C) { self.0 = Arc::new(RwLock::new(component)); } // ArcTypedComponent GETTERS. - pub fn get(&self) -> RwLockReadGuard<'_, T> { + pub fn get(&self) -> RwLockReadGuard<'_, C> { self.0.read().unwrap() } - pub(crate) fn handle(&self) -> Handle { - self.0.read().unwrap().handle() - } - - pub(crate) fn id(&self) -> Option { - self.0.read().unwrap().id() - } - - pub(crate) fn weight(&self) -> Weight { - self.0.read().unwrap().weight() + pub fn get_mut(&self) -> RwLockWriteGuard<'_, C> { + self.0.write().unwrap() } // ArcTypedComponent RENDER. @@ -47,55 +38,65 @@ impl ArcTypedComponent { pub fn render(&self, cx: &mut Context) -> Markup { self.0.write().unwrap().render(cx) } + + // ArcTypedComponent HELPERS. + + fn handle(&self) -> Handle { + self.0.read().unwrap().handle() + } + + fn id(&self) -> String { + self.0.read().unwrap().id().unwrap_or_default() + } + + fn weight(&self) -> Weight { + self.0.read().unwrap().weight() + } } // ************************************************************************************************* -pub enum ArcTypedOp { - Add(ArcTypedComponent), - AddAfterId(&'static str, ArcTypedComponent), - AddBeforeId(&'static str, ArcTypedComponent), - AddFirst(ArcTypedComponent), +pub enum ArcTypedOp { + Add(ArcTypedComponent), + AddAfterId(&'static str, ArcTypedComponent), + AddBeforeId(&'static str, ArcTypedComponent), + AddFirst(ArcTypedComponent), RemoveById(&'static str), - ReplaceById(&'static str, ArcTypedComponent), + ReplaceById(&'static str, ArcTypedComponent), Reset, } #[derive(Clone, Default)] -pub struct TypedComponents(Vec>); +pub struct TypedComponents(Vec>); -impl TypedComponents { - pub fn new(arc: ArcTypedComponent) -> Self { +impl TypedComponents { + pub fn new(arc: ArcTypedComponent) -> Self { TypedComponents::default().with_value(ArcTypedOp::Add(arc)) } // TypedComponents BUILDER. #[fn_builder] - pub fn alter_value(&mut self, op: ArcTypedOp) -> &mut Self { + pub fn alter_value(&mut self, op: ArcTypedOp) -> &mut Self { match op { ArcTypedOp::Add(one) => self.0.push(one), - ArcTypedOp::AddAfterId(id, one) => { - match self.0.iter().position(|c| c.id().as_deref() == Some(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().as_deref() == Some(id)) { - Some(index) => self.0.insert(index, one), - _ => self.0.insert(0, one), - } - } + ArcTypedOp::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) { + Some(index) => self.0.insert(index, one), + _ => self.0.insert(0, one), + }, ArcTypedOp::AddFirst(one) => self.0.insert(0, one), ArcTypedOp::RemoveById(id) => { - if let Some(index) = self.0.iter().position(|c| c.id().as_deref() == Some(id)) { + if let Some(index) = self.0.iter().position(|c| c.id() == id) { self.0.remove(index); } } ArcTypedOp::ReplaceById(id, one) => { for c in self.0.iter_mut() { - if c.id().as_deref() == Some(id) { + if c.id() == id { *c = one; break; } @@ -112,15 +113,17 @@ impl TypedComponents { self.0.is_empty() } - pub fn get_by_id(&self, id: &'static str) -> Option<&ArcTypedComponent> { - self.0.iter().find(|&c| c.id().as_deref() == Some(id)) + pub fn get_by_id(&self, id: impl Into) -> Option<&ArcTypedComponent> { + let id = id.into(); + self.0.iter().find(|&c| c.id() == id) } - pub fn iter_by_id(&self, id: &'static str) -> impl Iterator> { - self.0.iter().filter(|&c| c.id().as_deref() == Some(id)) + 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_handle(&self, handle: Handle) -> impl Iterator> { + pub fn iter_by_handle(&self, handle: Handle) -> impl Iterator> { self.0.iter().filter(move |&c| c.handle() == handle) }