Coder Social home page Coder Social logo

Add `UnwrapPretty` trait about miette HOT 5 OPEN

esoterra avatar esoterra commented on May 18, 2024
Add `UnwrapPretty` trait

from miette.

Comments (5)

esoterra avatar esoterra commented on May 18, 2024

Another possible idea that I'm less sure about is OkPretty as a replacement for calling ok() printing the diagnostic when it fails.

pub trait OkPretty {
    type Output;

    fn ok_pretty(self) -> Option<Self::Output>;
}

impl<T, E> OkPretty for Result<T, E>
where
    E: Diagnostic + Sync + Send + 'static
{
    type Output = T;

    fn ok_pretty(self) -> Option<Self::Output> {
        match self {
            Ok(output) => Some(output),
            Err(diagnostic) => {
                println!("{:?}", Report::new(diagnostic));
                None
            },
        }
    }
}

If I could pick one, it'd definitely be UnwrapPretty but I think both would be nice.

from miette.

zkat avatar zkat commented on May 18, 2024

I kinda like this idea, but I'm also debating whether it's good to include in miette.

Philosophically speaking, I think it's important to avoid unwraps as much as possible, and miette is designed with the intention of making it easier to avoid them.

But I can see how sometimes you just want to unwrap, such as tests! and how UnwrapPretty might be useful there.

That said: tests are not typically a place where I think fancy-looking diagnostics are really necessary. Fancy diagnostics are more intended for end-users of a tool.

I also definitely wouldn't want OkPretty because we should absolutely not be logging arbitrarily like that. I think doing that is a very application-specific decision to make, not something to encourage.

from miette.

esoterra avatar esoterra commented on May 18, 2024

Fair, OkPretty is pretty out there.

As someone writing a compiler right now, being able to unwrap in my tests and actually see where the error happened is incredibly helpful.

from miette.

zkat avatar zkat commented on May 18, 2024

Yes, but the cost of just adding this as your own utility, since you find it useful to you, is very very low. It might be best to keep it that way.

from miette.

esoterra avatar esoterra commented on May 18, 2024

Fair I did in fact implement this for my project.

I just meant that there are real use cases for this. Whether there are enough to justify inclusion 🤷‍♀️

https://github.com/esoterra/claw-lang/blob/main/crates/common/src/diagnostic.rs

from miette.

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.