1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
use std::fmt::Display;
use std::io::{self, Write};
/// This trait should be implemented for any value that can be the
/// result of an expression in a template.
///
/// This trait decides how to format the given object as html.
/// There exists a default implementation for any `T: Display` that
/// formats the value using Display and then html-encodes the result.
pub trait ToHtml {
/// Write self to `out`, which is in html representation.
fn to_html(&self, out: &mut dyn Write) -> io::Result<()>;
/// Write the HTML represention of this value to a buffer.
///
/// This can be used for testing, and for short-cutting situations
/// with complex ownership, since the resulting buffer gets owned
/// by the caller.
///
/// # Examples
/// ```ignore
/// # fn main() -> std::io::Result<()> {
/// # use ructe::templates;
/// use templates::ToHtml;
/// assert_eq!(17.to_buffer()?, "17");
/// assert_eq!("a < b".to_buffer()?, "a < b");
/// # Ok(())
/// # }
/// ```
fn to_buffer(&self) -> io::Result<HtmlBuffer> {
let mut buf = Vec::new();
self.to_html(&mut buf)?;
Ok(HtmlBuffer { buf })
}
}
/// Return type for [`ToHtml::to_buffer`].
///
/// An opaque heap-allocated buffer containing a rendered HTML snippet.
pub struct HtmlBuffer {
#[doc(hidden)]
buf: Vec<u8>,
}
impl std::fmt::Debug for HtmlBuffer {
fn fmt(&self, out: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(out, "HtmlBuffer({:?})", String::from_utf8_lossy(&self.buf))
}
}
impl ToHtml for HtmlBuffer {
fn to_html(&self, out: &mut dyn Write) -> io::Result<()> {
out.write_all(&self.buf)
}
}
impl AsRef<[u8]> for HtmlBuffer {
fn as_ref(&self) -> &[u8] {
&self.buf
}
}
impl PartialEq<&[u8]> for HtmlBuffer {
fn eq(&self, other: &&[u8]) -> bool {
&self.buf == other
}
}
impl PartialEq<&str> for HtmlBuffer {
fn eq(&self, other: &&str) -> bool {
let other: &[u8] = other.as_ref();
self.buf == other
}
}
/// Wrapper object for data that should be outputted as raw html
/// (objects that may contain markup).
#[allow(dead_code)]
pub struct Html<T>(pub T);
impl<T: Display> ToHtml for Html<T> {
#[inline]
fn to_html(&self, out: &mut dyn Write) -> io::Result<()> {
write!(out, "{}", self.0)
}
}
impl<T: Display> ToHtml for T {
#[inline]
fn to_html(&self, out: &mut dyn Write) -> io::Result<()> {
write!(ToHtmlEscapingWriter(out), "{self}")
}
}
struct ToHtmlEscapingWriter<'a>(&'a mut dyn Write);
impl<'a> Write for ToHtmlEscapingWriter<'a> {
#[inline]
// This takes advantage of the fact that `write` doesn't have to write everything,
// and the call will be retried with the rest of the data
// (it is a part of `write_all`'s loop or similar.)
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
// quickly skip over data that doesn't need escaping
let n = data
.iter()
.take_while(|&&c| {
c != b'"' && c != b'&' && c != b'\'' && c != b'<' && c != b'>'
})
.count();
if n > 0 {
self.0.write(&data[0..n])
} else {
Self::write_one_byte_escaped(&mut self.0, data)
}
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl<'a> ToHtmlEscapingWriter<'a> {
#[inline(never)]
fn write_one_byte_escaped(
out: &mut impl Write,
data: &[u8],
) -> io::Result<usize> {
let next = data.first();
out.write_all(match next {
Some(b'"') => b""",
Some(b'&') => b"&",
Some(b'<') => b"<",
Some(b'>') => b">",
None => return Ok(0),
// we know this function is called only for chars that need escaping,
// so we don't have to handle the "other" case (this one is for `'`)
_ => b"'",
})?;
Ok(1)
}
}