🚧 Unify mixed and typed component ops for clarity

This commit is contained in:
Manuel Cillero 2024-03-16 22:04:56 +01:00
parent d7acf0c3d1
commit e8364a40ee

View file

@ -132,22 +132,66 @@ impl MixedComponents {
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, op: AnyOp) -> &mut Self { pub fn alter_value(&mut self, op: AnyOp) -> &mut Self {
match op { match op {
AnyOp::Add(any) => self.0.push(any), AnyOp::Add(any) => self.add(any),
AnyOp::InsertAfterId(id, any) => match self.0.iter().position(|c| c.id() == id) { AnyOp::InsertAfterId(id, any) => self.insert_after_id(id, any),
AnyOp::InsertBeforeId(id, any) => self.insert_before_id(id, any),
AnyOp::Prepend(any) => self.prepend(any),
AnyOp::RemoveById(id) => self.remove_by_id(id),
AnyOp::ReplaceById(id, any) => self.replace_by_id(id, any),
AnyOp::Reset => self.reset(),
};
self
}
#[fn_builder]
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
match op {
TypedOp::Add(typed) => self.add(typed.to_any()),
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.to_any()),
TypedOp::InsertBeforeId(id, typed) => self.insert_before_id(id, typed.to_any()),
TypedOp::Prepend(typed) => self.prepend(typed.to_any()),
TypedOp::RemoveById(id) => self.remove_by_id(id),
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.to_any()),
TypedOp::Reset => self.reset(),
};
self
}
#[inline]
fn add(&mut self, any: AnyComponent) {
self.0.push(any);
}
#[inline]
fn insert_after_id(&mut self, id: &str, any: AnyComponent) {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, any), Some(index) => self.0.insert(index + 1, any),
_ => self.0.push(any), _ => self.0.push(any),
}, };
AnyOp::InsertBeforeId(id, any) => match self.0.iter().position(|c| c.id() == id) { }
#[inline]
fn insert_before_id(&mut self, id: &str, any: AnyComponent) {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, any), Some(index) => self.0.insert(index, any),
_ => self.0.insert(0, any), _ => self.0.insert(0, any),
}, };
AnyOp::Prepend(any) => self.0.insert(0, any), }
AnyOp::RemoveById(id) => {
#[inline]
fn prepend(&mut self, any: AnyComponent) {
self.0.insert(0, any);
}
#[inline]
fn remove_by_id(&mut self, id: &str) {
if let Some(index) = self.0.iter().position(|c| c.id() == id) { if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index); self.0.remove(index);
} }
} }
AnyOp::ReplaceById(id, any) => {
#[inline]
fn replace_by_id(&mut self, id: &str, any: AnyComponent) {
for c in self.0.iter_mut() { for c in self.0.iter_mut() {
if c.id() == id { if c.id() == id {
*c = any; *c = any;
@ -155,38 +199,10 @@ impl MixedComponents {
} }
} }
} }
AnyOp::Reset => self.0.clear(),
}
self
}
#[fn_builder] #[inline]
#[rustfmt::skip] fn reset(&mut self) {
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self { self.0.clear();
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. // MixedComponents GETTERS.