leptos_css/
custom_property.rs1use std::{borrow::Cow, fmt, marker::PhantomData};
2
3use crate::{
4 CheckedCssValue, CheckedDeclaration, checked_value,
5 identifier::is_checked_custom_property_name, serialization::CssWriteTo,
6};
7
8#[derive(Clone, Debug, PartialEq, Eq)]
15pub struct InvalidCustomPropertyName {
16 name: String,
17}
18
19impl InvalidCustomPropertyName {
20 pub fn name(&self) -> &str {
22 &self.name
23 }
24}
25
26impl fmt::Display for InvalidCustomPropertyName {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 write!(
29 f,
30 "custom property name must match --[A-Za-z_][A-Za-z0-9_-]*, got {:?}",
31 self.name
32 )
33 }
34}
35
36impl std::error::Error for InvalidCustomPropertyName {}
37
38#[derive(Debug, PartialEq, Eq, Hash)]
48pub struct CssCustomProperty<T> {
49 name: Cow<'static, str>,
50 marker: PhantomData<fn() -> T>,
51}
52
53impl<T> Clone for CssCustomProperty<T> {
54 fn clone(&self) -> Self {
55 Self {
56 name: self.name.clone(),
57 marker: PhantomData,
58 }
59 }
60}
61
62impl<T: CheckedCssValue> CssCustomProperty<T> {
63 #[track_caller]
72 pub const fn from_static(name: &'static str) -> Self {
73 assert!(
74 is_checked_custom_property_name(name),
75 "invalid checked CSS custom-property name"
76 );
77 Self {
78 name: Cow::Borrowed(name),
79 marker: PhantomData,
80 }
81 }
82
83 #[track_caller]
90 pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
91 Self::try_new(name).unwrap_or_else(|error| panic!("{error}"))
92 }
93
94 pub fn try_new(name: impl Into<Cow<'static, str>>) -> Result<Self, InvalidCustomPropertyName> {
96 let name = name.into();
97 if is_checked_custom_property_name(&name) {
98 Ok(Self {
99 name,
100 marker: PhantomData,
101 })
102 } else {
103 Err(InvalidCustomPropertyName {
104 name: name.into_owned(),
105 })
106 }
107 }
108
109 pub fn as_str(&self) -> &str {
111 &self.name
112 }
113
114 pub fn declare(&self, value: impl Into<DeclarationValue<T>>) -> CheckedDeclaration {
119 CheckedDeclaration::new(self.name.clone(), value.into())
120 }
121}
122
123#[macro_export]
143macro_rules! css_custom_property {
144 ($(#[$meta:meta])* $vis:vis $name:ident : $value:ty = $css_name:literal $(;)?) => {
145 $(#[$meta])*
146 $vis const $name: $crate::CssCustomProperty<$value> =
147 $crate::CssCustomProperty::from_static($css_name);
148 };
149}
150
151impl<T: CheckedCssValue> fmt::Display for CssCustomProperty<T> {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 f.write_str(self.as_str())
154 }
155}
156
157#[derive(Clone, Debug, PartialEq)]
163pub struct DeclarationValue<T>(DeclarationValueKind<T>);
164
165#[derive(Clone, Debug, PartialEq)]
166enum DeclarationValueKind<T> {
167 Literal(T),
168 Variable(CssVariableReference<T>),
169}
170
171impl<T: CheckedCssValue> From<T> for DeclarationValue<T> {
172 fn from(value: T) -> Self {
173 Self(DeclarationValueKind::Literal(value))
174 }
175}
176
177#[derive(Clone, Debug, PartialEq)]
179pub struct CssVariableReference<T> {
180 property: CssCustomProperty<T>,
181 fallback: Box<DeclarationValue<T>>,
182}
183
184impl<T: CheckedCssValue> From<CssVariableReference<T>> for DeclarationValue<T> {
185 fn from(value: CssVariableReference<T>) -> Self {
186 Self(DeclarationValueKind::Variable(value))
187 }
188}
189
190impl<T: CheckedCssValue> checked_value::sealed::Sealed for DeclarationValue<T> {}
191
192impl<T: CheckedCssValue> checked_value::sealed::Sealed for CssVariableReference<T> {}
193
194impl<T: CheckedCssValue> CssWriteTo for DeclarationValue<T> {
195 fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
196 match &self.0 {
197 DeclarationValueKind::Literal(value) => value.css_fmt(w),
198 DeclarationValueKind::Variable(value) => value.css_fmt(w),
199 }
200 }
201}
202
203impl<T: CheckedCssValue> CssWriteTo for CssVariableReference<T> {
204 fn css_fmt<W: fmt::Write>(&self, w: &mut W) -> fmt::Result {
205 write!(w, "var({}, ", self.property)?;
206 self.fallback.css_fmt(w)?;
207 w.write_char(')')
208 }
209}
210
211impl<T: CheckedCssValue> fmt::Display for DeclarationValue<T> {
212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213 self.css_fmt(f)
214 }
215}
216
217impl<T: CheckedCssValue> fmt::Display for CssVariableReference<T> {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 self.css_fmt(f)
220 }
221}
222
223pub fn var<T: CheckedCssValue>(
240 property: &CssCustomProperty<T>,
241 fallback: impl Into<DeclarationValue<T>>,
242) -> CssVariableReference<T> {
243 CssVariableReference {
244 property: property.clone(),
245 fallback: Box::new(fallback.into()),
246 }
247}