Skip to main content

topcoat_view/attribute/
key.rs

1use topcoat_core::context::Cx;
2
3use crate::{PartsWriter, Unescaped};
4
5/// Converts a value used as an attribute key into view parts.
6///
7/// When this trait is implemented on a type, it can be used in the attribute key position of an
8/// element in the [`view!`](https://docs.rs/topcoat/latest/topcoat/view/macro.view.html) macro:
9///
10/// ```rust
11/// # use topcoat::view::{component, view};
12/// # #[component]
13/// # async fn example() -> topcoat::Result {
14/// # let my_key = "data-state";
15/// view! {
16///     <div (my_key)="value"></div>
17/// }
18/// # }
19/// ```
20pub trait AttributeKeyViewParts {
21    /// Appends this attribute key to the view being built.
22    fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>);
23}
24
25impl AttributeKeyViewParts for String {
26    #[inline]
27    fn into_view_parts(self, _cx: &Cx, parts: &mut PartsWriter<'_>) {
28        parts.push_str(self);
29    }
30}
31
32impl AttributeKeyViewParts for &str {
33    #[inline]
34    fn into_view_parts(self, _cx: &Cx, parts: &mut PartsWriter<'_>) {
35        parts.push_str(self.to_owned());
36    }
37}
38
39impl AttributeKeyViewParts for Unescaped<String> {
40    #[inline]
41    fn into_view_parts(self, _cx: &Cx, parts: &mut PartsWriter<'_>) {
42        parts.push_str_unescaped(self.0);
43    }
44}
45
46impl AttributeKeyViewParts for Unescaped<&'static str> {
47    #[inline]
48    fn into_view_parts(self, _cx: &Cx, parts: &mut PartsWriter<'_>) {
49        parts.push_str_unescaped(self.0);
50    }
51}
52
53impl AttributeKeyViewParts for &String {
54    #[inline]
55    fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>) {
56        self.as_str().into_view_parts(cx, parts);
57    }
58}
59
60impl<'b, T: ?Sized> AttributeKeyViewParts for &&'b T
61where
62    &'b T: AttributeKeyViewParts,
63{
64    #[inline]
65    fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>) {
66        (*self).into_view_parts(cx, parts);
67    }
68}
69
70macro_rules! impl_tuple {
71    ($($ty:ident),+) => {
72        impl<$($ty),+> AttributeKeyViewParts for ($($ty,)+)
73        where
74            $($ty: AttributeKeyViewParts,)+
75        {
76            #[inline]
77            #[allow(non_snake_case)]
78            fn into_view_parts(self, cx: &Cx, parts: &mut PartsWriter<'_>) {
79                let ($($ty,)+) = self;
80                $($ty.into_view_parts(cx, parts);)+
81            }
82        }
83    };
84}
85
86impl_tuple!(T1);
87impl_tuple!(T1, T2);
88impl_tuple!(T1, T2, T3);
89impl_tuple!(T1, T2, T3, T4);
90impl_tuple!(T1, T2, T3, T4, T5);
91impl_tuple!(T1, T2, T3, T4, T5, T6);
92impl_tuple!(T1, T2, T3, T4, T5, T6, T7);
93impl_tuple!(T1, T2, T3, T4, T5, T6, T7, T8);
94impl_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
95impl_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
96impl_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
97impl_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);