Skip to main content

columbo/
html.rs

1use std::fmt;
2
3/// Pre-escaped HTML content. Columbo's canonical markup type.
4///
5/// This is a thin wrapper around a `String` that represents raw HTML. No
6/// escaping is performed — the caller is responsible for ensuring the content
7/// is safe.
8///
9/// External types convert into `Html` via [`From`] implementations:
10/// - `From<String>` and `From<&str>` for raw HTML strings.
11/// - `From<maud::Markup>` when the `maud` feature is enabled.
12#[derive(Clone, Debug, Default)]
13pub struct Html(String);
14
15impl Html {
16  /// Create a new `Html` from raw HTML content.
17  pub fn new(raw_html: impl Into<String>) -> Self { Html(raw_html.into()) }
18
19  /// Consume the `Html` and return the inner string.
20  pub fn into_string(self) -> String { self.0 }
21
22  /// View the inner HTML as a string slice.
23  pub fn as_str(&self) -> &str { &self.0 }
24}
25
26impl fmt::Display for Html {
27  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28    f.write_str(&self.0)
29  }
30}
31
32impl From<String> for Html {
33  fn from(s: String) -> Self { Html(s) }
34}
35
36impl From<&str> for Html {
37  fn from(s: &str) -> Self { Html(s.to_owned()) }
38}
39
40#[cfg(feature = "maud")]
41impl From<maud::Markup> for Html {
42  fn from(m: maud::Markup) -> Self { Html(m.into_string()) }
43}