-
-
Notifications
You must be signed in to change notification settings - Fork 123
✨ zv: Added a Signature Macro #1536
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?
Conversation
@hadihaider055 thanks! I'll have a look later but please remember to use emoji rather than the emoji code. 😉 |
Ah sure @zeenix, I just saw this on mobile and it seems like the emoji code isn't working. Thanks for pointing it out! |
A signature! macro that constructs a const singature Fixes dbus2#984
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. 😉 |
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.
Super cool. 👍 I've been meaning to do this for a while now. :) Just two things apart from the inline comments:
- It's a bit unexpected to have a non-derive macro in a crate suffixed
_derive
. I think, it possible, we should add this inzvariant_utils
and then re-export fromzvariant
. - 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 { |
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.
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
).
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() |
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.
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.
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