🎨 Recupera el prefijo "alter_" en lugar de "set_"
This commit is contained in:
parent
3e4fc097e1
commit
602108f561
17 changed files with 70 additions and 66 deletions
|
|
@ -8,8 +8,8 @@ use proc_macro_error::proc_macro_error;
|
|||
use quote::{quote, quote_spanned, ToTokens};
|
||||
use syn::{parse_macro_input, parse_str, DeriveInput, ItemFn};
|
||||
|
||||
/// Macro (*attribute*) que asocia a un método `set_` su correspondiente método `with_` para aplicar
|
||||
/// el patrón *builder*.
|
||||
/// Macro (*attribute*) que asocia a un método `alter_` su correspondiente método `with_` para
|
||||
/// aplicar el patrón *builder*.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
|
|
@ -19,7 +19,7 @@ use syn::{parse_macro_input, parse_str, DeriveInput, ItemFn};
|
|||
///
|
||||
/// ```rust#ignore
|
||||
/// #[fn_builder]
|
||||
/// pub fn set_example(&mut self) -> &mut Self {
|
||||
/// pub fn alter_example(&mut self) -> &mut Self {
|
||||
/// // implementación
|
||||
/// }
|
||||
/// ```
|
||||
|
|
@ -29,32 +29,32 @@ use syn::{parse_macro_input, parse_str, DeriveInput, ItemFn};
|
|||
/// ```rust#ignore
|
||||
/// #[inline]
|
||||
/// pub fn with_example(mut self) -> Self {
|
||||
/// self.set_example();
|
||||
/// self.alter_example();
|
||||
/// self
|
||||
/// }
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let fn_set = parse_macro_input!(item as ItemFn);
|
||||
let fn_set_name = fn_set.sig.ident.to_string();
|
||||
let fn_alter = parse_macro_input!(item as ItemFn);
|
||||
let fn_alter_name = fn_alter.sig.ident.to_string();
|
||||
|
||||
if !fn_set_name.starts_with("set_") {
|
||||
if !fn_alter_name.starts_with("alter_") {
|
||||
let expanded = quote_spanned! {
|
||||
fn_set.sig.ident.span() =>
|
||||
compile_error!("expected a \"pub fn set_...() -> &mut Self\" method");
|
||||
fn_alter.sig.ident.span() =>
|
||||
compile_error!("expected a \"pub fn alter_...() -> &mut Self\" method");
|
||||
};
|
||||
return expanded.into();
|
||||
}
|
||||
|
||||
let fn_with_name = fn_set_name.replace("set_", "with_");
|
||||
let fn_with_generics = if fn_set.sig.generics.params.is_empty() {
|
||||
let fn_with_name = fn_alter_name.replace("alter_", "with_");
|
||||
let fn_with_generics = if fn_alter.sig.generics.params.is_empty() {
|
||||
fn_with_name.clone()
|
||||
} else {
|
||||
let g = &fn_set.sig.generics;
|
||||
let g = &fn_alter.sig.generics;
|
||||
format!("{fn_with_name}{}", quote! { #g }.to_string())
|
||||
};
|
||||
|
||||
let where_clause = fn_set
|
||||
let where_clause = fn_alter
|
||||
.sig
|
||||
.generics
|
||||
.where_clause
|
||||
|
|
@ -63,7 +63,7 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||
format!("{} ", quote! { #where_clause }.to_string())
|
||||
});
|
||||
|
||||
let args: Vec<String> = fn_set
|
||||
let args: Vec<String> = fn_alter
|
||||
.sig
|
||||
.inputs
|
||||
.iter()
|
||||
|
|
@ -85,14 +85,14 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||
#[rustfmt::skip]
|
||||
let fn_with = parse_str::<ItemFn>(format!(r##"
|
||||
pub fn {fn_with_generics}(mut self, {}) -> Self {where_clause} {{
|
||||
self.{fn_set_name}({});
|
||||
self.{fn_alter_name}({});
|
||||
self
|
||||
}}
|
||||
"##, args.join(", "), params.join(", ")
|
||||
).as_str()).unwrap();
|
||||
|
||||
#[rustfmt::skip]
|
||||
let fn_set_doc = format!(r##"
|
||||
let fn_alter_doc = format!(r##"
|
||||
<p id="method.{fn_with_name}" style="margin-bottom: 12px;">Use
|
||||
<code class="code-header">pub fn <span class="fn" href="#method.{fn_with_name}">{fn_with_name}</span>(self, …) -> Self</code>
|
||||
for the <a href="#method.new">builder pattern</a>.
|
||||
|
|
@ -103,8 +103,8 @@ pub fn fn_builder(_: TokenStream, item: TokenStream) -> TokenStream {
|
|||
#[doc(hidden)]
|
||||
#fn_with
|
||||
#[inline]
|
||||
#[doc = #fn_set_doc]
|
||||
#fn_set
|
||||
#[doc = #fn_alter_doc]
|
||||
#fn_alter
|
||||
};
|
||||
expanded.into()
|
||||
}
|
||||
|
|
@ -130,7 +130,7 @@ pub fn derive_component_classes(input: TokenStream) -> TokenStream {
|
|||
let name = &input.ident;
|
||||
|
||||
#[rustfmt::skip]
|
||||
let fn_set_doc = format!(r##"
|
||||
let fn_alter_doc = format!(r##"
|
||||
<p id="method.with_classes">Use
|
||||
<code class="code-header"><span class="fn" href="#method.with_classes">with_classes</span>(self, …) -> Self</code>
|
||||
to apply the <a href="#method.new">builder pattern</a>.
|
||||
|
|
@ -140,9 +140,9 @@ pub fn derive_component_classes(input: TokenStream) -> TokenStream {
|
|||
let expanded = quote! {
|
||||
impl ComponentClasses for #name {
|
||||
#[inline]
|
||||
#[doc = #fn_set_doc]
|
||||
fn set_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
|
||||
self.classes.set_value(op, classes);
|
||||
#[doc = #fn_alter_doc]
|
||||
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
|
||||
self.classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ impl<C: ComponentTrait> AfterRender<C> {
|
|||
}
|
||||
|
||||
pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
|
||||
self.referer_id.set_value(id);
|
||||
self.referer_id.alter_value(id);
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ impl<C: ComponentTrait> BeforeRender<C> {
|
|||
}
|
||||
|
||||
pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
|
||||
self.referer_id.set_value(id);
|
||||
self.referer_id.alter_value(id);
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ impl<C: ComponentTrait> IsRenderable<C> {
|
|||
}
|
||||
|
||||
pub fn filter_by_referer_id(mut self, id: impl Into<String>) -> Self {
|
||||
self.referer_id.set_value(id);
|
||||
self.referer_id.alter_value(id);
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ impl Fluent {
|
|||
Fluent(l10n)
|
||||
}
|
||||
|
||||
pub fn set_l10n(&mut self, l10n: L10n) -> &mut Self {
|
||||
pub fn alter_l10n(&mut self, l10n: L10n) -> &mut Self {
|
||||
self.0 = l10n;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ impl Html {
|
|||
Html(html)
|
||||
}
|
||||
|
||||
pub fn set_html(&mut self, html: Markup) -> &mut Self {
|
||||
pub fn alter_html(&mut self, html: Markup) -> &mut Self {
|
||||
self.0 = html;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ impl Children {
|
|||
// Children BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, op: ChildOp) -> &mut Self {
|
||||
pub fn alter_value(&mut self, op: ChildOp) -> &mut Self {
|
||||
match op {
|
||||
ChildOp::Add(any) => self.add(any),
|
||||
ChildOp::InsertAfterId(id, any) => self.insert_after_id(id, any),
|
||||
|
|
@ -116,7 +116,7 @@ impl Children {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
|
||||
pub fn alter_typed<C: ComponentTrait + Default>(&mut self, op: TypedOp<C>) -> &mut Self {
|
||||
match op {
|
||||
TypedOp::Add(typed) => self.add(typed.to_child()),
|
||||
TypedOp::InsertAfterId(id, typed) => self.insert_after_id(id, typed.to_child()),
|
||||
|
|
|
|||
|
|
@ -6,14 +6,14 @@ pub trait ComponentClassesOp {
|
|||
}
|
||||
|
||||
pub trait ComponentClasses: ComponentBase + ComponentClassesOp {
|
||||
fn set_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self;
|
||||
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self;
|
||||
|
||||
fn classes(&self) -> &OptionClasses;
|
||||
}
|
||||
|
||||
impl<C: ComponentBase + ComponentClasses> ComponentClassesOp for C {
|
||||
fn with_classes(mut self, op: ClassesOp, classes: impl Into<String>) -> Self {
|
||||
self.set_classes(op, classes);
|
||||
self.alter_classes(op, classes);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@ impl Context {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_assets(&mut self, op: AssetsOp) -> &mut Self {
|
||||
pub fn alter_assets(&mut self, op: AssetsOp) -> &mut Self {
|
||||
match op {
|
||||
AssetsOp::LangId(langid) => {
|
||||
self.langid = langid;
|
||||
|
|
@ -109,12 +109,16 @@ impl Context {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn set_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
|
||||
self.regions.set_in_region(region, op);
|
||||
pub fn alter_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
|
||||
self.regions.alter_in_region(region, op);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_param<T: FromStr + ToString>(&mut self, key: &'static str, value: &T) -> &mut Self {
|
||||
pub fn alter_param<T: FromStr + ToString>(
|
||||
&mut self,
|
||||
key: &'static str,
|
||||
value: &T,
|
||||
) -> &mut Self {
|
||||
self.params.insert(key, value.to_string());
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,9 +24,9 @@ impl ChildrenInRegions {
|
|||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
|
||||
pub fn alter_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
|
||||
if let Some(region) = self.0.get_mut(region) {
|
||||
region.set_value(op);
|
||||
region.alter_value(op);
|
||||
} else {
|
||||
self.0.insert(region, Children::new().with_value(op));
|
||||
}
|
||||
|
|
@ -56,18 +56,18 @@ impl InRegion {
|
|||
COMMON_REGIONS
|
||||
.write()
|
||||
.unwrap()
|
||||
.set_in_region("content", ChildOp::Add(child));
|
||||
.alter_in_region("content", ChildOp::Add(child));
|
||||
}
|
||||
InRegion::Named(name) => {
|
||||
COMMON_REGIONS
|
||||
.write()
|
||||
.unwrap()
|
||||
.set_in_region(name, ChildOp::Add(child));
|
||||
.alter_in_region(name, ChildOp::Add(child));
|
||||
}
|
||||
InRegion::OfLayout(region, layout) => {
|
||||
let mut regions = LAYOUT_REGIONS.write().unwrap();
|
||||
if let Some(r) = regions.get_mut(&layout.type_id()) {
|
||||
r.set_in_region(region, ChildOp::Add(child));
|
||||
r.alter_in_region(region, ChildOp::Add(child));
|
||||
} else {
|
||||
regions.insert(layout.type_id(), ChildrenInRegions::with(region, child));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ impl OptionClasses {
|
|||
// OptionClasses BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
|
||||
pub fn alter_value(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
|
||||
let classes: String = classes.into();
|
||||
let classes: Vec<&str> = classes.split_ascii_whitespace().collect();
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ impl<C: ComponentTrait> OptionComponent<C> {
|
|||
// OptionComponent BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, component: Option<C>) -> &mut Self {
|
||||
pub fn alter_value(&mut self, component: Option<C>) -> &mut Self {
|
||||
if let Some(component) = component {
|
||||
self.0 = Some(TypedComponent::with(component));
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ impl OptionId {
|
|||
// OptionId BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, value: impl Into<String>) -> &mut Self {
|
||||
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
|
||||
self.0 = Some(value.into().trim().replace(' ', "_"));
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ impl OptionName {
|
|||
// OptionName BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, value: impl Into<String>) -> &mut Self {
|
||||
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
|
||||
self.0 = Some(value.into().trim().replace(' ', "_"));
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ impl OptionString {
|
|||
// OptionString BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, value: impl Into<String>) -> &mut Self {
|
||||
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
|
||||
self.0 = Some(value.into().trim().to_owned());
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ impl OptionTranslated {
|
|||
// OptionTranslated BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_value(&mut self, value: L10n) -> &mut Self {
|
||||
pub fn alter_value(&mut self, value: L10n) -> &mut Self {
|
||||
self.0 = value;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,68 +44,68 @@ impl Page {
|
|||
// Page BUILDER.
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_title(&mut self, title: L10n) -> &mut Self {
|
||||
self.title.set_value(title);
|
||||
pub fn alter_title(&mut self, title: L10n) -> &mut Self {
|
||||
self.title.alter_value(title);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_description(&mut self, description: L10n) -> &mut Self {
|
||||
self.description.set_value(description);
|
||||
pub fn alter_description(&mut self, description: L10n) -> &mut Self {
|
||||
self.description.alter_value(description);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_metadata(&mut self, name: &'static str, content: &'static str) -> &mut Self {
|
||||
pub fn alter_metadata(&mut self, name: &'static str, content: &'static str) -> &mut Self {
|
||||
self.metadata.push((name, content));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_property(&mut self, property: &'static str, content: &'static str) -> &mut Self {
|
||||
pub fn alter_property(&mut self, property: &'static str, content: &'static str) -> &mut Self {
|
||||
self.metadata.push((property, content));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_assets(&mut self, op: AssetsOp) -> &mut Self {
|
||||
self.context.set_assets(op);
|
||||
pub fn alter_assets(&mut self, op: AssetsOp) -> &mut Self {
|
||||
self.context.alter_assets(op);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_body_id(&mut self, id: impl Into<String>) -> &mut Self {
|
||||
self.body_id.set_value(id);
|
||||
pub fn alter_body_id(&mut self, id: impl Into<String>) -> &mut Self {
|
||||
self.body_id.alter_value(id);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_body_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
|
||||
self.body_classes.set_value(op, classes);
|
||||
pub fn alter_body_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
|
||||
self.body_classes.alter_value(op, classes);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_body_skip_to(&mut self, id: impl Into<String>) -> &mut Self {
|
||||
self.body_skip_to.set_value(id);
|
||||
pub fn alter_body_skip_to(&mut self, id: impl Into<String>) -> &mut Self {
|
||||
self.body_skip_to.alter_value(id);
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_layout(&mut self, layout: &'static str) -> &mut Self {
|
||||
self.context.set_assets(AssetsOp::Layout(layout));
|
||||
pub fn alter_layout(&mut self, layout: &'static str) -> &mut Self {
|
||||
self.context.alter_assets(AssetsOp::Layout(layout));
|
||||
self
|
||||
}
|
||||
|
||||
#[fn_builder]
|
||||
pub fn set_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
|
||||
self.context.set_in_region(region, op);
|
||||
pub fn alter_in_region(&mut self, region: &'static str, op: ChildOp) -> &mut Self {
|
||||
self.context.alter_in_region(region, op);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_component(mut self, component: impl ComponentTrait) -> Self {
|
||||
self.context
|
||||
.set_in_region("content", ChildOp::Add(ChildComponent::with(component)));
|
||||
.alter_in_region("content", ChildOp::Add(ChildComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
@ -115,7 +115,7 @@ impl Page {
|
|||
component: impl ComponentTrait,
|
||||
) -> Self {
|
||||
self.context
|
||||
.set_in_region(region, ChildOp::Add(ChildComponent::with(component)));
|
||||
.alter_in_region(region, ChildOp::Add(ChildComponent::with(component)));
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue