Skip to content

Commit

Permalink
Remove generic from Renderer and duplicate impl
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Jul 29, 2023
1 parent 40f1b47 commit c20be49
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ impl<'render> Template<'render, 'render> {
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[inline]
pub fn render<S>(&self, ctx: S) -> Renderer<'_, S>
pub fn render<S>(&self, ctx: S) -> Renderer<'_>
where
S: serde::Serialize,
{
Expand Down Expand Up @@ -541,7 +541,7 @@ impl<'render> TemplateRef<'render> {
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
#[inline]
pub fn render<S>(&self, ctx: S) -> Renderer<'_, S>
pub fn render<S>(&self, ctx: S) -> Renderer<'_>
where
S: serde::Serialize,
{
Expand Down
110 changes: 13 additions & 97 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>),
Borrowed(&'render Value),
Fn(Box<ValueFn<'render>>),
}

#[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<S>(
engine: &'render Engine<'render>,
template: &'render Template<'render>,
globals: S,
Expand All @@ -85,7 +83,7 @@ impl<'render, S> Renderer<'render, S> {
Self {
engine,
template,
globals: Globals::Serde(globals),
globals: Globals::Owned(crate::to_value(globals)),
}
}

Expand Down Expand Up @@ -114,13 +112,10 @@ impl<'render, S> Renderer<'render, S> {
}

/// Render the template to a string.
pub fn to_string(self) -> Result<String>
where
S: ::serde::Serialize,
{
pub fn to_string(self) -> Result<String> {
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)
}
Expand All @@ -139,11 +134,10 @@ impl<'render, S> Renderer<'render, S> {
pub fn to_writer<W>(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)
}
Expand All @@ -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<ValueFn<'render>>),
}

#[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<ValueFn<'render>>,
) -> Self {
Self {
engine,
template,
globals: Globals::Fn(value_fn),
}
}

/// Render the template to a string.
pub fn to_string(self) -> Result<String> {
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<W>(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)
}
}
}
}

0 comments on commit c20be49

Please sign in to comment.