Avoid bypassing setter checks by making fn.params.skip private

This commit is contained in:
J Henry Waugh 2020-08-27 23:08:34 -05:00
parent fedc4c5338
commit 62dc142c58
3 changed files with 20 additions and 12 deletions

View File

@ -111,7 +111,7 @@ pub(crate) struct ExportedFn {
signature: syn::Signature, signature: syn::Signature,
is_public: bool, is_public: bool,
mut_receiver: bool, mut_receiver: bool,
pub params: ExportedFnParams, params: ExportedFnParams,
} }
impl Parse for ExportedFn { impl Parse for ExportedFn {
@ -218,6 +218,14 @@ impl Parse for ExportedFn {
} }
impl ExportedFn { impl ExportedFn {
pub(crate) fn params(&self) -> &ExportedFnParams {
&self.params
}
pub(crate) fn skipped(&self) -> bool {
self.params.skip
}
pub(crate) fn mutable_receiver(&self) -> bool { pub(crate) fn mutable_receiver(&self) -> bool {
self.mut_receiver self.mut_receiver
} }

View File

@ -112,9 +112,9 @@ impl Parse for Module {
true true
}; };
syn::parse2::<ExportedFn>(itemfn.to_token_stream()) syn::parse2::<ExportedFn>(itemfn.to_token_stream())
.map(|mut f| { .and_then(|mut f| {
f.params = params; f.set_params(params)?;
f Ok(f)
}) })
.map(|f| vec.push(f)) .map(|f| vec.push(f))
.map(|_| vec) .map(|_| vec)
@ -411,7 +411,7 @@ mod module_tests {
assert!(item_mod.consts.is_empty()); assert!(item_mod.consts.is_empty());
assert_eq!(item_mod.submodules.len(), 1); assert_eq!(item_mod.submodules.len(), 1);
assert_eq!(item_mod.submodules[0].fns.len(), 1); assert_eq!(item_mod.submodules[0].fns.len(), 1);
assert!(item_mod.submodules[0].fns[0].params.skip); assert!(item_mod.submodules[0].fns[0].skipped());
assert!(item_mod.submodules[0].consts.is_empty()); assert!(item_mod.submodules[0].consts.is_empty());
assert!(item_mod.submodules[0].submodules.is_empty()); assert!(item_mod.submodules[0].submodules.is_empty());
} }
@ -433,7 +433,7 @@ mod module_tests {
assert!(item_mod.fns.is_empty()); assert!(item_mod.fns.is_empty());
assert!(item_mod.consts.is_empty()); assert!(item_mod.consts.is_empty());
assert_eq!(item_mod.submodules.len(), 1); assert_eq!(item_mod.submodules.len(), 1);
assert!(item_mod.submodules[0].params.skip); assert!(item_mod.submodules[0].skipped());
} }
#[test] #[test]
@ -466,7 +466,7 @@ mod module_tests {
let item_mod = syn::parse2::<Module>(input_tokens).unwrap(); let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
assert_eq!(item_mod.fns.len(), 1); assert_eq!(item_mod.fns.len(), 1);
assert!(item_mod.fns[0].params.skip); assert!(item_mod.fns[0].skipped());
assert!(item_mod.consts.is_empty()); assert!(item_mod.consts.is_empty());
} }
@ -483,7 +483,7 @@ mod module_tests {
let item_mod = syn::parse2::<Module>(input_tokens).unwrap(); let item_mod = syn::parse2::<Module>(input_tokens).unwrap();
assert_eq!(item_mod.fns.len(), 1); assert_eq!(item_mod.fns.len(), 1);
assert!(item_mod.fns[0].params.skip); assert!(item_mod.fns[0].skipped());
assert!(item_mod.consts.is_empty()); assert!(item_mod.consts.is_empty());
} }

View File

@ -56,7 +56,7 @@ pub(crate) fn generate_body(
// NB: these are token streams, because reparsing messes up "> >" vs ">>" // NB: these are token streams, because reparsing messes up "> >" vs ">>"
let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new(); let mut gen_fn_tokens: Vec<proc_macro2::TokenStream> = Vec::new();
for function in fns { for function in fns {
if function.params.skip { if function.skipped() {
continue; continue;
} }
let fn_token_name = syn::Ident::new( let fn_token_name = syn::Ident::new(
@ -64,7 +64,7 @@ pub(crate) fn generate_body(
function.name().span(), function.name().span(),
); );
let reg_name = function let reg_name = function
.params .params()
.name .name
.clone() .clone()
.unwrap_or_else(|| function.name().to_string()); .unwrap_or_else(|| function.name().to_string());
@ -151,8 +151,8 @@ pub(crate) fn check_rename_collisions(fns: &Vec<ExportedFn>) -> Result<(), syn::
let mut renames = HashMap::<String, proc_macro2::Span>::new(); let mut renames = HashMap::<String, proc_macro2::Span>::new();
let mut names = HashMap::<String, proc_macro2::Span>::new(); let mut names = HashMap::<String, proc_macro2::Span>::new();
for itemfn in fns.iter() { for itemfn in fns.iter() {
if let Some(ref name) = itemfn.params.name { if let Some(ref name) = itemfn.params().name {
let current_span = itemfn.params.span.as_ref().unwrap(); let current_span = itemfn.params().span.as_ref().unwrap();
let key = itemfn.arg_list().fold(name.clone(), |mut argstr, fnarg| { let key = itemfn.arg_list().fold(name.clone(), |mut argstr, fnarg| {
let type_string: String = match fnarg { let type_string: String = match fnarg {
syn::FnArg::Receiver(_) => unimplemented!("receiver rhai_fns not implemented"), syn::FnArg::Receiver(_) => unimplemented!("receiver rhai_fns not implemented"),