pub use fhtml_macros::*;
#[macro_export]
macro_rules! write {
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args!($($arg)*))
};
}
#[macro_export]
macro_rules! writeln {
($dst:expr $(,)?) => {
$crate::write!($dst, <br />)
};
($dst:expr, $($arg:tt)*) => {
$dst.write_fmt($crate::format_args_nl!($($arg)*))
};
}
#[macro_export]
macro_rules! format {
($($arg:tt)*) => {{
let res = ::std::fmt::format($crate::format_args!($($arg)*));
res
}};
}
#[inline]
pub fn escape<T: AsRef<str>>(input: T) -> String {
let input = input.as_ref();
let mut escaped = String::with_capacity(input.len());
for c in input.chars() {
match c {
'&' => escaped.push_str("&"),
'<' => escaped.push_str("<"),
'>' => escaped.push_str(">"),
'"' => escaped.push_str("""),
'\'' => escaped.push_str("'"),
_ => escaped.push(c),
}
}
escaped
}
#[cfg(test)]
mod tests {
use std::fmt::Write;
#[test]
fn write() {
let mut output = String::new();
let _ = crate::write!(output, <h1>"Hello, world!"</h1>);
assert_eq!(output, "<h1>Hello, world!</h1>");
}
#[test]
fn writeln() {
let mut output = String::new();
let _ = crate::writeln!(output, <h1>"Hello, world!"</h1>);
assert_eq!(output, "<h1>Hello, world!</h1><br>");
}
#[test]
fn format() {
assert_eq!(
crate::format!(<h1>"Hello, world!"</h1>),
"<h1>Hello, world!</h1>"
);
}
#[test]
fn concat() {
assert_eq!(crate::concat!(<div>{1}</div>), "<div>1</div>");
assert_eq!(
crate::concat!(<img src="/foo.png" alt="foo" />),
"<img src=\"/foo.png\" alt=\"foo\">"
);
}
}