Skip to content
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

Merged
merged 5 commits into from
Sep 17, 2024

Conversation

Veetaha
Copy link
Contributor

@Veetaha Veetaha commented Sep 16, 2024

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 modified api_params.rs and objects.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 every Option field. This can even be combined with the marco_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 as Debug, Clone, ::serde::Serialize, ::Deserialize as well as #[skip_serializing_none] to the aliases.

@Veetaha
Copy link
Contributor Author

Veetaha commented Sep 16, 2024

cc @EdJoPaTo this is what you wanted. No more manual #[builder(into)] attributes and a suggestion to remove manual #[serde(skip_serializing_if = "Option::is_none")] attributes as well (I'll leave the serde exercise to you though).

@ayrat555 ayrat555 requested a review from EdJoPaTo September 16, 2024 06:26
Cargo.toml Outdated Show resolved Hide resolved
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! {
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

@EdJoPaTo EdJoPaTo Sep 17, 2024

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

Copy link
Contributor Author

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.

src/api_params.rs Outdated Show resolved Hide resolved
@EdJoPaTo
Copy link
Collaborator

I like the basic idea of this! We can improve it a bit then I think this is a good simplification.

@Veetaha Veetaha requested a review from EdJoPaTo September 17, 2024 12:42
Copy link
Collaborator

@EdJoPaTo EdJoPaTo left a 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 ?

@ayrat555
Copy link
Owner

@EdJoPaTo for me it's looking good. wanted to get your review because you were strongly involved in the migration to the bon crate

@ayrat555 ayrat555 merged commit 13c35c1 into ayrat555:master Sep 17, 2024
28 checks passed
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.

3 participants