-
Notifications
You must be signed in to change notification settings - Fork 53
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
Miette or Ariadne integration, at least in documentation #104
Comments
Updated as Miette seems to be the more popular one, by download count |
I'm assuming this should build on #103 |
I'm currently experimenting with combining My parser is for a custom schema language that is very Rust inspired. I'm doing a few things to create some context, to have something to print in case of an error.
My integration with Please suggest alternatives if this is not the way to use the use miette::{IntoDiagnostic, LabeledSpan, MietteDiagnostic, Report, Result, Severity};
use winnow::{error::StrContext, Parser};
impl<'a> Schema<'a> {
pub fn parse(input: &'a str) -> Result<Self> {
use winnow::Parser;
parser::parse_schema.parse(input).map_err(|e| {
let offset = e.offset();
let inner = e.inner();
let scopes = inner
.context()
.filter_map(|ctx| {
if let StrContext::Label(label) = ctx {
Some(label)
} else {
None
}
})
.collect::<Vec<_>>();
// Very specific formatting of the labels, this forms messages like
// - "invalid struct name"
// - "invalid enum name"
// ...
let message = match scopes.as_slice() {
[&"name", element, ..] => format!("invalid {element} name"),
[first, ..] => format!("invalid {first}"),
[] => "invalid schema".to_owned(),
};
let mut diagnostic = MietteDiagnostic::new(message)
.with_severity(Severity::Error)
.with_code("stef::parse::invalid_schema");
// Just take the most "inner" label at the moment
// Would love to add them all, but still have to find a way
// to carry around the offsets or byte ranges
let label = inner.context().find_map(|ctx| {
if let StrContext::Expected(value) = ctx {
Some(LabeledSpan::at_offset(offset, format!("expected {value}")))
} else {
None
}
});
if let Some(label) = label {
diagnostic = diagnostic.with_label(label);
}
Report::new(diagnostic).with_source_code(input.to_owned())
})
}
} This is one of my schemas, with an invalid struct name. It must start with an uppercase ASCII letter, but this doesn't. struct sample {
options: [bool; 5] @1,
} Then I get this nice error output with the above logic (originally colored):
I only have played with this for 1–2 hours now. Hopefully this gives some starting point for the docs or some deeper integration even. A few points that I struggle with, currently:
|
Thats an interesting idea to use the pattern of
Thanks! When someone (me?) does write the example and put it in the Special Topic, I'm thinking it'll be json since that is a commonly understood format and can easily be compared to the existing implementations.
If I'm understanding correctly, you are wanting In addition to some of the thoughts you had on this,
In
I agree. In |
I wasn't away it allocates. I thought, as it takes a
True. I was just hoping to keep my parsers compact and easy to grep with the current version. Once I add some context to every little thing it'll be much harder to comprehend. But if that's the price to get nice detailed errors, it's probably okay.
Oh that is a nice alternative over my current So it seems like my best bet in extending the error reporting might be a custom error? Currently, I'm not tracking any spans, but I'll have to attach them to each element at some point anyway. Then I have to wrap the parser in a My idea is to actually not use the I'll experiment with that over the next days and let you know if I could succeed in that 😄 |
To support multiple
Was the performance loss with 0.4 or 0.5? 0.5 dramatically improved performance for
Be sure to watch the performance of your custom error. If you add too many fields, it can lead to a performance hit and you might end up needing to put some things on the heap to overcome it. |
Please complete the following tasks
winnow version
0.2.0
Describe your use case
Good error messages are important Users should easily get messages like this
Describe the solution you'd like
Unsure
Alternatives, if applicable
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: