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,
is_public: bool,
mut_receiver: bool,
pub params: ExportedFnParams,
params: ExportedFnParams,
}
impl Parse for ExportedFn {
@ -218,6 +218,14 @@ impl Parse for 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 {
self.mut_receiver
}

View File

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

View File

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