-
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
Support String
type in ContextError
#744
Comments
I tried the /// A static error message string that contains all keywords that're valid in the current section.
static VALID_KEYWORDS_ERROR: OnceLock<&'static str> = OnceLock::new();
fn get_valid_keywords_error() -> &'static str {
VALID_KEYWORDS_ERROR.get_or_init(|| {
let mut concatenated = [
SourceKeyword::VARIANTS,
MetaKeyword::VARIANTS,
RelationKeyword::VARIANTS,
]
.concat()
.join(", ");
concatenated.insert_str(0, "one of the allowed properties: [");
concatenated.push(']');
Box::leak(concatenated.into_boxed_str())
})
} |
I just stumbled over another use-case for such a string context type. If we were to write a simple parser for that, it would be nice to have the digest type and the expected length for the given digest in the error message. |
While leaking or not probably doesn't make a difference, you might not need to. For example, clap uses |
The big problem is knowing how we should format a One option is for us to explore making A workaround is to create your own adapter from |
Making
That would be a nice :) |
Inspired by winnow-rs#744.
Please complete the following tasks
winnow version
0.7.2
Describe your use case
I'm currently writing a parser that detects a rather large set of keywords.
The keywords are represented as multiple enum types, that all implement
FromStr
.Multiple enums were chosen it makes it easier to look at certain sets of keywords in the scope of a specific topic.
When the parser cannot match any of the keywords, an error should be thrown that shows the list of all available keywords in the current scope. To do this programatically, the idea is to use strum's
VariantNames
. This provides an easy and most importantly reliable automatic way of concatenating all keywords of the utilized keywords. Manual solutions are likely to go out of sync over time, as one has to adjust multiple error messages and go through a rather large list of keywords to keep them in sync.In practice the error message creation would look something like this:
The discussion started over here in a different issue:
#496 (comment)
Right now it's possible to create a custom StringContext type in the likes of this:
Which can be called and used like this:
This works fine as long as one works with
ContextError
s. But as soon asparse
instead ofparse_next
is called, which returns aParseError
instead, this is no longer compatible:Display
is implemented forParseError
, withE
being the inner error, which is theContextError<StringContext>
in this case.Currently,
Display
is only implemented forContextError<StrContext>
:Since it's a foreign type, I'm not allowed to
impl Display for ContextError<StringContext>
.Describe the solution you'd like
An easy way to use
String
type context errors that may be used inparse
, so that we can write dynamic error messages that're build during runtime.The nicest solution would be to provide an
StringContext
context type upstream. That would make it very ergonomic to just use Strings in error messages and I hope it shouldn't be too much of a hassle to implement.Alternatives, if applicable
I tried using
const
functions to create static strings, but sadly the Rust language isn't quite there yet, or at least I couldn't find a non-obstruse way to do that.While writing this I came up with the idea that
once_cell
could be a solution for my problem at hand. But even if that works, this feature would still be a quality of life improvement, as it would allow easier debugging and error message creation without having to come up with a way of creating static strings first.Another alternative would be to implement a custom Error type, but that feels like overkill to achieve this.
Additional Context
No response
The text was updated successfully, but these errors were encountered: