#![no_std]
#![warn(missing_docs)]
extern crate alloc;
use alloc::string::String;
#[cfg(test)]
extern crate std;
pub mod context;
pub mod error;
pub mod escape;
pub mod hidden;
pub mod render;
pub use context::{
AnimationContext, AnimationValue, Context, Descriptor, DocumentContext, DocumentValue,
HtmlContext, HtmlValue, NavigationContext, NavigationValue, ResourceContext, ResourceValue,
ScriptContext, ScriptValue, Srcset, SrcsetContext, SrcsetValue, StyleContext, StyleValue,
Trusted, TrustedAnimation, TrustedDocument, TrustedHtml, TrustedNavigation, TrustedResource,
TrustedScript, TrustedSrcset, TrustedStyle, TrustedUrl, Url, UrlContext, UrlError, UrlValue,
};
pub use error::RenderError;
pub use escape::HtmlEscape;
pub use hidden::{BooleanAttribute, HiddenAttribute, HiddenValue};
pub trait Hemplate {
fn render_into(&self, buf: &mut dyn core::fmt::Write) -> Result<(), RenderError>;
fn size_hint(&self) -> usize {
0
}
fn render(&self) -> Result<String, RenderError> {
let mut buf = String::with_capacity(self.size_hint());
self.render_into(&mut buf)?;
Ok(buf)
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Empty;
impl Hemplate for Empty {
fn render_into(&self, _buf: &mut dyn core::fmt::Write) -> Result<(), RenderError> {
Ok(())
}
}
struct Text;
impl Hemplate for Text {
fn render_into(&self, buf: &mut dyn core::fmt::Write) -> Result<(), RenderError> {
buf.write_str("rendered")?;
Ok(())
}
}
struct Failing;
impl Hemplate for Failing {
fn render_into(&self, _buf: &mut dyn core::fmt::Write) -> Result<(), RenderError> {
Err(core::fmt::Error.into())
}
}
#[test]
fn manual_implementation_defaults_to_no_size_hint() {
assert_eq!(Empty.size_hint(), 0);
}
#[test]
fn render_returns_generated_output_and_propagates_failure() -> Result<(), RenderError> {
assert_eq!(Text.render()?, "rendered");
assert!(Failing.render().is_err());
Ok(())
}
}