-
Notifications
You must be signed in to change notification settings - Fork 13
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
Render
trait
#10
Comments
A benefit of having a trait could be to enable reuse of the adapter between std::io::Write/std::fmt::Write. However, here it looks like you need to implement both methods. We have a html::Writer object that could potentially implement some trait, e.g: pub trait Render {
// implement this
fn write_fmt<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write>(
&mut self,
events: I,
out: W,
) -> std::fmt::Result;
// and get these for free
fn push<'s, I: Iterator<Item = Event<'s>>, W: std::fmt::Write>(&mut self, events: I, out: W) {
self.write_fmt(events, out).unwrap();
}
fn write<'s, I: Iterator<Item = Event<'s>>, W: std::io::Write>(
&mut self,
events: I,
out: W,
) -> std::io::Result<()> {
// adapter stuff here
self.write_fmt(events, &mut output)
.map_err(|_| output.error.unwrap_err())
}
} Then you have a mutable reference, so you can configure it if needed when constructing it. |
Yes, using the adapter is a great suggestion! Is there a reason why you are unwrapping the error in |
Yeah, I think the html::Writer does not cause formatting errors at least because it always uses the write! macro which is checked at compile time. But I guess there is nothing stopping the std::fmt::Write implementor from causing an error when we try to write to it. Usually it is just a string which should never cause an error on writing to it, but it could be anything, I guess? So I guess we should not unwrap, and just propagate the result instead.
Yeah, that is a better idea, we should only need those two. |
Awesome! In that case, I'll draft this out tomorrow and open a PR where we can have a look at this. |
Closed by #12 |
Djot is not meant to be HTML-centric, and jotdown should not be either. Lets let library consumers express anything that can render jotdown events via a
Render
trait (or similarly named.) Like the HTML implementation, we would have to distinguish between a unicode-accepting writer and a byte sink. Thepush
andwrite
names seem suitable:The advantage of this approach over a module with standalone functions is obvious: Well-defined extensibility. This also lets libraries accept any rendering format in a more succinct way and additionally makes it clearer how one would implement their own renderer.
I'm debating whether we require a reference to self in the trait methods or not... One might want to allow extensions or rendering customisation, so might be better to include a reference.
The text was updated successfully, but these errors were encountered: