🎨 [macros] Redefine #[fn_builder] con coherencia

La macro genera automáticamente un método "alter_", que modifica la
instancia actual usando "&mut self", y redefine el método "with_" para
delegar la lógica en el nuevo método "alter_".
This commit is contained in:
Manuel Cillero 2025-01-02 09:24:56 +01:00
parent 4db4d791a5
commit febb9bc9cb
13 changed files with 176 additions and 137 deletions

View file

@ -88,7 +88,7 @@ impl Children {
}
pub fn with(child: ChildComponent) -> Self {
Children::default().with_value(ChildOp::Add(child))
Children::default().with_child(ChildOp::Add(child))
}
pub(crate) fn merge(mixes: &[Option<&Children>]) -> Self {
@ -102,7 +102,7 @@ impl Children {
// Children BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: ChildOp) -> &mut Self {
pub fn with_child(mut self, op: ChildOp) -> Self {
match op {
ChildOp::Add(any) => self.add(any),
ChildOp::InsertAfterId(id, any) => self.insert_after_id(id, any),
@ -111,12 +111,11 @@ impl Children {
ChildOp::RemoveById(id) => self.remove_by_id(id),
ChildOp::ReplaceById(id, any) => self.replace_by_id(id, any),
ChildOp::Reset => self.reset(),
};
self
}
}
#[fn_builder]
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
pub fn with_typed<C: ComponentTrait + Default>(mut self, op: TypedOp<C>) -> Self {
match op {
TypedOp::Add(typed) => self.add(typed.to_child()),
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.to_child()),
@ -125,56 +124,62 @@ impl Children {
TypedOp::RemoveById(id) => self.remove_by_id(id),
TypedOp::ReplaceById(id, typed) => self.replace_by_id(id, typed.to_child()),
TypedOp::Reset => self.reset(),
}
}
#[inline]
pub fn add(&mut self, child: ChildComponent) -> &mut Self {
self.0.push(child);
self
}
#[inline]
fn insert_after_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, child),
_ => self.0.push(child),
};
self
}
#[inline]
fn add(&mut self, child: ChildComponent) {
self.0.push(child);
}
#[inline]
fn insert_after_id(&mut self, id: &str, child: ChildComponent) {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index + 1, child),
_ => self.0.push(child),
};
}
#[inline]
fn insert_before_id(&mut self, id: &str, child: ChildComponent) {
fn insert_before_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
match self.0.iter().position(|c| c.id() == id) {
Some(index) => self.0.insert(index, child),
_ => self.0.insert(0, child),
};
self
}
#[inline]
fn prepend(&mut self, child: ChildComponent) {
fn prepend(&mut self, child: ChildComponent) -> &mut Self {
self.0.insert(0, child);
self
}
#[inline]
fn remove_by_id(&mut self, id: &str) {
fn remove_by_id(&mut self, id: &str) -> &mut Self {
if let Some(index) = self.0.iter().position(|c| c.id() == id) {
self.0.remove(index);
}
self
}
#[inline]
fn replace_by_id(&mut self, id: &str, child: ChildComponent) {
fn replace_by_id(&mut self, id: &str, child: ChildComponent) -> &mut Self {
for c in &mut self.0 {
if c.id() == id {
*c = child;
break;
}
}
self
}
#[inline]
fn reset(&mut self) {
fn reset(&mut self) -> &mut Self {
self.0.clear();
self
}
// Children GETTERS.

View file

@ -24,11 +24,11 @@ impl ChildrenInRegions {
}
#[fn_builder]
pub fn alter_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
pub fn with_in_region(mut self, region: &'static str, op: ChildOp) -> Self {
if let Some(region) = self.0.get_mut(region) {
region.alter_value(op);
region.alter_child(op);
} else {
self.0.insert(region, Children::new().with_value(op));
self.0.insert(region, Children::new().with_child(op));
}
self
}

View file

@ -31,7 +31,7 @@ impl OptionClasses {
// OptionClasses BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
pub fn with_value(mut self, op: ClassesOp, classes: impl Into<String>) -> Self {
let classes: String = classes.into();
let classes: Vec<&str> = classes.split_ascii_whitespace().collect();

View file

@ -18,7 +18,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
// OptionComponent BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
pub fn with_value(mut self, component: Option<C>) -> Self {
if let Some(component) = component {
self.0 = Some(TypedComponent::with(component));
} else {

View file

@ -11,7 +11,7 @@ impl OptionId {
// OptionId BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.0 = Some(value.into().trim().replace(' ', "_"));
self
}

View file

@ -11,7 +11,7 @@ impl OptionName {
// OptionName BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.0 = Some(value.into().trim().replace(' ', "_"));
self
}

View file

@ -11,7 +11,7 @@ impl OptionString {
// OptionString BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
pub fn with_value(mut self, value: impl Into<String>) -> Self {
self.0 = Some(value.into().trim().to_owned());
self
}

View file

@ -13,7 +13,7 @@ impl OptionTranslated {
// OptionTranslated BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: L10n) -> &mut Self {
pub fn with_value(mut self, value: L10n) -> Self {
self.0 = value;
self
}

View file

@ -42,61 +42,61 @@ impl Page {
// Page BUILDER.
#[fn_builder]
pub fn alter_title(&mut self, title: L10n) -> &mut Self {
pub fn with_title(mut self, title: L10n) -> Self {
self.title.alter_value(title);
self
}
#[fn_builder]
pub fn alter_description(&mut self, description: L10n) -> &mut Self {
pub fn with_description(mut self, description: L10n) -> Self {
self.description.alter_value(description);
self
}
#[fn_builder]
pub fn alter_metadata(&mut self, name: &'static str, content: &'static str) -> &mut Self {
pub fn with_metadata(mut self, name: &'static str, content: &'static str) -> Self {
self.metadata.push((name, content));
self
}
#[fn_builder]
pub fn alter_property(&mut self, property: &'static str, content: &'static str) -> &mut Self {
pub fn with_property(mut self, property: &'static str, content: &'static str) -> Self {
self.metadata.push((property, content));
self
}
#[fn_builder]
pub fn alter_assets(&mut self, op: AssetsOp) -> &mut Self {
pub fn with_assets(mut self, op: AssetsOp) -> Self {
self.context.alter_assets(op);
self
}
#[fn_builder]
pub fn alter_body_id(&mut self, id: impl Into<String>) -> &mut Self {
pub fn with_body_id(mut self, id: impl Into<String>) -> Self {
self.body_id.alter_value(id);
self
}
#[fn_builder]
pub fn alter_body_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
pub fn with_body_classes(mut self, op: ClassesOp, classes: impl Into<String>) -> Self {
self.body_classes.alter_value(op, classes);
self
}
#[fn_builder]
pub fn alter_theme(&mut self, theme: &'static str) -> &mut Self {
pub fn with_theme(mut self, theme: &'static str) -> Self {
self.context.alter_assets(AssetsOp::Theme(theme));
self
}
#[fn_builder]
pub fn alter_layout(&mut self, layout: &'static str) -> &mut Self {
pub fn with_layout(mut self, layout: &'static str) -> Self {
self.context.alter_assets(AssetsOp::Layout(layout));
self
}
#[fn_builder]
pub fn alter_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
pub fn with_in_region(mut self, region: &'static str, op: ChildOp) -> Self {
self.context.alter_in_region(region, op);
self
}