Skip to content
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

Validate NIF attributes #279

Merged
merged 1 commit into from
Nov 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
),
}
}
}
}
}