Expand optional component props

This commit is contained in:
Manuel Cillero 2023-10-21 20:06:32 +02:00
parent 7c176ddafe
commit f5cfc4442e
5 changed files with 64 additions and 18 deletions

View file

@ -22,6 +22,9 @@ pub use opt_name::OptionName;
mod opt_string; mod opt_string;
pub use opt_string::OptionString; pub use opt_string::OptionString;
mod opt_translate;
pub use opt_translate::OptionTranslate;
mod opt_classes; mod opt_classes;
pub use opt_classes::{ClassesOp, OptionClasses}; pub use opt_classes::{ClassesOp, OptionClasses};

View file

@ -1,7 +1,7 @@
use crate::fn_builder; use crate::fn_builder;
#[derive(Default)] #[derive(Default)]
pub struct OptionId(String); pub struct OptionId(Option<String>);
impl OptionId { impl OptionId {
pub fn new() -> Self { pub fn new() -> Self {
@ -18,17 +18,18 @@ impl OptionId {
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self { pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
self.0 = value.into().trim().replace(' ', "_"); self.0 = Some(value.into().trim().replace(' ', "_"));
self self
} }
// OptionId GETTERS. // OptionId GETTERS.
pub fn get(&self) -> Option<String> { pub fn get(&self) -> Option<String> {
if self.0.is_empty() { if let Some(value) = &self.0 {
None if !value.is_empty() {
} else { return Some(value.to_owned());
Some(self.0.to_owned()) }
} }
None
} }
} }

View file

@ -1,7 +1,7 @@
use crate::fn_builder; use crate::fn_builder;
#[derive(Default)] #[derive(Default)]
pub struct OptionName(String); pub struct OptionName(Option<String>);
impl OptionName { impl OptionName {
pub fn new() -> Self { pub fn new() -> Self {
@ -18,17 +18,18 @@ impl OptionName {
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self { pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
self.0 = value.into().trim().replace(' ', "_"); self.0 = Some(value.into().trim().replace(' ', "_"));
self self
} }
// OptionName GETTERS. // OptionName GETTERS.
pub fn get(&self) -> Option<String> { pub fn get(&self) -> Option<String> {
if self.0.is_empty() { if let Some(value) = &self.0 {
None if !value.is_empty() {
} else { return Some(value.to_owned());
Some(self.0.to_owned()) }
} }
None
} }
} }

View file

@ -1,7 +1,7 @@
use crate::fn_builder; use crate::fn_builder;
#[derive(Default)] #[derive(Default)]
pub struct OptionString(String); pub struct OptionString(Option<String>);
impl OptionString { impl OptionString {
pub fn new() -> Self { pub fn new() -> Self {
@ -18,17 +18,18 @@ impl OptionString {
#[fn_builder] #[fn_builder]
pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self { pub fn alter_value(&mut self, value: impl Into<String>) -> &mut Self {
self.0 = value.into().trim().to_owned(); self.0 = Some(value.into().trim().to_owned());
self self
} }
// OptionString GETTERS. // OptionString GETTERS.
pub fn get(&self) -> Option<String> { pub fn get(&self) -> Option<String> {
if self.0.is_empty() { if let Some(value) = &self.0 {
None if !value.is_empty() {
} else { return Some(value.to_owned());
Some(self.0.to_owned()) }
} }
None
} }
} }

View file

@ -0,0 +1,40 @@
use crate::fn_builder;
use crate::html::{html, Markup};
use crate::locale::{L10n, LanguageIdentifier};
#[derive(Default)]
pub struct OptionTranslate(Option<L10n>);
impl OptionTranslate {
pub fn new() -> Self {
OptionTranslate::default()
}
pub fn with(value: L10n) -> Self {
OptionTranslate(Some(value))
}
// OptionTranslate BUILDER.
#[fn_builder]
pub fn alter_value(&mut self, value: L10n) -> &mut Self {
self.0 = Some(value);
self
}
// OptionTranslate GETTERS.
pub fn using(&self, langid: &LanguageIdentifier) -> Option<String> {
if let Some(value) = &self.0 {
return value.using(langid);
}
None
}
pub fn escaped(&self, langid: &LanguageIdentifier) -> Markup {
if let Some(value) = &self.0 {
return value.escaped(langid);
}
html! {}
}
}