Skip to main content

plait/
html.rs

1use core::{fmt, ops::Deref};
2
3/// A wrapper type representing safe, pre-rendered HTML content.
4#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub struct Html(String);
6
7impl Html {
8    /// Create a new empty HTML string.
9    pub fn new() -> Self {
10        Html(String::new())
11    }
12
13    /// Create a new HTML string with a given capacity.
14    pub fn with_capacity(capacity: usize) -> Self {
15        Html(String::with_capacity(capacity))
16    }
17
18    /// Convert the HTML string into a `String`.
19    pub fn into_string(self) -> String {
20        self.0
21    }
22
23    /// Get a mutable reference to the inner `String`.
24    pub(crate) fn inner_mut(&mut self) -> &mut String {
25        &mut self.0
26    }
27}
28
29impl Deref for Html {
30    type Target = str;
31
32    fn deref(&self) -> &Self::Target {
33        &self.0
34    }
35}
36
37impl fmt::Display for Html {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        self.0.fmt(f)
40    }
41}
42
43impl From<Html> for String {
44    fn from(html: Html) -> Self {
45        html.0
46    }
47}
48
49impl PartialEq<&str> for Html {
50    fn eq(&self, other: &&str) -> bool {
51        self.0 == *other
52    }
53}