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
use v_htmlescape::VHtmlescape;
pub use cuach_derive::template;
pub trait Render {
type Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error>;
fn render(&self) -> Result<String, Self::Error> {
let mut v = String::new();
self.render_into(&mut v)?;
Ok(v)
}
}
impl Render for () {
type Error = ();
fn render_into<W: std::fmt::Write>(&self, _: &mut W) -> Result<(), Self::Error> {
Ok(())
}
}
impl<'a, R: Render> Render for &'a R {
type Error = R::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok((*self).render_into(w)?)
}
}
impl Render for usize {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for isize {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for i32 {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for i64 {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for u32 {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for u64 {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for f32 {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl Render for f64 {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
impl<'a> Render for &'a str {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", VHtmlescape::new(self.as_bytes()))?)
}
}
impl Render for String {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", VHtmlescape::new(self.as_bytes()))?)
}
}
#[derive(Debug, Clone)]
pub struct PreEscaped<S: AsRef<str>>(pub S);
impl<S: AsRef<str>> Render for PreEscaped<S> {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self.0.as_ref())?)
}
}
impl Render for uuid::Uuid {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
Ok(write!(w, "{}", self)?)
}
}
use std::borrow::Borrow;
impl<'a> Render for std::borrow::Cow<'a, str> {
type Error = std::fmt::Error;
fn render_into<W: std::fmt::Write>(&self, w: &mut W) -> Result<(), Self::Error> {
let r: &str = self.borrow();
r.render_into(w)
}
}