Code Monkey home page Code Monkey logo

Comments (5)

hellux avatar hellux commented on August 18, 2024

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.

from jotdown.

kmaasrud avatar kmaasrud commented on August 18, 2024

Yes, using the adapter is a great suggestion! Is there a reason why you are unwrapping the error in push? I guess it's since Djot parsing can't really error, but we may want to give renderers the possibility to throw an error either way. Maybe we only need push and write then, push being the required implementation?

from jotdown.

hellux avatar hellux commented on August 18, 2024

Is there a reason why you are unwrapping the error in push? I guess it's since Djot parsing can't really error, but we may want to give renderers the possibility to throw an error either way.

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.

Maybe we only need push and write then, push being the required implementation?

Yeah, that is a better idea, we should only need those two.

from jotdown.

kmaasrud avatar kmaasrud commented on August 18, 2024

Awesome! In that case, I'll draft this out tomorrow and open a PR where we can have a look at this.

from jotdown.

kmaasrud avatar kmaasrud commented on August 18, 2024

Closed by #12

from jotdown.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.