-
-
Notifications
You must be signed in to change notification settings - Fork 140
π₯ zm: Handle non-String fields in error variant matching #1619
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
base: main
Are you sure you want to change the base?
π₯ zm: Handle non-String fields in error variant matching #1619
Conversation
Return None when the first field of a DBusError variant is not a String. This allows custom error variants with non-String or no fields to work, addressing a FIXME in the macro implementation.
|
@Hendrik-Hamerlinck Thanks so much for this contribution. π I'll have a look at it soon but let me start with a nitpick about commit emoji. π This is not a bug fix since I don't think we have advertised anywhere that this already works, nor it has every worked AFAIK. Also I have to ask, how come you ended up using the emoji code rather than the emoji character? You are far from the first one to make this mistake so this is not on you but more like me failing in communicating this in the contribution guide. I'm only asking so I can try to make this clearer.
|
|
I took the liberty of adding the most appropriate emoji to the PR title (please copy&paste that on the commit title). |
zeenix
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM in general. Thanks again for this contribution. Apart from the nitpicks (sorry, I'm a perfectionist π), we need to also update the macro docs (in lib.rs) and need a test case (perhaps just updating the example in the docs is sufficient).
| Self::#ident => None, | ||
| }, | ||
| Fields::Unnamed(_) => { | ||
| Fields::Unnamed(fields) => (|| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why switch to anonymous functions, just to call them immediately? This makes this change more intrusive than it would otherwise need to be and it's best to keep changes as small as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I was not entirely happy about it. In my own version where we use rust 2024 I did something like this:
Fields::Named(n) => {
if let Some(first_field) = n.named.first()
&& let Type::Path(type_path) = &first_field.ty
&& type_path.path.is_ident("String")
{
let f = &first_field.ident;
quote! {
Self::#ident { #f, } => Some(#f),
}
} else {
quote! {
Self::#ident { .. } => None,
}
}
}But the project is on 2021 and you cannot chain let pattern matching. So to prevent having to return the same value in each else branch I switched to an inline function, so that I could return directly. Perhaps there are better ways to do it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid nesting, I tend to use multiple if let .. else statements in a row combined with early return/break (in the else blocks). I think you can use that here.
2024 edition was stabilized in 1.85, which is almost a year old now so I'd be happy to bump the MSRV and switch to that edition. However, that should happen in a separate PR, if you'd like to contribute that as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, that would simplify this change. While updating the tests I noticed that my change only works under the 2024 edition. I'll send a PR for bumping the MSRV.
CodSpeed Performance ReportMerging #1619 will not alter performanceComparing Summary
|
Thanks for the quick review. Missing on the emoji is on me, I just noticed I didn't read the entire emoji part through. After clicking on the first link I assumed you just needed to copy the code. I will edit it as requested. |
|
@Hendrik-Hamerlinck Hi, could you please rebase this branch? |
This PR updates the
DBusErrorderive macro to support enum variants where the first field is not a String.Previously, such cases resulted in an error when using custom errors. Now, the macro attempts to parse the first field as a String. If parsing fails, it returns None instead.
Example
This now compiles and expands the
descriptionas following