use crate::str_types::SafeHtmlStr;
pub trait SafeHtmlPart {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result;
fn size_hint(&self) -> usize {
0
}
}
#[doc(hidden)]
pub trait DynPart {
fn dyn_write_to(&self, w: &mut dyn core::fmt::Write) -> core::fmt::Result;
fn dyn_size_hint(&self) -> usize;
}
impl<T: SafeHtmlPart + ?Sized> DynPart for T {
fn dyn_write_to(&self, w: &mut dyn core::fmt::Write) -> core::fmt::Result {
self.write_to(w)
}
fn dyn_size_hint(&self) -> usize {
self.size_hint()
}
}
#[doc(hidden)]
#[cfg(feature = "alloc")]
pub fn cat_from_parts(parts: &[&dyn DynPart]) -> crate::types::SafeHtml {
let mut cap = 0usize;
for part in parts {
cap = cap.saturating_add(part.dyn_size_hint());
}
let mut buf = alloc::string::String::with_capacity(cap);
for part in parts {
let _ = part.dyn_write_to(&mut buf);
}
crate::__private::finish_safe_html(buf)
}
impl<T: SafeHtmlPart + ?Sized> SafeHtmlPart for &T {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
(**self).write_to(w)
}
fn size_hint(&self) -> usize {
(**self).size_hint()
}
}
impl<T: SafeHtmlPart> SafeHtmlPart for Option<T> {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
match self {
Some(part) => part.write_to(w),
None => Ok(()),
}
}
fn size_hint(&self) -> usize {
match self {
Some(part) => part.size_hint(),
None => 0,
}
}
}
pub struct DisplayEscaped<T: core::fmt::Display>(T);
pub fn disp<T: core::fmt::Display>(value: T) -> DisplayEscaped<T> {
DisplayEscaped(value)
}
impl<T: core::fmt::Display> SafeHtmlPart for DisplayEscaped<T> {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
struct EscapingWriter<'a, W: core::fmt::Write + ?Sized>(&'a mut W);
impl<W: core::fmt::Write + ?Sized> core::fmt::Write for EscapingWriter<'_, W> {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
crate::escape_html_and_coerce_to(s, &mut *self.0)
}
}
use core::fmt::Write as _;
write!(EscapingWriter(w), "{}", self.0)
}
}
impl SafeHtmlPart for str {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
crate::escape_html_and_coerce_to(self, w)
}
fn size_hint(&self) -> usize {
self.len()
}
}
impl SafeHtmlPart for char {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
let mut tmp = [0; 4];
crate::escape_html_and_coerce_to(self.encode_utf8(&mut tmp), w)
}
fn size_hint(&self) -> usize {
self.len_utf8()
}
}
impl SafeHtmlPart for SafeHtmlStr {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
w.write_str(self.as_str())
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
impl SafeHtmlPart for crate::IdentifierStr {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
w.write_str(self.as_str())
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
macro_rules! impl_safe_html_part_for_display {
($($t:ty => $hint:expr),* $(,)?) => {
$(
impl SafeHtmlPart for $t {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
write!(w, "{}", self)
}
fn size_hint(&self) -> usize {
$hint
}
}
)*
};
}
impl_safe_html_part_for_display!(
i8 => 4, i16 => 6, i32 => 11, i64 => 20, i128 => 40, isize => 20,
u8 => 3, u16 => 5, u32 => 10, u64 => 20, u128 => 39, usize => 20,
f32 => 16, f64 => 24, bool => 5,
);
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl SafeHtmlPart for alloc::string::String {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
crate::escape_html_and_coerce_to(self.as_str(), w)
}
fn size_hint(&self) -> usize {
self.len()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl SafeHtmlPart for crate::types::SafeHtml {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
w.write_str(self.as_str())
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl SafeHtmlPart for crate::types::Identifier {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
w.write_str(self.as_str())
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
impl SafeHtmlPart for crate::SafeUrlStr {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
crate::escape_html_and_coerce_to(self.as_str(), w)
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
impl SafeHtmlPart for crate::TrustedResourceUrlStr {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
crate::escape_html_and_coerce_to(self.as_str(), w)
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl SafeHtmlPart for crate::types::SafeUrl {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
crate::escape_html_and_coerce_to(self.as_str(), w)
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl SafeHtmlPart for crate::types::TrustedResourceUrl {
fn write_to<W: core::fmt::Write + ?Sized>(&self, w: &mut W) -> core::fmt::Result {
crate::escape_html_and_coerce_to(self.as_str(), w)
}
fn size_hint(&self) -> usize {
self.as_str().len()
}
}