From 52056c2ed88f7545ae26e62e73fb1e3f9b3312f6 Mon Sep 17 00:00:00 2001 From: Nico Burniske <46934294+nicoburniske@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:34:01 -0400 Subject: [PATCH] Allow Option in tw_join! and tw_merge! macros (#19) * remove AsRef impl in favor of granular impls for AsTailwindClass --- fuse/src/core/mod.rs | 70 ++++++++++++++++++++++++++++++++++++++++++-- fuse/src/lib.rs | 2 +- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/fuse/src/core/mod.rs b/fuse/src/core/mod.rs index 12df278..d810dd5 100644 --- a/fuse/src/core/mod.rs +++ b/fuse/src/core/mod.rs @@ -11,11 +11,77 @@ pub trait AsTailwindClass { fn as_class(&self) -> &str; } -impl AsTailwindClass for T +impl AsTailwindClass for String { + fn as_class(&self) -> &str { + self.as_str() + } +} + +impl AsTailwindClass for &str { + fn as_class(&self) -> &str { + self + } +} + +impl AsTailwindClass for &T +where + T: AsTailwindClass, +{ + fn as_class(&self) -> &str { + (*self).as_class() + } +} + +impl AsTailwindClass for &mut T +where + T: AsTailwindClass, +{ + fn as_class(&self) -> &str { + (**self).as_class() + } +} + +impl AsTailwindClass for std::rc::Rc +where + T: AsTailwindClass, +{ + fn as_class(&self) -> &str { + self.as_ref().as_class() + } +} + +impl AsTailwindClass for std::sync::Arc where - T: AsRef, + T: AsTailwindClass, { + fn as_class(&self) -> &str { + self.as_ref().as_class() + } +} + +impl AsTailwindClass for std::borrow::Cow<'_, str> { fn as_class(&self) -> &str { self.as_ref() } } + +impl AsTailwindClass for Box +where + T: AsTailwindClass, +{ + fn as_class(&self) -> &str { + (**self).as_class() + } +} + +impl AsTailwindClass for Option +where + T: AsTailwindClass, +{ + fn as_class(&self) -> &str { + match self { + Some(t) => t.as_class(), + None => "", + } + } +} diff --git a/fuse/src/lib.rs b/fuse/src/lib.rs index 563d980..b883b0b 100644 --- a/fuse/src/lib.rs +++ b/fuse/src/lib.rs @@ -236,7 +236,7 @@ mod variant { impl TailwindFuse for TailwindMerge { fn fuse_classes(&self, class: &[&str]) -> String { - crate::core::merge::tw_merge_slice(class) + crate::merge::tw_merge_slice(class) } }