Skip to content

Commit

Permalink
feat: add .into_X_boxed() for classes, properties, and styles as fo…
Browse files Browse the repository at this point in the history
…r attributes
  • Loading branch information
jquesada2016 authored Sep 4, 2023
1 parent 006ca13 commit 716b9fb
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions leptos_dom/src/macro_helpers/into_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl std::fmt::Debug for Attribute {
pub trait IntoAttribute {
/// Converts the object into an [`Attribute`].
fn into_attribute(self) -> Attribute;

/// Helper function for dealing with `Box<dyn IntoAttribute>`.
fn into_attribute_boxed(self: Box<Self>) -> Attribute;
}
Expand Down
19 changes: 19 additions & 0 deletions leptos_dom/src/macro_helpers/into_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@ pub enum Class {
pub trait IntoClass {
/// Converts the object into a [`Class`].
fn into_class(self) -> Class;

/// Helper function for dealing with `Box<dyn IntoClass>`.
fn into_class_boxed(self: Box<Self>) -> Class;
}

impl IntoClass for bool {
#[inline(always)]
fn into_class(self) -> Class {
Class::Value(self)
}

fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}

impl<T> IntoClass for T
Expand All @@ -39,6 +46,10 @@ where
let modified_fn = Box::new(self);
Class::Fn(modified_fn)
}

fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}

impl Class {
Expand Down Expand Up @@ -128,6 +139,10 @@ macro_rules! class_signal_type {
let modified_fn = Box::new(move || self.get());
Class::Fn(modified_fn)
}

fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}
};
}
Expand All @@ -141,6 +156,10 @@ macro_rules! class_signal_type_optional {
let modified_fn = Box::new(move || self.get().unwrap_or(false));
Class::Fn(modified_fn)
}

fn into_class_boxed(self: Box<Self>) -> Class {
(*self).into_class()
}
}
};
}
Expand Down
25 changes: 24 additions & 1 deletion leptos_dom/src/macro_helpers/into_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use wasm_bindgen::UnwrapThrowExt;
pub enum Property {
/// A static JavaScript value.
Value(JsValue),
/// A (presumably reactive) function, which will be run inside an effect to toggle the class.
/// A (presumably reactive) function, which will be run inside an effect to update the property.
Fn(Box<dyn Fn() -> JsValue>),
}

Expand All @@ -25,6 +25,9 @@ pub enum Property {
pub trait IntoProperty {
/// Converts the object into a [`Property`].
fn into_property(self) -> Property;

/// Helper function for dealing with `Box<dyn IntoProperty>`.
fn into_property_boxed(self: Box<Self>) -> Property;
}

impl<T, U> IntoProperty for T
Expand All @@ -36,6 +39,10 @@ where
let modified_fn = Box::new(move || self().into());
Property::Fn(modified_fn)
}

fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}

macro_rules! prop_type {
Expand All @@ -45,13 +52,21 @@ macro_rules! prop_type {
fn into_property(self) -> Property {
Property::Value(self.into())
}

fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}

impl IntoProperty for Option<$prop_type> {
#[inline(always)]
fn into_property(self) -> Property {
Property::Value(self.into())
}

fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
};
}
Expand All @@ -67,6 +82,10 @@ macro_rules! prop_signal_type {
let modified_fn = Box::new(move || self.get().into());
Property::Fn(modified_fn)
}

fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
};
}
Expand All @@ -83,6 +102,10 @@ macro_rules! prop_signal_type_optional {
let modified_fn = Box::new(move || self.get().into());
Property::Fn(modified_fn)
}

fn into_property_boxed(self: Box<Self>) -> Property {
(*self).into_property()
}
}
};
}
Expand Down
63 changes: 63 additions & 0 deletions leptos_dom/src/macro_helpers/into_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,76 +41,119 @@ impl std::fmt::Debug for Style {
pub trait IntoStyle {
/// Converts the object into a [`Style`].
fn into_style(self) -> Style;

/// Helper function for dealing with `Box<dyn IntoStyle>`.
fn into_style_boxed(self: Box<Self>) -> Style;
}

impl IntoStyle for &'static str {
#[inline(always)]
fn into_style(self) -> Style {
Style::Value(Oco::Borrowed(self))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for String {
#[inline(always)]
fn into_style(self) -> Style {
Style::Value(Oco::Owned(self))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Rc<str> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Value(Oco::Counted(self))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(self).into_style()
}
}

impl IntoStyle for Cow<'static, str> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Value(self.into())
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Oco<'static, str> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Value(self)
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Option<&'static str> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Option(self.map(Oco::Borrowed))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Option<String> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Option(self.map(Oco::Owned))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(self).into_style()
}
}

impl IntoStyle for Option<Rc<str>> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Option(self.map(Oco::Counted))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Option<Cow<'static, str>> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Option(self.map(Oco::from))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Option<Oco<'static, str>> {
#[inline(always)]
fn into_style(self) -> Style {
Style::Option(self)
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl<T, U> IntoStyle for T
Expand All @@ -123,6 +166,10 @@ where
let modified_fn = Rc::new(move || (self)().into_style());
Style::Fn(modified_fn)
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl Style {
Expand Down Expand Up @@ -221,12 +268,20 @@ macro_rules! style_type {
fn into_style(self) -> Style {
Style::Value(self.to_string().into())
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}

impl IntoStyle for Option<$style_type> {
fn into_style(self) -> Style {
Style::Option(self.map(|n| n.to_string().into()))
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
};
}
Expand All @@ -242,6 +297,10 @@ macro_rules! style_signal_type {
let modified_fn = Rc::new(move || self.get().into_style());
Style::Fn(modified_fn)
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
};
}
Expand All @@ -258,6 +317,10 @@ macro_rules! style_signal_type_optional {
let modified_fn = Rc::new(move || self.get().into_style());
Style::Fn(modified_fn)
}

fn into_style_boxed(self: Box<Self>) -> Style {
(*self).into_style()
}
}
};
}
Expand Down

0 comments on commit 716b9fb

Please sign in to comment.