🧑‍💻 Major update to enhance Handle usage

This commit is contained in:
Manuel Cillero 2023-11-19 21:17:16 +01:00
parent 255fb393a9
commit d9f3561832
56 changed files with 201 additions and 353 deletions

View file

@ -1,9 +1,8 @@
use pagetop::prelude::*;
#[derive(AssignHandle)]
struct Drust;
impl_handle!(APP_DRUST for Drust);
impl ModuleTrait for Drust {
fn dependencies(&self) -> Vec<ModuleRef> {
vec![

View file

@ -1,9 +1,8 @@
use pagetop::prelude::*;
#[derive(AssignHandle)]
struct HelloName;
impl_handle!(APP_HELLO_NAME for HelloName);
impl ModuleTrait for HelloName {
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
scfg.service(hello_name);

View file

@ -1,9 +1,8 @@
use pagetop::prelude::*;
#[derive(AssignHandle)]
struct HelloWorld;
impl_handle!(APP_HELLO_WORLD for HelloWorld);
impl ModuleTrait for HelloWorld {
fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
scfg.route("/", service::web::get().to(hello_world));

View file

@ -4,10 +4,9 @@ static_locales!(LOCALES_ADMIN);
mod summary;
#[derive(AssignHandle)]
pub struct Admin;
impl_handle!(MODULE_ADMIN for Admin);
impl ModuleTrait for Admin {
fn name(&self) -> L10n {
L10n::t("module_name", &LOCALES_ADMIN)

View file

@ -4,10 +4,9 @@ static_locales!(LOCALES_BOOTSIER);
static_files!(bootsier);
#[derive(AssignHandle)]
pub struct Bootsier;
impl_handle!(THEME_BOOTSIER for Bootsier);
impl ModuleTrait for Bootsier {
fn theme(&self) -> Option<ThemeRef> {
Some(&Bootsier)
@ -83,7 +82,7 @@ impl ThemeTrait for Bootsier {
fn before_prepare_component(&self, component: &mut dyn ComponentTrait, _cx: &mut Context) {
match component.handle() {
COMPONENT_BASE_ICON => {
h if Icon::matches_handle(h) => {
if let Some(i) = component_as_mut::<Icon>(component) {
match i.font_size() {
FontSize::ExtraLarge => {
@ -105,7 +104,7 @@ impl ThemeTrait for Bootsier {
};
}
}
COMPONENT_BASE_BUTTON => {
h if Button::matches_handle(h) => {
if let Some(b) = component_as_mut::<Button>(component) {
match b.font_size() {
FontSize::ExtraLarge => {
@ -135,7 +134,7 @@ impl ThemeTrait for Bootsier {
};
}
}
COMPONENT_BASE_HEADING => {
h if Heading::matches_handle(h) => {
if let Some(h) = component_as_mut::<Heading>(component) {
match h.display() {
HeadingDisplay::ExtraLarge => {
@ -157,7 +156,7 @@ impl ThemeTrait for Bootsier {
};
}
}
COMPONENT_BASE_PARAGRAPH => {
h if Paragraph::matches_handle(h) => {
if let Some(p) = component_as_mut::<Paragraph>(component) {
match p.font_size() {
FontSize::ExtraLarge => {
@ -185,7 +184,7 @@ impl ThemeTrait for Bootsier {
fn render_component(&self, component: &dyn ComponentTrait, cx: &mut Context) -> Option<Markup> {
match component.handle() {
ERROR_404 => Some(html! {
h if Error404::matches_handle(h) => Some(html! {
div class="jumbotron" {
div class="media" {
img

View file

@ -2,10 +2,9 @@ use pagetop::prelude::*;
static_files!(bulmix);
#[derive(AssignHandle)]
pub struct Bulmix;
impl_handle!(THEME_BULMIX for Bulmix);
impl ModuleTrait for Bulmix {
fn theme(&self) -> Option<ThemeRef> {
Some(&Bulmix)
@ -32,7 +31,7 @@ impl ThemeTrait for Bulmix {
fn before_prepare_component(&self, component: &mut dyn ComponentTrait, _cx: &mut Context) {
match component.handle() {
COMPONENT_BASE_ICON => {
h if Icon::matches_handle(h) => {
if let Some(i) = component_as_mut::<Icon>(component) {
match i.font_size() {
FontSize::ExtraLarge => {
@ -54,7 +53,7 @@ impl ThemeTrait for Bulmix {
};
}
}
COMPONENT_BASE_BUTTON => {
h if Button::matches_handle(h) => {
if let Some(b) = component_as_mut::<Button>(component) {
match b.font_size() {
FontSize::ExtraLarge => {
@ -84,7 +83,7 @@ impl ThemeTrait for Bulmix {
};
}
}
COMPONENT_BASE_HEADING => {
h if Heading::matches_handle(h) => {
if let Some(h) = component_as_mut::<Heading>(component) {
match h.display() {
HeadingDisplay::Subtitle => {
@ -94,7 +93,7 @@ impl ThemeTrait for Bulmix {
};
}
}
COMPONENT_BASE_PARAGRAPH => {
h if Paragraph::matches_handle(h) => {
if let Some(p) = component_as_mut::<Paragraph>(component) {
p.add_classes("block");
match p.font_size() {
@ -127,7 +126,7 @@ impl ThemeTrait for Bulmix {
_cx: &mut Context,
) -> Option<Markup> {
match component.handle() {
COMPONENT_BASE_ICON => {
h if Icon::matches_handle(h) => {
if let Some(i) = component_as_ref::<Icon>(component) {
return match i.icon_name().get() {
None => None,

View file

@ -4,10 +4,9 @@ static_locales!(LOCALES_HOMEDEMO);
static_files!(homedemo);
#[derive(AssignHandle)]
pub struct HomeDemo;
impl_handle!(MODULE_HOMEDEMO for HomeDemo);
impl ModuleTrait for HomeDemo {
fn name(&self) -> L10n {
L10n::t("module_name", &LOCALES_HOMEDEMO)

View file

@ -2,9 +2,10 @@ mod maud;
use concat_string::concat_string;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::proc_macro_error;
use quote::{quote, quote_spanned, ToTokens};
use syn::{parse_macro_input, parse_str, ItemFn};
use quote::{format_ident, quote, quote_spanned, ToTokens};
use syn::{parse_macro_input, parse_str, DeriveInput, ItemFn};
#[proc_macro]
#[proc_macro_error]
@ -100,3 +101,66 @@ pub fn test(_: TokenStream, item: TokenStream) -> TokenStream {
output.extend(item);
output
}
#[proc_macro_derive(ComponentClasses)]
pub fn component_classes_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let expanded = quote! {
impl ComponentClasses for #name {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
};
TokenStream::from(expanded)
}
#[proc_macro_derive(CrateHandle, attributes(handle))]
pub fn crate_handle_derive(input: TokenStream) -> TokenStream {
impl_handle(input, quote! { crate })
}
#[proc_macro_derive(AssignHandle, attributes(handle))]
pub fn assign_handle_derive(input: TokenStream) -> TokenStream {
impl_handle(input, quote! { pagetop })
}
fn impl_handle(input: TokenStream, crate_name: TokenStream2) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let name = &input.ident;
let handle_name = format_ident!("HANDLE_{}", name.to_string().to_uppercase());
let expanded = quote! {
const #handle_name: #crate_name::Handle =
#crate_name::util::handle(module_path!(), file!(), line!(), column!());
impl #impl_generics #crate_name::ImplementHandle for #name #ty_generics #where_clause {
#[inline]
fn static_handle() -> #crate_name::Handle {
#handle_name
}
#[inline]
fn matches_handle(is: #crate_name::Handle) -> bool {
is == #handle_name
}
#[inline]
fn handle(&self) -> #crate_name::Handle {
#handle_name
}
}
};
TokenStream::from(expanded)
}

View file

@ -5,10 +5,9 @@ static_locales!(LOCALES_NODE);
//mod entity;
mod migration;
#[derive(AssignHandle)]
pub struct Node;
impl_handle!(MODULE_NODE for Node);
impl ModuleTrait for Node {
fn name(&self) -> L10n {
L10n::t("module_name", &LOCALES_NODE)

View file

@ -4,10 +4,9 @@ static_locales!(LOCALES_USER);
mod migration;
#[derive(AssignHandle)]
pub struct User;
impl_handle!(MODULE_USER for User);
impl ModuleTrait for User {
fn name(&self) -> L10n {
L10n::t("module_name", &LOCALES_USER)

View file

@ -1,7 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::FnAction;
#[derive(CrateHandle)]
pub struct AfterPrepareComponent<C: ComponentTrait> {
f: FnAction<C>,
referer_handle: Option<Handle>,
@ -9,8 +11,6 @@ pub struct AfterPrepareComponent<C: ComponentTrait> {
weight: Weight,
}
impl_handle!(ACTION_AFTER_PREPARE_COMPONENT for AfterPrepareComponent<ComponentTrait>);
impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
fn referer_handle(&self) -> Option<Handle> {
self.referer_handle

View file

@ -1,7 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::FnAction;
#[derive(CrateHandle)]
pub struct BeforePrepareComponent<C: ComponentTrait> {
f: FnAction<C>,
referer_handle: Option<Handle>,
@ -9,8 +11,6 @@ pub struct BeforePrepareComponent<C: ComponentTrait> {
weight: Weight,
}
impl_handle!(ACTION_BEFORE_PREPARE_COMPONENT for BeforePrepareComponent<ComponentTrait>);
impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
fn referer_handle(&self) -> Option<Handle> {
self.referer_handle

View file

@ -1,14 +1,14 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::FnActionPage;
#[derive(CrateHandle)]
pub struct AfterPrepareBody {
f: FnActionPage,
weight: Weight,
}
impl_handle!(ACTION_AFTER_PREPARE_BODY for AfterPrepareBody);
impl ActionTrait for AfterPrepareBody {
fn weight(&self) -> Weight {
self.weight

View file

@ -1,14 +1,14 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::FnActionPage;
#[derive(CrateHandle)]
pub struct BeforePrepareBody {
f: FnActionPage,
weight: Weight,
}
impl_handle!(ACTION_BEFORE_PREPARE_BODY for BeforePrepareBody);
impl ActionTrait for BeforePrepareBody {
fn weight(&self) -> Weight {
self.weight

View file

@ -124,35 +124,35 @@ impl ToString for FontSize {
// *************************************************************************************************
mod html;
pub use html::{Html, COMPONENT_BASE_HTML};
pub use html::Html;
mod translate;
pub use translate::{Translate, COMPONENT_BASE_TRANSLATE};
pub use translate::Translate;
mod wrapper;
pub use wrapper::{Wrapper, WrapperType, COMPONENT_BASE_WRAPPER};
pub use wrapper::{Wrapper, WrapperType};
pub mod flex;
mod icon;
pub use icon::{Icon, COMPONENT_BASE_ICON};
pub use icon::Icon;
mod heading;
pub use heading::{Heading, HeadingDisplay, HeadingType, COMPONENT_BASE_HEADING};
pub use heading::{Heading, HeadingDisplay, HeadingType};
mod paragraph;
pub use paragraph::{Paragraph, COMPONENT_BASE_PARAGRAPH};
pub use paragraph::Paragraph;
mod button;
pub use button::{Button, ButtonTarget, ButtonType, COMPONENT_BASE_BUTTON};
pub use button::{Button, ButtonTarget, ButtonType};
mod image;
pub use image::{Image, ImageSize, COMPONENT_BASE_IMAGE};
pub use image::{Image, ImageSize};
mod block;
pub use block::{Block, COMPONENT_BASE_BLOCK};
pub use block::Block;
mod branding;
pub use branding::{Branding, COMPONENT_BASE_BRANDING};
pub use branding::Branding;
mod powered_by;
pub use powered_by::{PoweredBy, PoweredByLogo, COMPONENT_BASE_POWEREDBY};
pub use powered_by::{PoweredBy, PoweredByLogo};
pub mod menu;
pub use menu::{Menu, COMPONENT_BASE_MENU};
pub use menu::Menu;
pub mod form;
pub use form::{Form, FormMethod, COMPONENT_BASE_FORM};
pub use form::{Form, FormMethod};

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Block {
id : OptionId,
weight : Weight,
@ -11,8 +12,6 @@ pub struct Block {
stuff : AnyComponents,
}
impl_handle!(COMPONENT_BASE_BLOCK for Block);
impl ComponentTrait for Block {
fn new() -> Self {
Block::default()
@ -51,17 +50,6 @@ impl ComponentTrait for Block {
}
}
impl ComponentClasses for Block {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Block {
// Block BUILDER.

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Branding {
id : OptionId,
weight : Weight,
@ -14,8 +15,6 @@ pub struct Branding {
frontpage : FnContextualPath,
}
impl_handle!(COMPONENT_BASE_BRANDING for Branding);
impl ComponentTrait for Branding {
fn new() -> Self {
Branding::default()

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
pub enum ButtonType {
@ -28,7 +29,7 @@ pub enum ButtonTarget {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Button {
id : OptionId,
weight : Weight,
@ -43,8 +44,6 @@ pub struct Button {
target : ButtonTarget,
}
impl_handle!(COMPONENT_BASE_BUTTON for Button);
impl ComponentTrait for Button {
fn new() -> Self {
Button::default()
@ -92,17 +91,6 @@ impl ComponentTrait for Button {
}
}
impl ComponentClasses for Button {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Button {
pub fn link(href: impl Into<String>, html: L10n) -> Self {
Button::default()

View file

@ -1,7 +1,7 @@
mod container;
pub use container::{Container, COMPONENT_BASE_FLEX_CONTAINER};
pub use container::Container;
mod item;
pub use item::{Item, COMPONENT_BASE_FLEX_ITEM};
pub use item::Item;
use crate::prelude::*;

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Container {
id : OptionId,
weight : Weight,
@ -15,8 +16,6 @@ pub struct Container {
gap : flex::Gap,
}
impl_handle!(COMPONENT_BASE_FLEX_CONTAINER for Container);
impl ComponentTrait for Container {
fn new() -> Self {
Container::default()
@ -62,17 +61,6 @@ impl ComponentTrait for Container {
}
}
impl ComponentClasses for Container {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Container {
// Container BUILDER.

View file

@ -1,12 +1,13 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Item {
id : OptionId,
weight : Weight,
renderable : Renderable,
item_classes : OptionClasses,
classes : OptionClasses,
inner_classes: OptionClasses,
item_grow : flex::ItemGrow,
item_shrink : flex::ItemShrink,
@ -16,8 +17,6 @@ pub struct Item {
stuff : AnyComponents,
}
impl_handle!(COMPONENT_BASE_FLEX_ITEM for Item);
impl ComponentTrait for Item {
fn new() -> Self {
Item::default()
@ -66,17 +65,6 @@ impl ComponentTrait for Item {
}
}
impl ComponentClasses for Item {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.item_classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.item_classes
}
}
impl Item {
// Item BUILDER.

View file

@ -1,11 +1,11 @@
mod form_main;
pub use form_main::{Form, FormMethod, COMPONENT_BASE_FORM};
pub use form_main::{Form, FormMethod};
mod input;
pub use input::{Input, InputType, COMPONENT_BASE_INPUT};
pub use input::{Input, InputType};
mod hidden;
pub use hidden::{Hidden, COMPONENT_BASE_HIDDEN};
pub use hidden::Hidden;
mod date;
pub use date::{Date, COMPONENT_BASE_DATE};
pub use date::Date;
mod button;
pub use button::{Button, ButtonType, COMPONENT_BASE_BUTTON};
pub use button::{Button, ButtonType};

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
pub enum ButtonType {
@ -20,7 +21,7 @@ impl ToString for ButtonType {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Button {
weight : Weight,
renderable : Renderable,
@ -33,8 +34,6 @@ pub struct Button {
template : String,
}
impl_handle!(COMPONENT_BASE_BUTTON for Button);
impl ComponentTrait for Button {
fn new() -> Self {
Button::default()
@ -73,17 +72,6 @@ impl ComponentTrait for Button {
}
}
impl ComponentClasses for Button {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Button {
pub fn with(value: L10n) -> Self {
Button::default().with_value(value)

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Date {
weight : Weight,
renderable : Renderable,
@ -19,8 +20,6 @@ pub struct Date {
template : String,
}
impl_handle!(COMPONENT_BASE_DATE for Date);
impl ComponentTrait for Date {
fn new() -> Self {
Date::default().with_classes(ClassesOp::Add, "form-item form-type-date")
@ -68,17 +67,6 @@ impl ComponentTrait for Date {
}
}
impl ComponentClasses for Date {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Date {
// Date BUILDER.

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
pub enum FormMethod {
@ -8,7 +9,7 @@ pub enum FormMethod {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Form {
id : OptionId,
weight : Weight,
@ -21,8 +22,6 @@ pub struct Form {
template : String,
}
impl_handle!(COMPONENT_BASE_FORM for Form);
impl ComponentTrait for Form {
fn new() -> Self {
Form::default()
@ -61,17 +60,6 @@ impl ComponentTrait for Form {
}
}
impl ComponentClasses for Form {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Form {
// Form BUILDER.

View file

@ -1,15 +1,14 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Hidden {
weight: Weight,
name : OptionName,
value : OptionString,
}
impl_handle!(COMPONENT_BASE_HIDDEN for Hidden);
impl ComponentTrait for Hidden {
fn new() -> Self {
Hidden::default()

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
pub enum InputType {
@ -12,7 +13,7 @@ pub enum InputType {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Input {
weight : Weight,
renderable : Renderable,
@ -34,8 +35,6 @@ pub struct Input {
template : String,
}
impl_handle!(COMPONENT_BASE_INPUT for Input);
impl ComponentTrait for Input {
fn new() -> Self {
Input::default()
@ -98,17 +97,6 @@ impl ComponentTrait for Input {
}
}
impl ComponentClasses for Input {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Input {
pub fn textfield() -> Self {
Input::default()

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
pub enum HeadingType {
@ -39,7 +40,7 @@ impl ToString for HeadingDisplay {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Heading {
id : OptionId,
weight : Weight,
@ -50,8 +51,6 @@ pub struct Heading {
display : HeadingDisplay,
}
impl_handle!(COMPONENT_BASE_HEADING for Heading);
impl ComponentTrait for Heading {
fn new() -> Self {
Heading::default()
@ -89,17 +88,6 @@ impl ComponentTrait for Heading {
}
}
impl ComponentClasses for Heading {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Heading {
pub fn h1(text: L10n) -> Self {
Heading::default()

View file

@ -1,10 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Html(Markup);
impl_handle!(COMPONENT_BASE_HTML for Html);
impl ComponentTrait for Html {
fn new() -> Self {
Html::default()

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Icon {
weight : Weight,
renderable: Renderable,
@ -10,8 +11,6 @@ pub struct Icon {
font_size : FontSize,
}
impl_handle!(COMPONENT_BASE_ICON for Icon);
impl ComponentTrait for Icon {
fn new() -> Self {
Icon::default()
@ -43,17 +42,6 @@ impl ComponentTrait for Icon {
}
}
impl ComponentClasses for Icon {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Icon {
pub fn with(icon_name: impl Into<String>) -> Self {
Icon::default().with_icon_name(icon_name)

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
const IMG_FLUID: &str = "pt-img__fluid";
const IMG_FIXED: &str = "pt-img__fixed";
@ -14,7 +15,7 @@ pub enum ImageSize {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Image {
id : OptionId,
weight : Weight,
@ -24,8 +25,6 @@ pub struct Image {
size : ImageSize,
}
impl_handle!(COMPONENT_BASE_IMAGE for Image);
impl ComponentTrait for Image {
fn new() -> Self {
Image::default().with_classes(ClassesOp::Add, IMG_FLUID)
@ -62,17 +61,6 @@ impl ComponentTrait for Image {
}
}
impl ComponentClasses for Image {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Image {
pub fn with(source: &str) -> Self {
Image::default()

View file

@ -1,17 +1,17 @@
mod menu_main;
pub use menu_main::{Menu, COMPONENT_BASE_MENU};
pub use menu_main::Menu;
mod item;
pub use item::{Item, ItemType, COMPONENT_BASE_MENU_ITEM};
pub use item::{Item, ItemType};
mod submenu;
pub use submenu::{Submenu, COMPONENT_BASE_MENU_SUBMENU};
pub use submenu::Submenu;
mod megamenu;
pub use megamenu::{Megamenu, COMPONENT_BASE_MENU_MEGAMENU};
pub use megamenu::Megamenu;
mod group;
pub use group::{Group, COMPONENT_BASE_MENU_GROUP};
pub use group::Group;
mod element;
pub use element::{Element, ElementType, COMPONENT_BASE_MENU_ELEMENT};
pub use element::{Element, ElementType};

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::Submenu;
@ -16,15 +17,13 @@ pub enum ElementType {
// Element.
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Element {
weight : Weight,
renderable : Renderable,
element_type: ElementType,
}
impl_handle!(COMPONENT_BASE_MENU_ELEMENT for Element);
impl ComponentTrait for Element {
fn new() -> Self {
Element::default()

View file

@ -1,9 +1,10 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::Element;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Group {
id : OptionId,
weight : Weight,
@ -11,8 +12,6 @@ pub struct Group {
elements : TypedComponents<Element>,
}
impl_handle!(COMPONENT_BASE_MENU_GROUP for Group);
impl ComponentTrait for Group {
fn new() -> Self {
Group::default()

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::{Megamenu, Submenu};
@ -22,7 +23,7 @@ pub enum ItemType {
// Item.
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Item {
weight : Weight,
renderable : Renderable,
@ -32,8 +33,6 @@ pub struct Item {
right_icon : OptionComponent<Icon>,
}
impl_handle!(COMPONENT_BASE_MENU_ITEM for Item);
impl ComponentTrait for Item {
fn new() -> Self {
Item::default()

View file

@ -1,9 +1,10 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::Group;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Megamenu {
id : OptionId,
weight : Weight,
@ -11,8 +12,6 @@ pub struct Megamenu {
groups : TypedComponents<Group>,
}
impl_handle!(COMPONENT_BASE_MENU_MEGAMENU for Megamenu);
impl ComponentTrait for Megamenu {
fn new() -> Self {
Megamenu::default()

View file

@ -1,9 +1,10 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::Item;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Menu {
id : OptionId,
weight : Weight,
@ -11,8 +12,6 @@ pub struct Menu {
items : TypedComponents<Item>,
}
impl_handle!(COMPONENT_BASE_MENU for Menu);
impl ComponentTrait for Menu {
fn new() -> Self {
Menu::default()

View file

@ -1,9 +1,10 @@
use crate::prelude::*;
use crate::CrateHandle;
use super::Item;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Submenu {
id : OptionId,
weight : Weight,
@ -12,8 +13,6 @@ pub struct Submenu {
items : TypedComponents<Item>,
}
impl_handle!(COMPONENT_BASE_MENU_SUBMENU for Submenu);
impl ComponentTrait for Submenu {
fn new() -> Self {
Submenu::default()

View file

@ -1,7 +1,8 @@
use crate::prelude::*;
use crate::CrateHandle;
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Paragraph {
id : OptionId,
weight : Weight,
@ -11,8 +12,6 @@ pub struct Paragraph {
stuff : AnyComponents,
}
impl_handle!(COMPONENT_BASE_PARAGRAPH for Paragraph);
impl ComponentTrait for Paragraph {
fn new() -> Self {
Paragraph::default()
@ -46,17 +45,6 @@ impl ComponentTrait for Paragraph {
}
}
impl ComponentClasses for Paragraph {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Paragraph {
pub fn with(component: impl ComponentTrait) -> Self {
Paragraph::default().add_component(component)

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(Default, Eq, PartialEq)]
pub enum PoweredByLogo {
@ -11,7 +12,7 @@ pub enum PoweredByLogo {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct PoweredBy {
weight : Weight,
renderable: Renderable,
@ -19,8 +20,6 @@ pub struct PoweredBy {
logo : PoweredByLogo,
}
impl_handle!(COMPONENT_BASE_POWEREDBY for PoweredBy);
impl ComponentTrait for PoweredBy {
fn new() -> Self {
let year = Utc::now().format("%Y").to_string();

View file

@ -1,10 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
#[derive(CrateHandle, SmartDefault)]
pub struct Translate(L10n);
impl_handle!(COMPONENT_BASE_TRANSLATE for Translate);
impl ComponentTrait for Translate {
fn new() -> Self {
Translate::default()

View file

@ -1,4 +1,5 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(SmartDefault)]
pub enum WrapperType {
@ -11,7 +12,7 @@ pub enum WrapperType {
}
#[rustfmt::skip]
#[derive(SmartDefault)]
#[derive(ComponentClasses, CrateHandle, SmartDefault)]
pub struct Wrapper {
id : OptionId,
weight : Weight,
@ -23,8 +24,6 @@ pub struct Wrapper {
template : String,
}
impl_handle!(COMPONENT_BASE_WRAPPER for Wrapper);
impl ComponentTrait for Wrapper {
fn new() -> Self {
Wrapper::default()
@ -83,17 +82,6 @@ impl ComponentTrait for Wrapper {
}
}
impl ComponentClasses for Wrapper {
fn alter_classes(&mut self, op: ClassesOp, classes: impl Into<String>) -> &mut Self {
self.classes.alter_value(op, classes);
self
}
fn classes(&self) -> &OptionClasses {
&self.classes
}
}
impl Wrapper {
pub fn header() -> Self {
let mut c = Wrapper::default()

View file

@ -1,8 +1,8 @@
mod basic;
pub use basic::{Basic, THEME_BASIC};
pub use basic::Basic;
mod chassis;
pub use chassis::{Chassis, THEME_CHASSIS};
pub use chassis::Chassis;
mod inception;
pub use inception::{Inception, THEME_INCEPTION};
pub use inception::Inception;

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(CrateHandle)]
pub struct Basic;
impl_handle!(THEME_BASIC for Basic);
impl ModuleTrait for Basic {
fn name(&self) -> L10n {
L10n::n("Basic")

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(CrateHandle)]
pub struct Chassis;
impl_handle!(THEME_CHASSIS for Chassis);
impl ModuleTrait for Chassis {
fn name(&self) -> L10n {
L10n::n("Chassis")

View file

@ -1,9 +1,9 @@
use crate::prelude::*;
use crate::CrateHandle;
#[derive(CrateHandle)]
pub struct Inception;
impl_handle!(THEME_INCEPTION for Inception);
impl ModuleTrait for Inception {
fn name(&self) -> L10n {
L10n::n("Inception")

View file

@ -1,4 +1,4 @@
use crate::{Handle, HasHandle, Weight};
use crate::{Handle, ImplementHandle, Weight};
use std::any::Any;
@ -6,7 +6,7 @@ pub trait ActionBase: Any {
fn as_ref_any(&self) -> &dyn Any;
}
pub trait ActionTrait: ActionBase + HasHandle + Send + Sync {
pub trait ActionTrait: ActionBase + ImplementHandle + Send + Sync {
fn referer_handle(&self) -> Option<Handle> {
None
}

View file

@ -1,7 +1,7 @@
use crate::base::action;
use crate::core::component::Context;
use crate::html::{html, Markup, PrepareMarkup};
use crate::{util, HasHandle, Weight};
use crate::{util, ImplementHandle, Weight};
use std::any::Any;
@ -13,7 +13,7 @@ pub trait ComponentBase: Any {
fn as_mut_any(&mut self) -> &mut dyn Any;
}
pub trait ComponentTrait: ComponentBase + HasHandle + Send + Sync {
pub trait ComponentTrait: ComponentBase + ImplementHandle + Send + Sync {
fn new() -> Self
where
Self: Sized;

View file

@ -1,7 +1,7 @@
use crate::core::action::Action;
use crate::core::theme::ThemeRef;
use crate::locale::L10n;
use crate::{actions, service, util, HasHandle};
use crate::{actions, service, util, ImplementHandle};
#[cfg(feature = "database")]
use crate::{db::MigrationItem, migrations};
@ -13,7 +13,7 @@ pub trait ModuleBase {
}
/// Los módulos deben implementar este *trait*.
pub trait ModuleTrait: HasHandle + ModuleBase + Send + Sync {
pub trait ModuleTrait: ImplementHandle + ModuleBase + Send + Sync {
fn name(&self) -> L10n {
L10n::n(self.single_name())
}

View file

@ -44,15 +44,10 @@
//! ```rust
//! use pagetop::prelude::*;
//!
//! #[derive(AssignHandle)]
//! struct HelloWorld;
//!
//! impl_handle!(APP_HELLO_WORLD for HelloWorld);
//!
//! impl ModuleTrait for HelloWorld {
//! fn handle(&self) -> Handle {
//! APP_HELLO_WORLD
//! }
//!
//! fn configure_service(&self, scfg: &mut service::web::ServiceConfig) {
//! scfg.route("/", service::web::get().to(hello_world));
//! }
@ -113,7 +108,9 @@ pub use paste::paste;
/// customized default values.
pub use smart_default::SmartDefault;
pub use pagetop_macros::{fn_builder, main, test};
pub use pagetop_macros::{fn_builder, main, test, AssignHandle, ComponentClasses};
pub(crate) use pagetop_macros::CrateHandle;
// *************************************************************************************************
// GLOBAL.
@ -124,11 +121,15 @@ pub use static_files::Resource as StaticResource;
pub type Handle = u64;
pub trait HasHandle {
pub trait ImplementHandle {
fn static_handle() -> Handle
where
Self: Sized;
fn matches_handle(is: Handle) -> bool
where
Self: Sized;
fn handle(&self) -> Handle;
}

View file

@ -4,11 +4,11 @@
pub use crate::{concat_string, fn_builder, main, paste, test, SmartDefault};
// Global.
pub use crate::{Handle, HasHandle, HashMapResources, LazyStatic, Weight};
pub use crate::{Handle, HashMapResources, ImplementHandle, LazyStatic, Weight};
// Functions and macro helpers.
pub use crate::util;
pub use crate::{impl_handle, kv};
pub use crate::{kv, AssignHandle, ComponentClasses};
// MACROS.

View file

@ -1,7 +1,7 @@
mod error403;
pub use error403::ERROR_403;
pub use error403::Error403;
mod error404;
pub use error404::ERROR_404;
pub use error404::Error404;
use crate::locale::L10n;
use crate::response::{page::Page, ResponseError};

View file

@ -1,11 +1,10 @@
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, PrepareMarkup};
use crate::impl_handle;
use crate::CrateHandle;
#[derive(CrateHandle)]
pub struct Error403;
impl_handle!(ERROR_403 for Error403);
impl ComponentTrait for Error403 {
fn new() -> Self {
Self

View file

@ -1,11 +1,10 @@
use crate::core::component::{ComponentTrait, Context};
use crate::html::{html, PrepareMarkup};
use crate::impl_handle;
use crate::CrateHandle;
#[derive(CrateHandle)]
pub struct Error404;
impl_handle!(ERROR_404 for Error404);
impl ComponentTrait for Error404 {
fn new() -> Self {
Self

View file

@ -92,44 +92,6 @@ pub fn absolute_dir(
// MACRO HELPERS.
// *************************************************************************************************
#[macro_export]
macro_rules! impl_handle {
( $HANDLE:ident for $Element:ident ) => {
/// Constant handle to represent a unique PageTop building element.
pub const $HANDLE: $crate::Handle =
$crate::util::handle(module_path!(), file!(), line!(), column!());
impl $crate::HasHandle for $Element {
#[inline]
fn static_handle() -> $crate::Handle {
$HANDLE
}
#[inline]
fn handle(&self) -> $crate::Handle {
$HANDLE
}
}
};
( $HANDLE:ident for $Element:ident<$Implement:ident> ) => {
/// Constant handle to represent a unique PageTop building element.
pub const $HANDLE: $crate::Handle =
$crate::util::handle(module_path!(), file!(), line!(), column!());
impl<I: $Implement> $crate::HasHandle for $Element<I> {
#[inline]
fn static_handle() -> $crate::Handle {
$HANDLE
}
#[inline]
fn handle(&self) -> $crate::Handle {
$HANDLE
}
}
};
}
#[macro_export]
/// Macro para construir grupos de pares clave-valor.
///

View file

@ -1,9 +1,8 @@
use pagetop::prelude::*;
#[derive(AssignHandle)]
struct HealthCheck;
impl_handle!(MODULE_TEST_SERVER_HEALTH_CHECK for HealthCheck);
impl ModuleTrait for HealthCheck {}
#[pagetop::test]