Revert "lib: add Render::render_{event, prologue, epilogue}"

This reverts commit e8503e28fd.

This imposed too many limitations on the renderer implementation. E.g.
making it impossible to store `Event<'s>`'s in the renderer struct.

Revert back to having the renderer struct separate from the implementor
of the Render trait. The implementor may instead create a renderer
struct without any restrictions.
This commit is contained in:
Noah Hellman 2023-04-07 23:17:17 +02:00
parent 8e48021f7a
commit c4ecd0c677
3 changed files with 38 additions and 54 deletions

View file

@ -9,6 +9,32 @@ use crate::OrderedListNumbering::*;
use crate::Render; use crate::Render;
use crate::SpanLinkType; use crate::SpanLinkType;
#[derive(Default)]
pub struct Renderer {}
impl Render for Renderer {
fn push<'s, I, W>(&self, mut events: I, mut out: W) -> std::fmt::Result
where
I: Iterator<Item = Event<'s>>,
W: std::fmt::Write,
{
let mut w = Writer::default();
events.try_for_each(|e| w.render_event(&e, &mut out))?;
w.render_epilogue(&mut out)
}
fn push_borrowed<'s, E, I, W>(&self, mut events: I, mut out: W) -> std::fmt::Result
where
E: AsRef<Event<'s>>,
I: Iterator<Item = E>,
W: std::fmt::Write,
{
let mut w = Writer::default();
events.try_for_each(|e| w.render_event(e.as_ref(), &mut out))?;
w.render_epilogue(&mut out)
}
}
enum Raw { enum Raw {
None, None,
Html, Html,
@ -22,7 +48,7 @@ impl Default for Raw {
} }
#[derive(Default)] #[derive(Default)]
pub struct Renderer { struct Writer {
raw: Raw, raw: Raw,
img_alt_text: usize, img_alt_text: usize,
list_tightness: Vec<bool>, list_tightness: Vec<bool>,
@ -33,7 +59,7 @@ pub struct Renderer {
ignore: bool, ignore: bool,
} }
impl Render for Renderer { impl Writer {
fn render_event<'s, W>(&mut self, e: &Event<'s>, mut out: W) -> std::fmt::Result fn render_event<'s, W>(&mut self, e: &Event<'s>, mut out: W) -> std::fmt::Result
where where
W: std::fmt::Write, W: std::fmt::Write,

View file

@ -75,11 +75,6 @@ type CowStr<'s> = std::borrow::Cow<'s, str>;
/// If ownership of the [`Event`]s cannot be given to the renderer, use [`Render::push_borrowed`] /// If ownership of the [`Event`]s cannot be given to the renderer, use [`Render::push_borrowed`]
/// or [`Render::write_borrowed`]. /// or [`Render::write_borrowed`].
/// ///
/// An implementor needs to at least implement the [`Render::render_event`] function that renders a
/// single event to the output. If anything needs to be rendered at the beginning or end of the
/// output, the [`Render::render_prologue`] and [`Render::render_epilogue`] can be implemented as
/// well.
///
/// # Examples /// # Examples
/// ///
/// Push to a [`String`] (implements [`std::fmt::Write`]): /// Push to a [`String`] (implements [`std::fmt::Write`]):
@ -90,7 +85,7 @@ type CowStr<'s> = std::borrow::Cow<'s, str>;
/// # use jotdown::Render; /// # use jotdown::Render;
/// # let events = std::iter::empty(); /// # let events = std::iter::empty();
/// let mut output = String::new(); /// let mut output = String::new();
/// let mut renderer = jotdown::html::Renderer::default(); /// let renderer = jotdown::html::Renderer::default();
/// renderer.push(events, &mut output); /// renderer.push(events, &mut output);
/// # } /// # }
/// ``` /// ```
@ -103,54 +98,22 @@ type CowStr<'s> = std::borrow::Cow<'s, str>;
/// # use jotdown::Render; /// # use jotdown::Render;
/// # let events = std::iter::empty(); /// # let events = std::iter::empty();
/// let mut out = std::io::BufWriter::new(std::io::stdout()); /// let mut out = std::io::BufWriter::new(std::io::stdout());
/// let mut renderer = jotdown::html::Renderer::default(); /// let renderer = jotdown::html::Renderer::default();
/// renderer.write(events, &mut out).unwrap(); /// renderer.write(events, &mut out).unwrap();
/// # } /// # }
/// ``` /// ```
pub trait Render { pub trait Render {
/// Render a single event.
fn render_event<'s, W>(&mut self, e: &Event<'s>, out: W) -> std::fmt::Result
where
W: std::fmt::Write;
/// Render something before any events have been provided.
///
/// This does nothing by default, but an implementation may choose to prepend data at the
/// beginning of the output if needed.
fn render_prologue<W>(&mut self, _out: W) -> std::fmt::Result
where
W: std::fmt::Write,
{
Ok(())
}
/// Render something after all events have been provided.
///
/// This does nothing by default, but an implementation may choose to append extra data at the
/// end of the output if needed.
fn render_epilogue<W>(&mut self, _out: W) -> std::fmt::Result
where
W: std::fmt::Write,
{
Ok(())
}
/// Push owned [`Event`]s to a unicode-accepting buffer or stream. /// Push owned [`Event`]s to a unicode-accepting buffer or stream.
fn push<'s, I, W>(&mut self, mut events: I, mut out: W) -> fmt::Result fn push<'s, I, W>(&self, events: I, out: W) -> fmt::Result
where where
I: Iterator<Item = Event<'s>>, I: Iterator<Item = Event<'s>>,
W: fmt::Write, W: fmt::Write;
{
self.render_prologue(&mut out)?;
events.try_for_each(|e| self.render_event(&e, &mut out))?;
self.render_epilogue(&mut out)
}
/// Write owned [`Event`]s to a byte sink, encoded as UTF-8. /// Write owned [`Event`]s to a byte sink, encoded as UTF-8.
/// ///
/// NOTE: This performs many small writes, so IO writes should be buffered with e.g. /// NOTE: This performs many small writes, so IO writes should be buffered with e.g.
/// [`std::io::BufWriter`]. /// [`std::io::BufWriter`].
fn write<'s, I, W>(&mut self, events: I, out: W) -> io::Result<()> fn write<'s, I, W>(&self, events: I, out: W) -> io::Result<()>
where where
I: Iterator<Item = Event<'s>>, I: Iterator<Item = Event<'s>>,
W: io::Write, W: io::Write,
@ -177,26 +140,21 @@ pub trait Render {
/// # use jotdown::Render; /// # use jotdown::Render;
/// # let events: &[jotdown::Event] = &[]; /// # let events: &[jotdown::Event] = &[];
/// let mut output = String::new(); /// let mut output = String::new();
/// let mut renderer = jotdown::html::Renderer::default(); /// let renderer = jotdown::html::Renderer::default();
/// renderer.push_borrowed(events.iter(), &mut output); /// renderer.push_borrowed(events.iter(), &mut output);
/// # } /// # }
/// ``` /// ```
fn push_borrowed<'s, E, I, W>(&mut self, mut events: I, mut out: W) -> fmt::Result fn push_borrowed<'s, E, I, W>(&self, events: I, out: W) -> fmt::Result
where where
E: AsRef<Event<'s>>, E: AsRef<Event<'s>>,
I: Iterator<Item = E>, I: Iterator<Item = E>,
W: fmt::Write, W: fmt::Write;
{
self.render_prologue(&mut out)?;
events.try_for_each(|e| self.render_event(e.as_ref(), &mut out))?;
self.render_epilogue(&mut out)
}
/// Write borrowed [`Event`]s to a byte sink, encoded as UTF-8. /// Write borrowed [`Event`]s to a byte sink, encoded as UTF-8.
/// ///
/// NOTE: This performs many small writes, so IO writes should be buffered with e.g. /// NOTE: This performs many small writes, so IO writes should be buffered with e.g.
/// [`std::io::BufWriter`]. /// [`std::io::BufWriter`].
fn write_borrowed<'s, E, I, W>(&mut self, events: I, out: W) -> io::Result<()> fn write_borrowed<'s, E, I, W>(&self, events: I, out: W) -> io::Result<()>
where where
E: AsRef<Event<'s>>, E: AsRef<Event<'s>>,
I: Iterator<Item = E>, I: Iterator<Item = E>,

View file

@ -68,7 +68,7 @@ fn run() -> Result<(), std::io::Error> {
}; };
let parser = jotdown::Parser::new(&content); let parser = jotdown::Parser::new(&content);
let mut renderer = jotdown::html::Renderer::default(); let renderer = jotdown::html::Renderer::default();
match app.output { match app.output {
Some(path) => renderer.write(parser, File::create(path)?)?, Some(path) => renderer.write(parser, File::create(path)?)?,