Skip to content

Commit

Permalink
Validate NIF attributes (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
evnu authored and scrogson committed Nov 12, 2019
1 parent 42eb699 commit 5da9019
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions rustler_codegen/src/nif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub fn transcoder_decorator(args: syn::AttributeArgs, fun: syn::ItemFn) -> Token
let sig = &fun.sig;
let name = &sig.ident;
let inputs = &sig.inputs;

validate_attributes(args.clone());

let flags = schedule_flag(args.to_owned());
let function = fun.to_owned().into_token_stream();
let arity = arity(inputs.clone());
Expand Down Expand Up @@ -203,3 +206,25 @@ fn arity(inputs: Punctuated<syn::FnArg, Comma>) -> u32 {

arity
}

fn validate_attributes(args: syn::AttributeArgs) {
use syn::{Meta, MetaNameValue, NestedMeta};
let known_attrs = ["schedule", "name"];

for arg in args.iter() {
if let NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, .. })) = arg {
if known_attrs.iter().all(|known| !path.is_ident(known)) {
match path.get_ident() {
Some(path) => panic!(
"Unknown attribute '{}'. Allowed attributes: {:?}",
path, known_attrs
),
None => panic!(
"Cannot parse attribute. Allowed attributes: {:?}",
known_attrs
),
}
}
}
}
}

0 comments on commit 5da9019

Please sign in to comment.