From f081a00bd42caf5c0d9f47070e4df5d65bc48d63 Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Sat, 28 Jan 2023 09:05:10 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Macro=20#[fn=5Fbuilder]=20?= =?UTF-8?q?para=20constructor=20de=20p=C3=A1ginas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagetop-admin/src/summary.rs | 6 +- pagetop-user/src/lib.rs | 2 +- pagetop/src/base/module/homepage.rs | 10 +-- pagetop/src/response/fatal_error.rs | 8 +-- pagetop/src/response/page/definition.rs | 88 +++++++------------------ 5 files changed, 38 insertions(+), 76 deletions(-) diff --git a/pagetop-admin/src/summary.rs b/pagetop-admin/src/summary.rs index 168d5a10..e3de92be 100644 --- a/pagetop-admin/src/summary.rs +++ b/pagetop-admin/src/summary.rs @@ -43,8 +43,8 @@ pub async fn summary() -> ResultPage { Page::new() .with_context(ContextOp::SetTheme("Bootsier")) .with_title("Admin") - .add_to("top-menu", top_menu) - .add_to( + .with_this_in("top-menu", top_menu) + .with_this_in( "region-content", grid::Row::new() .with_column(grid::Column::new().with_component(side_menu)) @@ -52,6 +52,6 @@ pub async fn summary() -> ResultPage { p { "Columna 2"} }))), ) - .using_template("admin") + .with_template("admin") .render() } diff --git a/pagetop-user/src/lib.rs b/pagetop-user/src/lib.rs index 5ea54320..0d01c771 100644 --- a/pagetop-user/src/lib.rs +++ b/pagetop-user/src/lib.rs @@ -38,7 +38,7 @@ impl ModuleTrait for User { async fn login() -> ResultPage { Page::new() .with_title("Identificación del usuario") - .add_to( + .with_this_in( "region-content", Container::new() .with_id("welcome") diff --git a/pagetop/src/base/module/homepage.rs b/pagetop/src/base/module/homepage.rs index f71a04a4..7ccebc69 100644 --- a/pagetop/src/base/module/homepage.rs +++ b/pagetop/src/base/module/homepage.rs @@ -31,11 +31,11 @@ async fn demo() -> ResultPage { "/theme/module/homepage/styles.css", ))) .with_body_classes(ClassesOp::AddFirst, "default-homepage") - .add_to("region-content", hello_world()) - .add_to("region-content", welcome()) - .add_to("region-content", about_pagetop()) - .add_to("region-content", promo_pagetop()) - .add_to("region-content", reporting_issues()) + .with_this_in("region-content", hello_world()) + .with_this_in("region-content", welcome()) + .with_this_in("region-content", about_pagetop()) + .with_this_in("region-content", promo_pagetop()) + .with_this_in("region-content", reporting_issues()) .render() } diff --git a/pagetop/src/response/fatal_error.rs b/pagetop/src/response/fatal_error.rs index 00b82e91..591b0472 100644 --- a/pagetop/src/response/fatal_error.rs +++ b/pagetop/src/response/fatal_error.rs @@ -28,8 +28,8 @@ impl fmt::Display for FatalError { let error_content = error_page.context().theme().error_403_access_denied(); if let Ok(page) = error_page .with_title("Error FORBIDDEN") - .using_template("error") - .add_to("region-content", error_content) + .with_this_in("region-content", error_content) + .with_template("error") .render() { write!(f, "{}", page.into_string()) @@ -43,8 +43,8 @@ impl fmt::Display for FatalError { let error_content = error_page.context().theme().error_404_not_found(); if let Ok(page) = error_page .with_title("Error RESOURCE NOT FOUND") - .using_template("error") - .add_to("region-content", error_content) + .with_this_in("region-content", error_content) + .with_template("error") .render() { write!(f, "{}", page.into_string()) diff --git a/pagetop/src/response/page/definition.rs b/pagetop/src/response/page/definition.rs index 0adcd6be..5b6360e2 100644 --- a/pagetop/src/response/page/definition.rs +++ b/pagetop/src/response/page/definition.rs @@ -4,7 +4,7 @@ use crate::core::component::*; use crate::core::hook::{action_ref, run_actions}; use crate::html::{html, AttributeValue, Classes, ClassesOp, Favicon, Markup, DOCTYPE}; use crate::response::FatalError; -use crate::{config, trace, LazyStatic}; +use crate::{config, fn_builder, trace, LazyStatic}; use std::collections::HashMap; @@ -88,73 +88,13 @@ impl Page { // Page BUILDER. - pub fn with_language(mut self, language: &str) -> Self { - self.alter_language(language); - self - } - - pub fn with_direction(mut self, dir: TextDirection) -> Self { - self.alter_direction(dir); - self - } - - pub fn with_title(mut self, title: &str) -> Self { - self.alter_title(title); - self - } - - pub fn with_description(mut self, description: &str) -> Self { - self.alter_description(description); - self - } - - pub fn with_metadata(mut self, name: &'static str, content: &'static str) -> Self { - self.alter_metadata(name, content); - self - } - - pub fn with_property(mut self, property: &'static str, content: &'static str) -> Self { - self.alter_property(property, content); - self - } - - pub fn with_favicon(mut self, favicon: Option) -> Self { - self.alter_favicon(favicon); - self - } - - pub fn with_context(mut self, op: ContextOp) -> Self { - self.alter_context(op); - self - } - - pub fn with_body_classes(mut self, op: ClassesOp, classes: &str) -> Self { - self.alter_body_classes(op, classes); - self - } - - pub fn add_to(mut self, region: &'static str, component: impl ComponentTrait) -> Self { - if let Some(regions) = self.regions.get_mut(region) { - regions.add(component); - } else { - self.regions - .insert(region, ComponentsBundle::new_with(component)); - } - self - } - - pub fn using_template(mut self, template: &str) -> Self { - self.alter_template(template); - self - } - - // Page ALTER. - + #[fn_builder] pub fn alter_language(&mut self, language: &str) -> &mut Self { self.language.alter_value(language); self } + #[fn_builder] pub fn alter_direction(&mut self, dir: TextDirection) -> &mut Self { self.direction.alter_value(match dir { TextDirection::Auto => "auto", @@ -164,41 +104,63 @@ impl Page { self } + #[fn_builder] pub fn alter_title(&mut self, title: &str) -> &mut Self { self.title.alter_value(title); self } + #[fn_builder] pub fn alter_description(&mut self, description: &str) -> &mut Self { self.description.alter_value(description); self } + #[fn_builder] pub fn alter_metadata(&mut self, name: &'static str, content: &'static str) -> &mut Self { self.metadata.push((name, content)); self } + #[fn_builder] pub fn alter_property(&mut self, property: &'static str, content: &'static str) -> &mut Self { self.metadata.push((property, content)); self } + #[fn_builder] pub fn alter_favicon(&mut self, favicon: Option) -> &mut Self { self.favicon = favicon; self } + #[fn_builder] pub fn alter_context(&mut self, op: ContextOp) -> &mut Self { self.context.alter(op); self } + #[fn_builder] pub fn alter_body_classes(&mut self, op: ClassesOp, classes: &str) -> &mut Self { self.body_classes.alter_value(op, classes); self } + #[fn_builder] + pub fn alter_this_in( + &mut self, + region: &'static str, + component: impl ComponentTrait, + ) -> &mut Self { + if let Some(regions) = self.regions.get_mut(region) { + regions.add(component); + } else { + self.regions.insert(region, ComponentsBundle::new_with(component)); + } + self + } + + #[fn_builder] pub fn alter_template(&mut self, template: &str) -> &mut Self { self.template = template.to_owned(); self