Skip to main content

rama_http/protocols/html/rama_impls/
strings.rs

1//! `IntoHtml` for the rama-utils string-like types.
2//!
3//! Each type gets two impls: one consuming the value (escapes on render)
4//! and one for `&T`. `PreEscaped<T>` versions write the inner string
5//! verbatim instead of escaping.
6
7use rama_utils::str::{
8    NonEmptyStr,
9    arcstr::{ArcStr, Substr},
10    smol_str::SmolStr,
11};
12
13use crate::protocols::html::core::{IntoHtml, PreEscaped, escape_into};
14
15macro_rules! impl_str_like {
16    ($ty:ty) => {
17        impl IntoHtml for $ty {
18            #[inline]
19            fn into_html(self) -> impl IntoHtml {
20                self
21            }
22            #[inline]
23            fn escape_and_write(self, buf: &mut String) {
24                escape_into(buf, self.as_str())
25            }
26            #[inline]
27            fn size_hint(&self) -> usize {
28                self.len()
29            }
30        }
31
32        impl IntoHtml for &$ty {
33            #[inline]
34            fn into_html(self) -> impl IntoHtml {
35                self
36            }
37            #[inline]
38            fn escape_and_write(self, buf: &mut String) {
39                escape_into(buf, self.as_str())
40            }
41            #[inline]
42            fn size_hint(&self) -> usize {
43                self.len()
44            }
45        }
46
47        impl IntoHtml for PreEscaped<$ty> {
48            #[inline]
49            fn into_html(self) -> impl IntoHtml {
50                self
51            }
52            #[inline]
53            fn escape_and_write(self, buf: &mut String) {
54                buf.push_str(self.0.as_str());
55            }
56            #[inline]
57            fn size_hint(&self) -> usize {
58                self.0.len()
59            }
60        }
61    };
62}
63
64impl_str_like!(ArcStr);
65impl_str_like!(Substr);
66impl_str_like!(SmolStr);
67
68// `NonEmptyStr` exposes the inner string via `Deref`/`AsRef<str>` rather
69// than `as_str`, so it does not fit the macro above.
70impl IntoHtml for NonEmptyStr {
71    #[inline]
72    fn into_html(self) -> impl IntoHtml {
73        self
74    }
75    #[inline]
76    fn escape_and_write(self, buf: &mut String) {
77        escape_into(buf, &self)
78    }
79    #[inline]
80    fn size_hint(&self) -> usize {
81        self.len()
82    }
83}
84
85impl IntoHtml for &NonEmptyStr {
86    #[inline]
87    fn into_html(self) -> impl IntoHtml {
88        self
89    }
90    #[inline]
91    fn escape_and_write(self, buf: &mut String) {
92        escape_into(buf, self)
93    }
94    #[inline]
95    fn size_hint(&self) -> usize {
96        self.len()
97    }
98}
99
100impl IntoHtml for PreEscaped<NonEmptyStr> {
101    #[inline]
102    fn into_html(self) -> impl IntoHtml {
103        self
104    }
105    #[inline]
106    fn escape_and_write(self, buf: &mut String) {
107        buf.push_str(&self.0);
108    }
109    #[inline]
110    fn size_hint(&self) -> usize {
111        self.0.len()
112    }
113}