Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Conversation

@Hendrik-Hamerlinck
Copy link
Contributor

This PR updates the DBusError derive 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

#[derive(Debug, DBusError)]
pub enum ExampleDBusError {
   UnitVariant,
   UnnamedVariant(u32, String),
   UnnamedVariantString(String),
   NamedVariant { code: u32, message: String },
   NamedVariantString { message: String },
}

This now compiles and expands the description as following

fn description(&self) -> Option<&str> {
    match self {
        Self::UnitVariant => None,
        Self::UnnamedVariant(..) => None,
        Self::UnnamedVariantString(desc, ..) => Some(desc),
        Self::NamedVariant { .. } => None,
        Self::NamedVariantString { message } => Some(message),
    }
}

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.
@zeenix
Copy link
Contributor

zeenix commented Dec 17, 2025

@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.

You can also pick an emoji direcitly from here (please remember to copy the emoji itself and not the :emoji-code: string, by just clicking on it). NOTE: This is a curated list of emojis that have specific meanings. Please use one of the methods recommended here to select/fetch the most appropriate one. πŸ™

@zeenix zeenix changed the title πŸ› zm: Handle non-String fields in error variant matching πŸ₯… zm: Handle non-String fields in error variant matching Dec 17, 2025
@zeenix
Copy link
Contributor

zeenix commented Dec 17, 2025

I took the liberty of adding the most appropriate emoji to the PR title (please copy&paste that on the commit title).

Copy link
Contributor

@zeenix zeenix left a 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) => (|| {
Copy link
Contributor

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.

Copy link
Contributor Author

@Hendrik-Hamerlinck Hendrik-Hamerlinck Dec 17, 2025

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?

Copy link
Contributor

@zeenix zeenix Dec 17, 2025

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.

Copy link
Contributor Author

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-hq
Copy link

codspeed-hq bot commented Dec 17, 2025

CodSpeed Performance Report

Merging #1619 will not alter performance

Comparing barcoopensource:fix/error-variant-non-strings (013e003) with main (7e447ac)

Summary

βœ… 22 untouched

@Hendrik-Hamerlinck
Copy link
Contributor Author

@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.

You can also pick an emoji direcitly from here (please remember to copy the emoji itself and not the :emoji-code: string, by just clicking on it). NOTE: This is a curated list of emojis that have specific meanings. Please use one of the methods recommended here to select/fetch the most appropriate one. πŸ™

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.

@zeenix
Copy link
Contributor

zeenix commented Jan 9, 2026

@Hendrik-Hamerlinck Hi, could you please rebase this branch?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants