From c20be49085c9093af4a525bb5e9ee1f29effdf55 Mon Sep 17 00:00:00 2001 From: Ross MacArthur Date: Sat, 29 Jul 2023 15:43:30 +0200 Subject: [PATCH] Remove generic from `Renderer` and duplicate impl --- src/lib.rs | 4 +- src/render/mod.rs | 110 ++++++---------------------------------------- 2 files changed, 15 insertions(+), 99 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index de5b4da..d16c8c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -486,7 +486,7 @@ impl<'render> Template<'render, 'render> { #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[inline] - pub fn render(&self, ctx: S) -> Renderer<'_, S> + pub fn render(&self, ctx: S) -> Renderer<'_> where S: serde::Serialize, { @@ -541,7 +541,7 @@ impl<'render> TemplateRef<'render> { #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[inline] - pub fn render(&self, ctx: S) -> Renderer<'_, S> + pub fn render(&self, ctx: S) -> Renderer<'_> where S: serde::Serialize, { diff --git a/src/render/mod.rs b/src/render/mod.rs index 409571b..96e84ae 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -57,24 +57,22 @@ where /// - [`Template{,Ref}::render`][crate::Template::render] /// - [`Template{,Ref}::render_from`][crate::Template::render_from] /// - [`Template{,Ref}::render_from_fn`][crate::Template::render_from_fn] -#[cfg(feature = "serde")] #[must_use = "must call `.to_string()` or `.to_writer(..)` on the renderer"] -pub struct Renderer<'render, S = ()> { +pub struct Renderer<'render> { engine: &'render Engine<'render>, template: &'render Template<'render>, - globals: Globals<'render, S>, + globals: Globals<'render>, } -#[cfg(feature = "serde")] -enum Globals<'render, S = ()> { - Serde(S), +enum Globals<'render> { + Owned(Result), Borrowed(&'render Value), Fn(Box>), } -#[cfg(feature = "serde")] -impl<'render, S> Renderer<'render, S> { - pub(crate) fn with_serde( +impl<'render> Renderer<'render> { + #[cfg(feature = "serde")] + pub(crate) fn with_serde( engine: &'render Engine<'render>, template: &'render Template<'render>, globals: S, @@ -85,7 +83,7 @@ impl<'render, S> Renderer<'render, S> { Self { engine, template, - globals: Globals::Serde(globals), + globals: Globals::Owned(crate::to_value(globals)), } } @@ -114,13 +112,10 @@ impl<'render, S> Renderer<'render, S> { } /// Render the template to a string. - pub fn to_string(self) -> Result - where - S: ::serde::Serialize, - { + pub fn to_string(self) -> Result { match self.globals { - Globals::Serde(ctx) => { - let value = crate::to_value(ctx)?; + Globals::Owned(result) => { + let value = result?; let stack = Stack::new(&value); to_string(self.engine, self.template, stack) } @@ -139,11 +134,10 @@ impl<'render, S> Renderer<'render, S> { pub fn to_writer(self, w: W) -> Result<()> where W: io::Write, - S: ::serde::Serialize, { match self.globals { - Globals::Serde(ctx) => { - let value = crate::to_value(ctx)?; + Globals::Owned(result) => { + let value = result?; let stack = Stack::new(&value); to_writer(self.engine, self.template, stack, w) } @@ -158,81 +152,3 @@ impl<'render, S> Renderer<'render, S> { } } } - -/// A renderer that interprets a compiled [`Template`][crate::Template]. -/// -/// This struct is created by one of the following functions: -/// - [`Template{,Ref}::render`][crate::Template::render] -/// - [`Template{,Ref}::render_from`][crate::Template::render_from] -/// - [`Template{,Ref}::render_from_fn`][crate::Template::render_from_fn] -#[cfg(not(feature = "serde"))] -#[must_use = "must call `.to_string()` or `.to_writer(..)` on the renderer"] -pub struct Renderer<'render> { - engine: &'render Engine<'render>, - template: &'render Template<'render>, - globals: Globals<'render>, -} - -#[cfg(not(feature = "serde"))] -enum Globals<'render> { - Borrowed(&'render Value), - Fn(Box>), -} - -#[cfg(not(feature = "serde"))] -impl<'render> Renderer<'render> { - pub(crate) fn with_value( - engine: &'render Engine<'render>, - template: &'render Template<'render>, - globals: &'render Value, - ) -> Self { - Self { - engine, - template, - globals: Globals::Borrowed(globals), - } - } - - pub(crate) fn with_value_fn( - engine: &'render Engine<'render>, - template: &'render Template<'render>, - value_fn: Box>, - ) -> Self { - Self { - engine, - template, - globals: Globals::Fn(value_fn), - } - } - - /// Render the template to a string. - pub fn to_string(self) -> Result { - match self.globals { - Globals::Borrowed(value) => { - let stack = Stack::new(value); - to_string(self.engine, self.template, stack) - } - Globals::Fn(value_fn) => { - let stack = Stack::with_value_fn(&value_fn); - to_string(self.engine, self.template, stack) - } - } - } - - /// Render the template to the given writer. - pub fn to_writer(self, w: W) -> Result<()> - where - W: io::Write, - { - match self.globals { - Globals::Borrowed(value) => { - let stack = Stack::new(value); - to_writer(self.engine, self.template, stack, w) - } - Globals::Fn(value_fn) => { - let stack = Stack::with_value_fn(&value_fn); - to_writer(self.engine, self.template, stack, w) - } - } - } -}