Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

Commit

Permalink
build(deps): switch to darling & syn2 (#15)
Browse files Browse the repository at this point in the history
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Artem Medvedev <artem.medvedev@workato.com>
  • Loading branch information
dependabot[bot] and DDtKey authored Nov 25, 2023
1 parent 593f11d commit 3f83ced
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 40 deletions.
18 changes: 0 additions & 18 deletions CHANGELOG.md

This file was deleted.

3 changes: 2 additions & 1 deletion proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ edition.workspace = true
proc-macro = true

[dependencies]
darling = "0.20.3"
proc-macro2 = "1.0"
quote = "1"
syn = { version = "1.0", features = ["full", "derive", "extra-traits"] }
syn = { version = "2.0", features = ["full", "derive", "extra-traits"] }

[dev-dependencies]
rocket-grants = { workspace = true }
Expand Down
41 changes: 23 additions & 18 deletions proc-macro/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use darling::ast::NestedMeta;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens};
use syn::{AttributeArgs, ItemFn, NestedMeta, ReturnType};
use syn::{ExprLit, ItemFn, ReturnType};

pub(crate) struct HasPermissions {
check_fn: Ident,
Expand All @@ -9,10 +10,9 @@ pub(crate) struct HasPermissions {
}

impl HasPermissions {
pub fn new(check_fn: &str, args: AttributeArgs, func: ItemFn) -> syn::Result<Self> {
pub fn new(check_fn: &str, args: Args, func: ItemFn) -> syn::Result<Self> {
let check_fn: Ident = syn::parse_str(check_fn)?;

let args = Args::new(args)?;
if args.permissions.is_empty() {
return Err(syn::Error::new(
Span::call_site(),
Expand Down Expand Up @@ -100,25 +100,28 @@ impl ToTokens for HasPermissions {
}
}

struct Args {
#[derive(Default)]
pub(crate) struct Args {
permissions: Vec<syn::LitStr>,
secure: Option<syn::Expr>,
ty: Option<syn::Expr>,
}

impl Args {
fn new(args: AttributeArgs) -> syn::Result<Self> {
let mut permissions = Vec::with_capacity(args.len());
impl darling::FromMeta for Args {
fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {
let mut permissions = Vec::new();
let mut secure = None;
let mut ty = None;
for arg in args {
match arg {
NestedMeta::Lit(syn::Lit::Str(lit)) => {
permissions.push(lit);
}

for item in items {
match item {
NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
path,
lit: syn::Lit::Str(lit_str),
value:
syn::Expr::Lit(ExprLit {
lit: syn::Lit::Str(lit_str),
..
}),
..
})) => {
if path.is_ident("secure") {
Expand All @@ -128,14 +131,16 @@ impl Args {
let expr = lit_str.parse().unwrap();
ty = Some(expr);
} else {
return Err(syn::Error::new_spanned(
path,
"Unknown identifier. Available: 'secure' and 'ty'",
));
return Err(darling::Error::unknown_field_path(path));
}
}
NestedMeta::Lit(syn::Lit::Str(lit)) => {
permissions.push(lit.clone());
}
_ => {
return Err(syn::Error::new_spanned(arg, "Unknown attribute."));
return Err(darling::Error::custom(
"Unknown attribute, available: 'secure', 'ty' & string literal",
))
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
extern crate proc_macro;
use darling::ast::NestedMeta;
use darling::FromMeta;
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{parse_macro_input, AttributeArgs, ItemFn};
use syn::{parse_macro_input, ItemFn};

use crate::expand::HasPermissions;
use crate::expand::{Args, HasPermissions};

mod expand;

Expand Down Expand Up @@ -103,7 +105,19 @@ pub fn has_any_role(args: TokenStream, input: TokenStream) -> TokenStream {
}

fn check_permissions(check_fn_name: &str, args: TokenStream, input: TokenStream) -> TokenStream {
let args = parse_macro_input!(args as AttributeArgs);
let args = match NestedMeta::parse_meta_list(args.into()) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(darling::Error::from(e).write_errors());
}
};
let args = match Args::from_list(&args) {
Ok(v) => v,
Err(e) => {
return TokenStream::from(e.write_errors());
}
};

let func = parse_macro_input!(input as ItemFn);

match HasPermissions::new(check_fn_name, args, func) {
Expand Down

0 comments on commit 3f83ced

Please sign in to comment.