-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ContractArgs for building function args
- Loading branch information
1 parent
e7eccd3
commit c349f06
Showing
9 changed files
with
163 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use proc_macro2::{Span, TokenStream}; | ||
use quote::{format_ident, quote}; | ||
use syn::{Error, FnArg, Lifetime, Path, Type, TypePath, TypeReference}; | ||
|
||
use crate::syn_ext; | ||
|
||
pub fn derive_args_type(ty: &str, name: &str) -> TokenStream { | ||
let ty_str = quote!(#ty).to_string(); | ||
// Render the Client. | ||
let args_doc = format!("{name} is a type for buildings function args defined in {ty_str}."); | ||
let args_ident = format_ident!("{}", name); | ||
quote! { | ||
#[doc = #args_doc] | ||
pub struct #args_ident; | ||
} | ||
} | ||
|
||
pub fn derive_args_impl(crate_path: &Path, name: &str, fns: &[syn_ext::Fn]) -> TokenStream { | ||
// Map the traits methods to methods for the Args. | ||
let mut errors = Vec::<Error>::new(); | ||
let fns: Vec<_> = fns | ||
.iter() | ||
.map(|f| { | ||
let fn_ident = &f.ident; | ||
|
||
// Check for the Env argument. | ||
let env_input = f.inputs.first().and_then(|a| match a { | ||
FnArg::Typed(pat_type) => { | ||
let mut ty = &*pat_type.ty; | ||
if let Type::Reference(TypeReference { elem, .. }) = ty { | ||
ty = elem; | ||
} | ||
if let Type::Path(TypePath { | ||
path: syn::Path { segments, .. }, | ||
.. | ||
}) = ty | ||
{ | ||
if segments.last().map_or(false, |s| s.ident == "Env") { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
} | ||
FnArg::Receiver(_) => None, | ||
}); | ||
|
||
// Map all remaining inputs. | ||
let fn_input_lifetime = Lifetime::new("'i", Span::call_site()); | ||
let (fn_input_names, fn_input_types): (Vec<_>, Vec<_>) = f | ||
.inputs | ||
.iter() | ||
.skip(if env_input.is_some() { 1 } else { 0 }) | ||
.map(|t| { | ||
let ident = match syn_ext::fn_arg_ident(t) { | ||
Ok(ident) => ident, | ||
Err(e) => { | ||
errors.push(e); | ||
format_ident!("_") | ||
} | ||
}; | ||
(ident, syn_ext::fn_arg_make_ref(t, Some(&fn_input_lifetime))) | ||
}) | ||
.unzip(); | ||
|
||
quote! { | ||
// TODO: Make the fn_input_types an impl Borrow | ||
pub fn #fn_ident<#fn_input_lifetime>(#(#fn_input_types),*) | ||
-> impl #crate_path::IntoVal<#crate_path::Env, #crate_path::Vec<#crate_path::Val>> + #fn_input_lifetime | ||
{ | ||
(#(#fn_input_names,)*) | ||
} | ||
} | ||
}) | ||
.collect(); | ||
|
||
// If errors have occurred, render them instead. | ||
if !errors.is_empty() { | ||
let compile_errors = errors.iter().map(Error::to_compile_error); | ||
return quote! { #(#compile_errors)* }; | ||
} | ||
|
||
// Render the Client. | ||
let args_ident = format_ident!("{}", name); | ||
quote! { | ||
impl #args_ident { | ||
#(#fns)* | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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