-
Notifications
You must be signed in to change notification settings - Fork 121
feat(macros): builder params #926
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
Open
Berrysoft
wants to merge
4
commits into
compio-rs:master
Choose a base branch
from
Berrysoft:dev/macros-more-params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,65 +1,211 @@ | ||
| use proc_macro2::TokenStream; | ||
| use quote::quote; | ||
| use quote::{ToTokens, TokenStreamExt, quote}; | ||
| use syn::{ | ||
| Attribute, Expr, Lit, Meta, Signature, Visibility, parse::Parse, punctuated::Punctuated, | ||
| Attribute, Expr, ExprLit, Ident, Lit, Meta, Path, Signature, Token, Visibility, parse::Parse, | ||
| punctuated::Punctuated, | ||
| }; | ||
|
|
||
| type AttributeArgs = Punctuated<syn::Meta, syn::Token![,]>; | ||
| use crate::{retrieve_driver_mod, retrieve_runtime_mod}; | ||
|
|
||
| struct MetaPunctuated(Punctuated<Meta, Token![,]>); | ||
|
|
||
| impl Parse for MetaPunctuated { | ||
| fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { | ||
| Ok(Self(Punctuated::parse_terminated(input)?)) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct BuilderMethod { | ||
| pub name: Ident, | ||
| pub value: Expr, | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub(crate) struct RawAttr { | ||
| pub inner_attrs: AttributeArgs, | ||
| pub runtime_methods: Vec<BuilderMethod>, | ||
| pub proactor_methods: Vec<BuilderMethod>, | ||
| pub crate_name: Option<TokenStream>, | ||
| pub with_proactor_call: Option<Path>, | ||
| } | ||
|
|
||
| impl Parse for RawAttr { | ||
| fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { | ||
| let inner_attrs = AttributeArgs::parse_terminated(input)?; | ||
| Ok(Self { inner_attrs }) | ||
| let items = Punctuated::<Meta, Token![,]>::parse_terminated(input)?; | ||
|
|
||
| let mut runtime_methods = Vec::new(); | ||
| let mut proactor_methods = Vec::new(); | ||
| let mut crate_name = None; | ||
| let mut with_proactor_call = None; | ||
|
|
||
| for meta in items { | ||
| match meta { | ||
| Meta::List(list) => { | ||
| if list.path.is_ident("with_proactor") { | ||
| if !list.tokens.is_empty() { | ||
| let inner_items = syn::parse2::<MetaPunctuated>(list.tokens)?.0; | ||
| for inner_meta in inner_items { | ||
| if let Meta::NameValue(nv) = inner_meta { | ||
| let name = nv.path.require_ident()?.clone(); | ||
| proactor_methods.push(BuilderMethod { | ||
| name, | ||
| value: nv.value, | ||
| }); | ||
| } else { | ||
| return Err(syn::Error::new_spanned( | ||
| inner_meta, | ||
| "expected `name = value` inside `with_proactor`", | ||
| )); | ||
| } | ||
| } | ||
| with_proactor_call = Some(list.path.clone()); | ||
| } | ||
| } else { | ||
| return Err(syn::Error::new_spanned( | ||
| list.path, | ||
| "unknown key; use `name = value` for parameters or \ | ||
| `with_proactor(...)` for proactor config", | ||
| )); | ||
| } | ||
| } | ||
| Meta::NameValue(nv) => { | ||
| if nv.path.is_ident("crate") { | ||
| if let Expr::Lit(ExprLit { | ||
| lit: Lit::Str(s), .. | ||
| }) = &nv.value | ||
| { | ||
| crate_name = Some(s.parse::<TokenStream>()?); | ||
| } else { | ||
| crate_name = Some(nv.value.into_token_stream()); | ||
| } | ||
| } else { | ||
| let name = nv.path.require_ident()?.clone(); | ||
| runtime_methods.push(BuilderMethod { | ||
| name, | ||
| value: nv.value, | ||
| }); | ||
| } | ||
| } | ||
| Meta::Path(path) => { | ||
| return Err(syn::Error::new_spanned( | ||
| path, | ||
| "expected `name = value` or `with_proactor(...)`", | ||
| )); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(Self { | ||
| runtime_methods, | ||
| proactor_methods, | ||
| crate_name, | ||
| with_proactor_call, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) struct RawBodyItemFn { | ||
| pub attrs: Vec<Attribute>, | ||
| pub args: AttributeArgs, | ||
| pub args: RawAttr, | ||
| pub vis: Visibility, | ||
| pub sig: Signature, | ||
| pub body: TokenStream, | ||
| pub test: bool, | ||
| } | ||
|
|
||
| impl RawBodyItemFn { | ||
| pub fn new(attrs: Vec<Attribute>, vis: Visibility, sig: Signature, body: TokenStream) -> Self { | ||
| Self { | ||
| attrs, | ||
| args: AttributeArgs::new(), | ||
| args: RawAttr::default(), | ||
| vis, | ||
| sig, | ||
| body, | ||
| test: false, | ||
| } | ||
| } | ||
|
|
||
| pub fn set_args(&mut self, args: AttributeArgs) { | ||
| pub fn set_args(&mut self, args: RawAttr) { | ||
| self.args = args; | ||
| } | ||
|
|
||
| pub fn crate_name(&self) -> Option<TokenStream> { | ||
| for attr in &self.args { | ||
| if let Meta::NameValue(name) = &attr { | ||
| let ident = name | ||
| .path | ||
| .get_ident() | ||
| .map(|ident| ident.to_string().to_lowercase()) | ||
| .unwrap_or_default(); | ||
| if ident == "crate" { | ||
| if let Expr::Lit(lit) = &name.value | ||
| && let Lit::Str(s) = &lit.lit | ||
| { | ||
| let crate_name = s.parse::<TokenStream>().unwrap(); | ||
| return Some(quote!(#crate_name::runtime)); | ||
| } | ||
| } else { | ||
| panic!("Unsupported property {ident}"); | ||
| } | ||
| pub fn set_test(&mut self, test: bool) { | ||
| self.test = test; | ||
| } | ||
|
|
||
| pub fn emit_fn_to_tokens(&self, tokens: &mut TokenStream) { | ||
| if self.test { | ||
| tokens.append_all(quote!(#[test])); | ||
| } | ||
| tokens.append_all( | ||
| self.attrs | ||
| .iter() | ||
| .filter(|a| matches!(a.style, syn::AttrStyle::Outer)), | ||
| ); | ||
| self.vis.to_tokens(tokens); | ||
| self.sig.to_tokens(tokens); | ||
| tokens.append_all(self.gen_runtime_block()); | ||
| } | ||
|
|
||
| fn gen_runtime_block(&self) -> TokenStream { | ||
| let runtime_mod = match &self.args.crate_name { | ||
| Some(c) => { | ||
| let c = c.clone(); | ||
| quote!(#c::runtime) | ||
| } | ||
| None => retrieve_runtime_mod(), | ||
| }; | ||
|
|
||
| let driver_mod = match &self.args.crate_name { | ||
| Some(c) => { | ||
| let c = c.clone(); | ||
| quote!(#c::driver) | ||
| } | ||
| None => retrieve_driver_mod(), | ||
| }; | ||
|
|
||
| let block = &self.body; | ||
|
|
||
| let mut builder = quote! { | ||
| #runtime_mod::Runtime::builder() | ||
| }; | ||
|
|
||
| for method in &self.args.runtime_methods { | ||
| let name = &method.name; | ||
| let value = &method.value; | ||
| builder = quote! { | ||
| #builder.#name(#value) | ||
| }; | ||
| } | ||
|
Berrysoft marked this conversation as resolved.
|
||
|
|
||
| if !self.args.proactor_methods.is_empty() { | ||
| let mut proactor_stmts: Vec<TokenStream> = Vec::new(); | ||
| proactor_stmts.push(quote! { | ||
| let mut __compio_proactor_builder = #driver_mod::Proactor::builder(); | ||
| }); | ||
| for method in &self.args.proactor_methods { | ||
| let name = &method.name; | ||
| let value = &method.value; | ||
| proactor_stmts.push(quote! { | ||
| __compio_proactor_builder.#name(#value); | ||
| }); | ||
| } | ||
| // Preserve the original token for the `with_proactor` call to make the language | ||
| // server work better. | ||
| let with_proactor_call = if let Some(path) = &self.args.with_proactor_call { | ||
| quote!(#path) | ||
| } else { | ||
| quote!(with_proactor) | ||
| }; | ||
| builder = quote! { | ||
| #builder.#with_proactor_call({ | ||
| #(#proactor_stmts)* | ||
| __compio_proactor_builder | ||
| }) | ||
| }; | ||
| } | ||
| None | ||
|
|
||
| quote!({ | ||
| #builder.build().expect("cannot create runtime").block_on(async move #block) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.