+
Skip to content

Conversation

hadihaider055
Copy link
Contributor

Add a signature! macro that creates const signatures with compile-time validation. This makes it easier to work with D-Bus signatures while catching invalid ones early.

Fixes #984

@zeenix
Copy link
Contributor

zeenix commented Oct 9, 2025

@hadihaider055 thanks! I'll have a look later but please remember to use emoji rather than the emoji code. 😉

@hadihaider055
Copy link
Contributor Author

Ah sure @zeenix, I just saw this on mobile and it seems like the emoji code isn't working. Thanks for pointing it out!

@hadihaider055 hadihaider055 changed the title ✨ zv: Added a Signature Macro ✨ zv: Added a Signature Macro Oct 9, 2025
A signature! macro that constructs a const singature

Fixes dbus2#984
@zeenix
Copy link
Contributor

zeenix commented Oct 9, 2025

Ah sure @zeenix, I just saw this on mobile and it seems like the emoji code isn't working. Thanks for pointing it out

It won't work in many software (including git cli tools). That's why it's best to follow the advice in the contribution guidelines exactly. 😉

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.

Super cool. 👍 I've been meaning to do this for a while now. :) Just two things apart from the inline comments:

  1. It's a bit unexpected to have a non-derive macro in a crate suffixed _derive. I think, it possible, we should add this in zvariant_utils and then re-export from zvariant.
  2. Let's make use of this in the zvariant tests where we create signature from a static string (there are at least 3 places, I can see).

}

fn signature_to_tokens(signature: &Signature, zv: &TokenStream) -> TokenStream {
pub fn signature_to_tokens(signature: &Signature, zv: &TokenStream) -> TokenStream {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pub(crate) no? Also if it's going to be shared between modules, best to either put it in separate module or utils.rs. Actually, I think it can simply be moved to the new module (perhaps named signature) you'll create for the macro impl (see my other comment in lib.rs).

Comment on lines +506 to +523
let result = || -> Result<proc_macro2::TokenStream, syn::Error> {
let signature_str = syn::parse::<Literal>(input)?;

let signature_string = signature_str.to_string();
let signature_string = signature_string.trim_matches('"');

let signature = match signature_string {
"dict" => Signature::dict(Signature::Str, Signature::Variant),
s => Signature::from_str(s).map_err(|e| syn::Error::new(signature_str.span(), e))?,
};

let zv = utils::zvariant_path();
let signature_tokens = r#type::signature_to_tokens(&signature, &zv);

Ok(signature_tokens)
};

result().unwrap_or_else(|err| err.to_compile_error()).into()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very strange pattern to me and also inconsistent with how we do things in the other macros. Please split the impl into a separate function in seperate module.

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.

Provide a signature! macro that constructs a const singature

2 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载