♻️ Use new() with arguments, default() without

This commit is contained in:
Manuel Cillero 2023-11-13 16:52:08 +01:00
parent 258d42049f
commit f208da1b7b
13 changed files with 35 additions and 66 deletions

View file

@ -26,7 +26,7 @@ impl<C: ComponentTrait> ActionTrait for AfterPrepareComponent<C> {
} }
impl<C: ComponentTrait> AfterPrepareComponent<C> { impl<C: ComponentTrait> AfterPrepareComponent<C> {
pub fn with(f: FnAction<C>) -> Self { pub fn new(f: FnAction<C>) -> Self {
AfterPrepareComponent { AfterPrepareComponent {
f, f,
referer_handle: Some(C::static_handle()), referer_handle: Some(C::static_handle()),

View file

@ -26,7 +26,7 @@ impl<C: ComponentTrait> ActionTrait for BeforePrepareComponent<C> {
} }
impl<C: ComponentTrait> BeforePrepareComponent<C> { impl<C: ComponentTrait> BeforePrepareComponent<C> {
pub fn with(f: FnAction<C>) -> Self { pub fn new(f: FnAction<C>) -> Self {
BeforePrepareComponent { BeforePrepareComponent {
f, f,
referer_handle: Some(C::static_handle()), referer_handle: Some(C::static_handle()),

View file

@ -16,7 +16,7 @@ impl ActionTrait for AfterPrepareBody {
} }
impl AfterPrepareBody { impl AfterPrepareBody {
pub fn with(f: FnActionPage) -> Self { pub fn new(f: FnActionPage) -> Self {
AfterPrepareBody { f, weight: 0 } AfterPrepareBody { f, weight: 0 }
} }

View file

@ -16,7 +16,7 @@ impl ActionTrait for BeforePrepareBody {
} }
impl BeforePrepareBody { impl BeforePrepareBody {
pub fn with(f: FnActionPage) -> Self { pub fn new(f: FnActionPage) -> Self {
BeforePrepareBody { f, weight: 0 } BeforePrepareBody { f, weight: 0 }
} }

View file

@ -20,7 +20,7 @@ pub fn add_action(action: Action) {
if let Some(list) = actions.get_mut(&key_action) { if let Some(list) = actions.get_mut(&key_action) {
list.add(action); list.add(action);
} else { } else {
actions.insert(key_action, ActionsList::with(action)); actions.insert(key_action, ActionsList::new(action));
} }
} }

View file

@ -4,15 +4,12 @@ use std::sync::{Arc, RwLock};
pub type Action = Box<dyn ActionTrait>; pub type Action = Box<dyn ActionTrait>;
#[derive(Default)]
pub struct ActionsList(Arc<RwLock<Vec<Action>>>); pub struct ActionsList(Arc<RwLock<Vec<Action>>>);
impl ActionsList { impl ActionsList {
pub fn new() -> Self { pub fn new(action: Action) -> Self {
ActionsList(Arc::new(RwLock::new(Vec::new()))) let mut list = ActionsList::default();
}
pub fn with(action: Action) -> Self {
let mut list = ActionsList::new();
list.add(action); list.add(action);
list list
} }

View file

@ -25,7 +25,7 @@ pub trait ThemeTrait: ModuleTrait + Send + Sync {
if render_region.is_empty() { if render_region.is_empty() {
html! {} html! {}
} else { } else {
let id = OptionId::with(region).get().unwrap(); let id = OptionId::new(region).get().unwrap();
let id_inner = concat_string!(id, "__inner"); let id_inner = concat_string!(id, "__inner");
html! { html! {
div id=(id) class="pt-region" { div id=(id) class="pt-region" {

View file

@ -24,14 +24,8 @@ pub enum ClassesOp {
pub struct OptionClasses(Vec<String>); pub struct OptionClasses(Vec<String>);
impl OptionClasses { impl OptionClasses {
pub fn new() -> Self { pub fn new(classes: impl Into<String>) -> Self {
OptionClasses::default() OptionClasses::default().with_value(ClassesOp::AddFirst, classes)
}
pub fn with(op: ClassesOp, classes: impl Into<String>) -> Self {
let mut opt = OptionClasses::default();
opt.alter_value(op, classes);
opt
} }
// OptionClasses BUILDER. // OptionClasses BUILDER.
@ -96,11 +90,6 @@ impl OptionClasses {
// OptionClasses GETTERS. // OptionClasses GETTERS.
pub fn exists(&self, class: impl Into<String>) -> bool {
let class: String = class.into();
self.0.iter().any(|c| c.eq(&class))
}
pub fn get(&self) -> Option<String> { pub fn get(&self) -> Option<String> {
if self.0.is_empty() { if self.0.is_empty() {
None None
@ -108,4 +97,9 @@ impl OptionClasses {
Some(self.0.join(" ")) Some(self.0.join(" "))
} }
} }
pub fn contains(&self, class: impl Into<String>) -> bool {
let class: String = class.into();
self.0.iter().any(|c| c.eq(&class))
}
} }

View file

@ -4,14 +4,8 @@ use crate::fn_builder;
pub struct OptionId(Option<String>); pub struct OptionId(Option<String>);
impl OptionId { impl OptionId {
pub fn new() -> Self { pub fn new(value: impl Into<String>) -> Self {
OptionId::default() OptionId::default().with_value(value)
}
pub fn with(value: impl Into<String>) -> Self {
let mut opt = OptionId::default();
opt.alter_value(value);
opt
} }
// OptionId BUILDER. // OptionId BUILDER.

View file

@ -4,14 +4,8 @@ use crate::fn_builder;
pub struct OptionName(Option<String>); pub struct OptionName(Option<String>);
impl OptionName { impl OptionName {
pub fn new() -> Self { pub fn new(value: impl Into<String>) -> Self {
OptionName::default() OptionName::default().with_value(value)
}
pub fn with(value: impl Into<String>) -> Self {
let mut opt = OptionName::default();
opt.alter_value(value);
opt
} }
// OptionName BUILDER. // OptionName BUILDER.

View file

@ -4,14 +4,8 @@ use crate::fn_builder;
pub struct OptionString(Option<String>); pub struct OptionString(Option<String>);
impl OptionString { impl OptionString {
pub fn new() -> Self { pub fn new(value: impl Into<String>) -> Self {
OptionString::default() OptionString::default().with_value(value)
}
pub fn with(value: impl Into<String>) -> Self {
let mut opt = OptionString::default();
opt.alter_value(value);
opt
} }
// OptionString BUILDER. // OptionString BUILDER.

View file

@ -1,17 +1,13 @@
use crate::fn_builder; use crate::fn_builder;
use crate::html::{html, Markup}; use crate::html::Markup;
use crate::locale::{L10n, LanguageIdentifier}; use crate::locale::{L10n, LanguageIdentifier};
#[derive(Default)] #[derive(Default)]
pub struct OptionTranslated(Option<L10n>); pub struct OptionTranslated(Option<L10n>);
impl OptionTranslated { impl OptionTranslated {
pub fn new() -> Self { pub fn new(value: L10n) -> Self {
OptionTranslated::default() OptionTranslated::default().with_value(value)
}
pub fn with(value: L10n) -> Self {
OptionTranslated(Some(value))
} }
// OptionTranslated BUILDER. // OptionTranslated BUILDER.
@ -31,10 +27,10 @@ impl OptionTranslated {
None None
} }
pub fn escaped(&self, langid: &LanguageIdentifier) -> Markup { pub fn escaped(&self, langid: &LanguageIdentifier) -> Option<Markup> {
if let Some(value) = &self.0 { if let Some(value) = &self.0 {
return value.escaped(langid); return Some(value.escaped(langid));
} }
html! {} None
} }
} }

View file

@ -30,15 +30,15 @@ impl Page {
#[rustfmt::skip] #[rustfmt::skip]
pub fn new(request: service::HttpRequest) -> Self { pub fn new(request: service::HttpRequest) -> Self {
Page { Page {
title : OptionTranslated::new(), title : OptionTranslated::default(),
description : OptionTranslated::new(), description : OptionTranslated::default(),
metadata : Vec::new(), metadata : Vec::default(),
properties : Vec::new(), properties : Vec::default(),
favicon : None, favicon : None,
context : Context::new(request), context : Context::new(request),
body_classes: OptionClasses::new(), body_classes: OptionClasses::default(),
skip_to : OptionId::new(), skip_to : OptionId::default(),
regions : ComponentsRegions::new(), regions : ComponentsRegions::default(),
template : "default".to_owned(), template : "default".to_owned(),
} }
} }