-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Move ContextKind
and ContextValue
into one enum
#5683
Comments
Are you referring to a person creating an error or rendering it? Some of your comments make it sound like both perspectives and I'm wanting to better understand your use case. For whichever side you are on, what problem are you solving by using this API?
Something to consider is how far do we go on being more statically typed. The This API also gives us a lot more flexibility to evolve things without committing to any specific details, allowing us to gracefully degrade. Originally, our error type was not open to extension for content or rendering. Opening it up was to offer some extra flexibility for more advanced cases and is lower in our design priority list compared to other values. |
As for some of the details...
With your proposed API, users rendering their own errors would still need to be non-exhaustive because we'd use
|
I'm kind of referring to both sides, not only were contexts not well documented for me as an end-user (what was and wasn't supported by each formatter). It was annoying to improve it because it was hard to communicate to a potential end-user what should and shouldn't be used. If not this, then at least provide documentation over either the formatter implementations or the
While this concern is valid, as it stands the context API is relatively hard to use and unspecific. Perhaps a feature-gated |
Sounds like your primary interest is in generating errors and in support of improving that, the complexity of implementing a formatter was a concern. For generating errors, could you expand on why you are doing so? What problems are you solving within your application? Understanding use cases is important for knowing what to improve, how to improve it, and how urgent improving it is. |
I was trying to write a Maybe the above usage is a misuse, but it seemed simpler and more lightweight to use the auto-generated messages instead of something custom. To add to the confusion, the example given here uses |
Thanks for providing this context! You can do |
|
Could you provide concrete examples of what you are trying to get out of context, of that special reporting? Its helpful to understand the specific problems you are running into to make sure we are on the same page and can consider the right set of solutions, rather than solutions for a situation I guessed that isn't what you are doing. For example, looking over
|
impl TypedValueParser for MonitorParser {
type Value = &'static Monitor;
fn parse_ref(
&self,
cmd: &Command,
arg: Option<&Arg>,
value: &OsStr,
) -> Result<Self::Value, Error> {
// guaranteed to be non-empty by parser
let value = NonEmptyStringValueParser::new().parse_ref(cmd, arg, value)?;
let monitors = get_monitors();
match monitors {
Ok(monitors) => monitors.iter().find(|v| v.name == value).ok_or(
Error::new(ErrorKind::ValueValidation)
.with_context(ContextKind::InvalidArg, ContextValue::String(value))
.with_context(
ContextKind::ValidValue,
ContextValue::Strings(monitors.iter().map(|v| v.name.clone()).collect()),
)
.with_cmd(cmd),
),
Err(e) => Err(Error::new(ErrorKind::ValueValidation)
.with_context(
ContextKind::Custom,
ContextValue::String(format!("Error whilst fetching monitors: {e}")),
)
.with_cmd(cmd)),
}
}
} Expected Result:Some kind of output saying Actual Result:
|
As a heads up, I was primarily looking for your "Expected Result" which you left out. I'm assuming you are looking for
(with arguments/values actually related to your code) I'm assuming the use of I'm also assuming the reason the possible values aren't defined ahead of time is that its a slow, failable operation. A workaround would be to have your This type of pattern seems reasonable enough that I wonder if we should have built-in support, e.g. |
I think that this a great compromise between an in-depth system and nothing. I would be happy to write a PR adding a Most likely same technique used |
Yes, feel free to post a PR for I was originally hoping to have this built-in to |
Please complete the following tasks
Clap Version
master
Describe your use case
When looking at the clap error formatters there are lots of non-rusty things such as multiple
if let
and non-exhaustive enum matching when dealing with contexts:clap/clap_builder/src/error/format.rs
Lines 105 to 106 in d811585
Not only is this code messy, it also makes using contexts generally annoying for an end user without digging through the code to see which contexts have been implemented and what kinds of values they can take.
Describe the solution you'd like
This could be done better by merging
ContextKind
andContextValue
into one enum as well as switching the code in existing formatters to usematch
instead ofError.get()
. (Unless there is some reason that it is being used as such.)Enforcing that certain
ContextKind
be restricted to only certain values would make the context API more accessible as well as allowing a singular match instead of multiple if statements in formatters. This can be done with a singleContext
enum that contains theContextKind
and multiple nested<context type>ContextValue
enums.Alternatives, if applicable
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: