-
Notifications
You must be signed in to change notification settings - Fork 29
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
Use consistent builder derive configs across API types #213
Conversation
cc @EdJoPaTo this is what you wanted. No more manual |
src/lib.rs
Outdated
@@ -37,3 +37,18 @@ mod trait_sync; | |||
|
|||
/// Default Bot API URL | |||
pub const BASE_API_URL: &str = "https://api.telegram.org/bot"; | |||
|
|||
macro_rules_attribute::attribute_alias! { |
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.
Moving this to its own file might be a good idea. Personally I try to only do pub use
and mod
in the lib/mod files to simplify the overview whats there. Not sure how well this works with macro_rules_attribute
?
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.
It uses pub(crate)
internally
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.
(My comment was not about the visibility, my comment was about having either submodules or content, not both)
Feel free to mark this conversation as resolved
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.
Not sure how well this works with macro_rules_attribute
My comment was to this. So yes, it works, because it uses pub(crate)
and thus can be accessed from other modules in the crate.
I like the basic idea of this! We can improve it a bit then I think this is a good simplification. |
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.
My first concern when looking into it was some smaller crate with not that much usage / being proven with time, but it seems to be used for quite some time. So I think this is a good and stable addition. Any other thoughts @ayrat555 ?
@EdJoPaTo for me it's looking good. wanted to get your review because you were strongly involved in the migration to the bon crate |
I found a really handy crate that allows you to define derive/attribute aliases etc. without defining a separate proc macro crate. This crate doesn't even depend on syn/quote/proc-macro2. The compile time is still the same for me (~17 seconds from scratch) after this change. This crate uses a bit of macro tricks where one macro generates another macro and thus can preserve some state.
With this approach it's possible to define the
builder
attribute alias in a single place and remove all the manual#[builder(into)]
attributes from all the API struct fields. Note that I deliberately didn't touch the Client builder syntax just to keep things simple there. I only modifiedapi_params.rs
andobjects.rs
.I think I'll add this to
bon
docs as a "global builder config" pattern.Also, another suggestion is that you can use
serde_with::skip_serializing_none
to avoid manually adding#[serde(skip_serializing_if = "Option::is_none")]
on everyOption
field. This can even be combined with themarco_rules_attribute
crate to make the code even cleaner.You can further develop the idea of using the
macro_rules_attribute
to reduce the boilerplate. E.g. move common derives such asDebug, Clone, ::serde::Serialize, ::Deserialize
as well as#[skip_serializing_none]
to the aliases.