Skip to content
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

SourceCode on Option<impl SourceCode>? #217

Closed
mimoo opened this issue Oct 30, 2022 · 3 comments · Fixed by #301
Closed

SourceCode on Option<impl SourceCode>? #217

mimoo opened this issue Oct 30, 2022 · 3 comments · Fixed by #301

Comments

@mimoo
Copy link

mimoo commented Oct 30, 2022

I would find it useful if there were such an implementation:

impl miette::SourceCode for Option<NamedSource> {
    fn read_span<'a>(
        &'a self,
        span: &miette::SourceSpan,
        context_lines_before: usize,
        context_lines_after: usize,
    ) -> std::result::Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
        self.expect("no source code was set in the error").read_span(span, context_lines_before, context_lines_after)     
    }
}

or perhaps a more general implementation:

impl<T> miette::SourceCode for Option<T> where T: miette::SourceCode {
    fn read_span<'a>(
        &'a self,
        span: &miette::SourceSpan,
        context_lines_before: usize,
        context_lines_after: usize,
    ) -> std::result::Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
        self.expect("no source code was set in the error").read_span(span, context_lines_before, context_lines_after)     
    }
}

this way I can have an optional source up until I set it:

#[source_code]
src: Option<NamedSource>

BTW I also noticed that I can't call this field source otherwise I get a weird error. Might be good to have a better error when I call the field source instead of src

@olivia-fl
Copy link
Contributor

Having an implementation for Option<T: SourceCode> that panics at runtime when it's None is not ideal, since it makes it easy to accidentally cause a panic when trying to print an error. I think there's a better option though. The Diagnostic::source_code method returns Option<&dyn SourceCode>. Currently the Diagnostic derive macro always returns Some(src) when a field is annotated with #[source_code], and always returns None otherwise. We could update it to pass src through as an Option when applicable, which should handle your use-case.

@erratic-pattern
Copy link
Contributor

Just wanted to say that I would also find this very useful on #[diagnostic_source] as well, to optionally provide more detailed debugging info based on environment variables, cli options, configuration, etc. This could also use a similar implementation to what @Benjamin-L suggested for #[source_code]

@zkat
Copy link
Owner

zkat commented Apr 24, 2023

this might be a nice thing to have, yes.

And regarding calling the field source: this is because thiserror uses that field name to determine its own source. It's not a miette thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants