Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose the fact that templates implement Display #654

Merged
merged 2 commits into from
Mar 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions askama_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ mod parser;
/// Main `Template` trait; implementations are generally derived
///
/// If you need an object-safe template, use [`DynTemplate`].
pub trait Template {
pub trait Template: fmt::Display {
/// Helper method which allocates a new `String` and renders into it
fn render(&self) -> Result<String> {
let mut buf = String::with_capacity(Self::SIZE_HINT);
self.render_into(&mut buf)?;
Ok(buf)
}

/// Renders the template to the given `writer` buffer
/// Renders the template to the given `writer` fmt buffer
fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()>;

/// Renders the template to the given `writer` io buffer
#[inline]
fn write_into(&self, writer: &mut (impl std::io::Write + ?Sized)) -> std::io::Result<()> {
writer.write_fmt(format_args!("{}", self))
}

/// The template's extension, if provided
const EXTENSION: Option<&'static str>;

Expand All @@ -57,9 +63,12 @@ pub trait DynTemplate {
/// Helper method which allocates a new `String` and renders into it
fn dyn_render(&self) -> Result<String>;

/// Renders the template to the given `writer` buffer
/// Renders the template to the given `writer` fmt buffer
fn dyn_render_into(&self, writer: &mut dyn std::fmt::Write) -> Result<()>;

/// Renders the template to the given `writer` io buffer
fn dyn_write_into(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()>;

/// Helper function to inspect the template's extension
fn extension(&self) -> Option<&'static str>;

Expand All @@ -79,6 +88,11 @@ impl<T: Template> DynTemplate for T {
<Self as Template>::render_into(self, writer)
}

#[inline]
fn dyn_write_into(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
writer.write_fmt(format_args!("{}", self))
}

fn extension(&self) -> Option<&'static str> {
Self::EXTENSION
}
Expand All @@ -92,6 +106,12 @@ impl<T: Template> DynTemplate for T {
}
}

impl fmt::Display for dyn DynTemplate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.dyn_render_into(f).map_err(|_| ::std::fmt::Error {})
}
}

#[derive(Debug)]
struct Config<'a> {
dirs: Vec<PathBuf>,
Expand Down Expand Up @@ -603,10 +623,27 @@ mod tests {
const MIME_TYPE: &'static str = "text/plain; charset=utf-8";
}

impl fmt::Display for Test {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.render_into(f).map_err(|_| fmt::Error {})
}
}

fn render(t: &dyn DynTemplate) -> String {
t.dyn_render().unwrap()
}

assert_eq!(render(&Test), "test");
let test = &Test as &dyn DynTemplate;

assert_eq!(render(test), "test");

assert_eq!(test.to_string(), "test");

assert_eq!(format!("{}", test), "test");

let mut vec = Vec::new();
test.dyn_write_into(&mut vec).unwrap();
assert_eq!(vec, vec![b't', b'e', b's', b't']);
}
}