Skip to main content

basecoat_core/
markup.rs

1use std::borrow::Cow;
2use std::fmt;
3
4/// Pre-escaped HTML fragment. Do **not** double-escape when rendering.
5///
6/// Construct with `Markup::from_static` (zero-alloc for string literals) or
7/// `Markup::from(String)` for dynamically-built HTML.  Never pass user
8/// controlled strings directly — escape them first with `crate::attrs::escape_attr`.
9#[derive(Clone, Debug, PartialEq, Eq, Default)]
10pub struct Markup(pub Cow<'static, str>);
11
12impl Markup {
13    /// An empty markup fragment (allocates nothing).
14    pub fn empty() -> Self {
15        Markup(Cow::Borrowed(""))
16    }
17
18    /// Borrow a `'static` string literal — zero allocation.
19    pub fn from_static(s: &'static str) -> Self {
20        Markup(Cow::Borrowed(s))
21    }
22}
23
24impl fmt::Display for Markup {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.write_str(&self.0)
27    }
28}
29
30impl From<String> for Markup {
31    fn from(s: String) -> Self {
32        Markup(Cow::Owned(s))
33    }
34}
35
36impl From<&'static str> for Markup {
37    fn from(s: &'static str) -> Self {
38        Markup(Cow::Borrowed(s))
39    }
40}