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

Can renderer of related errors (optionally) preserve source code? #99

Closed
tailhook opened this issue Jan 5, 2022 · 2 comments
Closed
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@tailhook
Copy link
Contributor

tailhook commented Jan 5, 2022

The scenario is like:

struct ParentError {
    #[source_code]
    source: String,
    #[related]
    other: Vec<ChildError>,
}

I can use Arc to copy source code to every child error, but this is a bit inconvenient. Parsers usually only propagate byte range as a span so I have to either wrap every error into another wrapper having source code, or propagate some custom span that includes Arc<SourceCode>.

Can handler propagate that source code while rendering or are there any big downsides of that?

@zkat
Copy link
Owner

zkat commented Jan 5, 2022

This can be done pretty easily by using an error wrapper that "forwards" the source_code implementation

Would that work for you? It seemed to work pretty well for engine-q.

@tailhook
Copy link
Contributor Author

tailhook commented Jan 5, 2022

This means I should make a manual wrapper with a related method kinda like this:

fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
  Some(self.other.iter().map(|e| ErrorWrapper { source: &self.source, error: e }))
}

But it's impossible to do as iterator should return a reference and I can not get a reference for the temporary object like above. So it will need to end up something like this:

struct ParentError {
    #[source_code]
    source: Arc<String>,
    #[related]
    other: Vec<ErrorWrapper<ChildError>>,
}

Still having an Arc to clone the source code into every error.

@zkat zkat added enhancement New feature or request help wanted Extra attention is needed labels Jan 8, 2022
tailhook added a commit to tailhook/miette that referenced this issue Feb 18, 2022
tailhook added a commit to tailhook/miette that referenced this issue Feb 18, 2022
@zkat zkat closed this as completed in 3a17fce Feb 20, 2022
tailhook added a commit to tailhook/miette that referenced this issue Feb 22, 2022
zkat pushed a commit that referenced this issue Feb 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants