🧑‍💻 Swap Default for more versatile SmartDefault

This commit is contained in:
Manuel Cillero 2023-11-18 07:31:45 +01:00
parent e25cc16859
commit 5daa84283d
45 changed files with 93 additions and 106 deletions

View file

@ -40,6 +40,7 @@ itoa = "1.0.9"
nom = "7.1.3"
once_cell = "1.18.0"
paste = "1.0.14"
smart-default = "0.7.1"
substring = "1.4.5"
term_size = "0.3.2"
toml = "0.8.8"

View file

@ -1,6 +1,6 @@
use crate::core::component::{Context, ContextOp};
use crate::html::{JavaScript, StyleSheet};
use crate::Weight;
use crate::{SmartDefault, Weight};
// Context parameters.
pub const PARAM_BASE_WEIGHT: &str = "base.weight";
@ -57,7 +57,7 @@ pub(crate) fn add_base_assets(cx: &mut Context) {
// *************************************************************************************************
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub enum BreakPoint {
#[default]
None, /* Does not apply. Rest initially assume 1 pixel = 0.0625em */
@ -88,7 +88,7 @@ impl ToString for BreakPoint {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum FontSize {
ExtraLarge,
XxLarge,

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Block {
id : OptionId,
weight : Weight,

View file

@ -1,31 +1,19 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(SmartDefault)]
pub struct Branding {
id : OptionId,
weight : Weight,
renderable: Renderable,
#[default(_code = "config::SETTINGS.app.name.to_owned()")]
app_name : String,
slogan : OptionTranslated,
logo : ArcTypedComponent<Image>,
logo : OptionComponent<Image>,
#[default(_code = "|_| \"/\"")]
frontpage : FnContextualPath,
}
#[rustfmt::skip]
impl Default for Branding {
fn default() -> Self {
Branding {
id : OptionId::default(),
weight : Weight::default(),
renderable: Renderable::default(),
app_name : config::SETTINGS.app.name.to_owned(),
slogan : OptionTranslated::default(),
logo : ArcTypedComponent::<Image>::default(),
frontpage : |_| "/",
}
}
}
impl_handle!(COMPONENT_BASE_BRANDING for Branding);
impl ComponentTrait for Branding {

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ButtonType {
#[default]
Link,
@ -17,7 +17,7 @@ impl ToString for ButtonType {
}
}
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ButtonTarget {
#[default]
Default,
@ -27,10 +27,8 @@ pub enum ButtonTarget {
Context(String),
}
type ButtonIcon = ArcTypedComponent<Icon>;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Button {
id : OptionId,
weight : Weight,

View file

@ -7,7 +7,7 @@ use crate::prelude::*;
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum Direction {
#[default]
Default,
@ -42,7 +42,7 @@ impl ToString for Direction {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum WrapAlign {
#[default]
Default,
@ -65,7 +65,7 @@ impl ToString for WrapAlign {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ContentAlign {
#[default]
Default,
@ -94,7 +94,7 @@ impl ToString for ContentAlign {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ContentJustify {
#[default]
Default,
@ -123,7 +123,7 @@ impl ToString for ContentJustify {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ItemAlign {
#[default]
Default,
@ -150,7 +150,7 @@ impl ToString for ItemAlign {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum Gap {
#[default]
Default,
@ -175,7 +175,7 @@ impl ToString for Gap {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ItemGrow {
#[default]
Default,
@ -210,7 +210,7 @@ impl ToString for ItemGrow {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ItemShrink {
#[default]
Default,
@ -245,7 +245,7 @@ impl ToString for ItemShrink {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ItemSize {
#[default]
Default,
@ -284,7 +284,7 @@ impl ToString for ItemSize {
// *************************************************************************************************
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ItemOffset {
#[default]
Default,

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Container {
id : OptionId,
weight : Weight,

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Item {
id : OptionId,
weight : Weight,

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ButtonType {
#[default]
Button,
@ -20,7 +20,7 @@ impl ToString for ButtonType {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Button {
weight : Weight,
renderable : Renderable,

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Date {
weight : Weight,
renderable : Renderable,

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum FormMethod {
#[default]
Post,
@ -8,7 +8,7 @@ pub enum FormMethod {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Form {
id : OptionId,
weight : Weight,

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Hidden {
weight: Weight,
name : OptionName,

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum InputType {
#[default]
Textfield,
@ -12,7 +12,7 @@ pub enum InputType {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Input {
weight : Weight,
renderable : Renderable,

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum HeadingType {
#[default]
H1,
@ -11,7 +11,7 @@ pub enum HeadingType {
H6,
}
#[derive(Default)]
#[derive(SmartDefault)]
pub enum HeadingDisplay {
ExtraLarge,
XxLarge,
@ -39,7 +39,7 @@ impl ToString for HeadingDisplay {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Heading {
id : OptionId,
weight : Weight,

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Html(Markup);
impl_handle!(COMPONENT_BASE_HTML for Html);

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Icon {
weight : Weight,
renderable: Renderable,

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
const IMG_FLUID: &str = "pt-img__fluid";
const IMG_FIXED: &str = "pt-img__fixed";
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ImageSize {
#[default]
Auto,
@ -14,7 +14,7 @@ pub enum ImageSize {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Image {
id : OptionId,
weight : Weight,

View file

@ -5,7 +5,7 @@ use super::Submenu;
type Content = ArcTypedComponent<Html>;
type SubmenuItems = ArcTypedComponent<Submenu>;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ElementType {
#[default]
Void,
@ -16,7 +16,7 @@ pub enum ElementType {
// Element.
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Element {
weight : Weight,
renderable : Renderable,

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::Element;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Group {
id : OptionId,
weight : Weight,

View file

@ -7,7 +7,7 @@ type Content = ArcTypedComponent<Html>;
type SubmenuItems = ArcTypedComponent<Submenu>;
type MegamenuGroups = ArcTypedComponent<Megamenu>;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum ItemType {
#[default]
Void,
@ -22,7 +22,7 @@ pub enum ItemType {
// Item.
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Item {
weight : Weight,
renderable : Renderable,

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::Group;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Megamenu {
id : OptionId,
weight : Weight,

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::Item;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Menu {
id : OptionId,
weight : Weight,

View file

@ -3,7 +3,7 @@ use crate::prelude::*;
use super::Item;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Submenu {
id : OptionId,
weight : Weight,

View file

@ -1,7 +1,7 @@
use crate::prelude::*;
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Paragraph {
id : OptionId,
weight : Weight,

View file

@ -11,7 +11,7 @@ pub enum PoweredByLogo {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct PoweredBy {
weight : Weight,
renderable: Renderable,

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Translate(L10n);
impl_handle!(COMPONENT_BASE_TRANSLATE for Translate);

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
#[derive(Default)]
#[derive(SmartDefault)]
pub enum WrapperType {
#[default]
Container,
@ -11,7 +11,7 @@ pub enum WrapperType {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Wrapper {
id : OptionId,
weight : Weight,

View file

@ -1,10 +1,11 @@
use crate::core::action::ActionTrait;
use crate::SmartDefault;
use std::sync::{Arc, RwLock};
pub type Action = Box<dyn ActionTrait>;
#[derive(Default)]
#[derive(SmartDefault)]
pub struct ActionsList(Arc<RwLock<Vec<Action>>>);
impl ActionsList {

View file

@ -1,19 +1,14 @@
use crate::core::component::Context;
use crate::SmartDefault;
pub type FnIsRenderable = fn(cx: &Context) -> bool;
#[derive(SmartDefault)]
pub struct Renderable {
#[default(_code = "render_always")]
pub check: FnIsRenderable,
}
impl Default for Renderable {
fn default() -> Self {
Renderable {
check: render_always,
}
}
}
fn render_always(_cx: &Context) -> bool {
true
}

View file

@ -1,6 +1,6 @@
use crate::core::component::{AnyComponents, ArcAnyComponent, ArcAnyOp};
use crate::core::theme::ThemeRef;
use crate::{Handle, LazyStatic};
use crate::{Handle, LazyStatic, SmartDefault};
use std::collections::HashMap;
use std::sync::RwLock;
@ -11,7 +11,7 @@ static THEME_REGIONS: LazyStatic<RwLock<HashMap<Handle, ComponentsRegions>>> =
static COMMON_REGIONS: LazyStatic<RwLock<ComponentsRegions>> =
LazyStatic::new(|| RwLock::new(ComponentsRegions::default()));
#[derive(Default)]
#[derive(SmartDefault)]
pub struct ComponentsRegions(HashMap<&'static str, AnyComponents>);
impl ComponentsRegions {

View file

@ -4,7 +4,7 @@ pub mod javascript;
pub mod stylesheet;
use crate::html::{html, Markup};
use crate::Weight;
use crate::{SmartDefault, Weight};
pub trait AssetsTrait {
fn path(&self) -> &str;
@ -14,7 +14,7 @@ pub trait AssetsTrait {
fn prepare(&self) -> Markup;
}
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Assets<T>(Vec<T>);
impl<T: AssetsTrait> Assets<T> {

View file

@ -1,9 +1,9 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
use crate::{SmartDefault, Weight};
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct HeadScript {
path : String,
code : String,

View file

@ -1,9 +1,9 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
use crate::{SmartDefault, Weight};
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct HeadStyles {
path : String,
styles: String,

View file

@ -1,6 +1,6 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
use crate::{SmartDefault, Weight};
#[derive(Default, Eq, PartialEq)]
pub enum ModeJS {
@ -11,7 +11,7 @@ pub enum ModeJS {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct JavaScript {
path : String,
prefix : &'static str,

View file

@ -1,6 +1,6 @@
use crate::html::assets::AssetsTrait;
use crate::html::{html, Markup};
use crate::Weight;
use crate::{SmartDefault, Weight};
pub enum TargetMedia {
Default,
@ -10,7 +10,7 @@ pub enum TargetMedia {
}
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub struct StyleSheet {
path : String,
prefix : &'static str,

View file

@ -1,6 +1,7 @@
use crate::html::{html, Markup};
use crate::SmartDefault;
#[derive(Default)]
#[derive(SmartDefault)]
pub struct Favicon(Vec<Markup>);
impl Favicon {

View file

@ -9,7 +9,7 @@
//! **OptionClasses** assumes that the order of the classes is irrelevant
//! (<https://stackoverflow.com/a/1321712>), and duplicate classes will not be allowed.
use crate::fn_builder;
use crate::{fn_builder, SmartDefault};
pub enum ClassesOp {
Add,
@ -20,7 +20,7 @@ pub enum ClassesOp {
Clear,
}
#[derive(Default)]
#[derive(SmartDefault)]
pub struct OptionClasses(Vec<String>);
impl OptionClasses {

View file

@ -1,6 +1,6 @@
use crate::fn_builder;
use crate::{fn_builder, SmartDefault};
#[derive(Default)]
#[derive(SmartDefault)]
pub struct OptionId(Option<String>);
impl OptionId {

View file

@ -1,6 +1,6 @@
use crate::fn_builder;
use crate::{fn_builder, SmartDefault};
#[derive(Default)]
#[derive(SmartDefault)]
pub struct OptionName(Option<String>);
impl OptionName {

View file

@ -1,6 +1,6 @@
use crate::fn_builder;
use crate::{fn_builder, SmartDefault};
#[derive(Default)]
#[derive(SmartDefault)]
pub struct OptionString(Option<String>);
impl OptionString {

View file

@ -1,9 +1,8 @@
use crate::fn_builder;
use crate::html::Markup;
use crate::locale::{L10n, LanguageIdentifier};
use crate::{fn_builder, SmartDefault};
#[derive(Default)]
#[derive(SmartDefault)]
pub struct OptionTranslated(L10n);
impl OptionTranslated {

View file

@ -1,4 +1,4 @@
use crate::concat_string;
use crate::{concat_string, SmartDefault};
// About pixels: Pixels (px) are relative to the viewing device. For low-dpi devices, 1px is one
// device pixel (dot) of the display. For printers and high resolution screens 1px implies multiple
@ -10,7 +10,7 @@ use crate::concat_string;
// About viewport: If the browser window size is 50cm wide, 1vw = 0.5cm.
#[rustfmt::skip]
#[derive(Default)]
#[derive(SmartDefault)]
pub enum Value {
#[default]
None,

View file

@ -101,7 +101,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
// *************************************************************************************************
// RE-EXPORTED MACROS.
// RE-EXPORTED MACROS AND DERIVES.
// *************************************************************************************************
pub use concat_string::concat_string;
@ -109,6 +109,10 @@ pub use concat_string::concat_string;
/// Enables flexible identifier concatenation in macros, allowing new items with pasted identifiers.
pub use paste::paste;
/// Custom derive for automatically implementing the [Default](std::default::Default) trait with
/// customized default values.
pub use smart_default::SmartDefault;
pub use pagetop_macros::{fn_builder, main, test};
// *************************************************************************************************

View file

@ -88,7 +88,7 @@
//! ```
use crate::html::{Markup, PreEscaped};
use crate::{config, kv, LazyStatic, LOCALES_PAGETOP};
use crate::{config, kv, LazyStatic, SmartDefault, LOCALES_PAGETOP};
pub use fluent_templates;
pub use unic_langid::LanguageIdentifier;
@ -169,7 +169,7 @@ macro_rules! static_locales {
};
}
#[derive(Default)]
#[derive(SmartDefault)]
enum L10nOp {
#[default]
None,
@ -177,7 +177,7 @@ enum L10nOp {
Translate(String),
}
#[derive(Default)]
#[derive(SmartDefault)]
pub struct L10n {
op: L10nOp,
locales: Option<&'static Locales>,

View file

@ -1,7 +1,7 @@
//! The PageTop Prelude.
// Re-exported macros.
pub use crate::{concat_string, fn_builder, main, paste, test};
// Re-exported macros and derives.
pub use crate::{concat_string, fn_builder, main, paste, test, SmartDefault};
// Global.
pub use crate::{Handle, HasHandle, HashMapResources, LazyStatic, Weight};