Fmt
This commit is contained in:
parent
5b03bce2e8
commit
549aa7cc72
|
@ -126,11 +126,14 @@ pub fn generate_guards(
|
||||||
Meta::List(args) => {
|
Meta::List(args) => {
|
||||||
println!("args = {:#?}\n", args.path);
|
println!("args = {:#?}\n", args.path);
|
||||||
match args.path.get_ident() {
|
match args.path.get_ident() {
|
||||||
Some(ident) => {
|
Some(ident) => match ident.to_string().as_str() {
|
||||||
match ident.to_string().as_str() {
|
|
||||||
"guard" => {
|
"guard" => {
|
||||||
if args.nested.len() != 1 {
|
if args.nested.len() != 1 {
|
||||||
return Err(Error::new_spanned(args, "Chained rules isn't possible anymore, please use operators.").into());
|
return Err(Error::new_spanned(
|
||||||
|
args,
|
||||||
|
"Chained rules isn't possible anymore, please use operators.",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
if let NestedMeta::Meta(rule) = &args.nested[0] {
|
if let NestedMeta::Meta(rule) = &args.nested[0] {
|
||||||
return generate_guards(crate_name, rule);
|
return generate_guards(crate_name, rule);
|
||||||
|
@ -140,7 +143,11 @@ pub fn generate_guards(
|
||||||
}
|
}
|
||||||
"and" => {
|
"and" => {
|
||||||
if args.nested.len() != 2 {
|
if args.nested.len() != 2 {
|
||||||
return Err(Error::new_spanned(args, "and operator support only 2 operands.").into());
|
return Err(Error::new_spanned(
|
||||||
|
args,
|
||||||
|
"and operator support only 2 operands.",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
let first_rule: Option<TokenStream>;
|
let first_rule: Option<TokenStream>;
|
||||||
let second_rule: Option<TokenStream>;
|
let second_rule: Option<TokenStream>;
|
||||||
|
@ -154,11 +161,17 @@ pub fn generate_guards(
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new_spanned(&args.nested[1], "Invalid rule.").into());
|
return Err(Error::new_spanned(&args.nested[1], "Invalid rule.").into());
|
||||||
}
|
}
|
||||||
Ok(Some(quote! { #crate_name::guard::GuardExt::and(#first_rule, #second_rule) }))
|
Ok(Some(
|
||||||
|
quote! { #crate_name::guard::GuardExt::and(#first_rule, #second_rule) },
|
||||||
|
))
|
||||||
}
|
}
|
||||||
"or" => {
|
"or" => {
|
||||||
if args.nested.len() != 2 {
|
if args.nested.len() != 2 {
|
||||||
return Err(Error::new_spanned(args, "or operator support only 2 operands.").into());
|
return Err(Error::new_spanned(
|
||||||
|
args,
|
||||||
|
"or operator support only 2 operands.",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
let first_rule: Option<TokenStream>;
|
let first_rule: Option<TokenStream>;
|
||||||
let second_rule: Option<TokenStream>;
|
let second_rule: Option<TokenStream>;
|
||||||
|
@ -172,7 +185,9 @@ pub fn generate_guards(
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new_spanned(&args.nested[1], "Invalid rule.").into());
|
return Err(Error::new_spanned(&args.nested[1], "Invalid rule.").into());
|
||||||
}
|
}
|
||||||
Ok(Some(quote! { #crate_name::guard::GuardExt::or(#first_rule, #second_rule) }))
|
Ok(Some(
|
||||||
|
quote! { #crate_name::guard::GuardExt::or(#first_rule, #second_rule) },
|
||||||
|
))
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let ty = &args.path;
|
let ty = &args.path;
|
||||||
|
@ -204,11 +219,8 @@ pub fn generate_guards(
|
||||||
}
|
}
|
||||||
Ok(Some(quote! { #ty { #(#params),* } }))
|
Ok(Some(quote! { #ty { #(#params),* } }))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
None => {
|
None => Err(Error::new_spanned(args, "Invalid guards").into()),
|
||||||
Err(Error::new_spanned(args, "Invalid guards").into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(Error::new_spanned(args, "Invalid guards").into()),
|
_ => Err(Error::new_spanned(args, "Invalid guards").into()),
|
||||||
|
|
|
@ -42,7 +42,6 @@ impl Guard for UserGuard {
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
pub async fn test_guard_simple_rule() {
|
pub async fn test_guard_simple_rule() {
|
||||||
|
|
||||||
#[derive(SimpleObject)]
|
#[derive(SimpleObject)]
|
||||||
struct Query {
|
struct Query {
|
||||||
#[graphql(guard(RoleGuard(role = "Role::Admin")))]
|
#[graphql(guard(RoleGuard(role = "Role::Admin")))]
|
||||||
|
@ -54,10 +53,7 @@ pub async fn test_guard_simple_rule() {
|
||||||
let query = "{ value }";
|
let query = "{ value }";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema
|
schema
|
||||||
.execute(
|
.execute(Request::new(query).data(Role::Admin))
|
||||||
Request::new(query)
|
|
||||||
.data(Role::Admin)
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.data,
|
.data,
|
||||||
serde_json::json!({"value": 10})
|
serde_json::json!({"value": 10})
|
||||||
|
@ -66,10 +62,7 @@ pub async fn test_guard_simple_rule() {
|
||||||
let query = "{ value }";
|
let query = "{ value }";
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
schema
|
schema
|
||||||
.execute(
|
.execute(Request::new(query).data(Role::Guest))
|
||||||
Request::new(query)
|
|
||||||
.data(Role::Guest)
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.into_result()
|
.into_result()
|
||||||
.unwrap_err(),
|
.unwrap_err(),
|
||||||
|
@ -84,10 +77,12 @@ pub async fn test_guard_simple_rule() {
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
pub async fn test_guard_and_operator() {
|
pub async fn test_guard_and_operator() {
|
||||||
|
|
||||||
#[derive(SimpleObject)]
|
#[derive(SimpleObject)]
|
||||||
struct Query {
|
struct Query {
|
||||||
#[graphql(guard(and(RoleGuard(role = "Role::Admin"), UserGuard(username = r#""test""#))))]
|
#[graphql(guard(and(
|
||||||
|
RoleGuard(role = "Role::Admin"),
|
||||||
|
UserGuard(username = r#""test""#)
|
||||||
|
)))]
|
||||||
value: i32,
|
value: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,7 +161,6 @@ pub async fn test_guard_and_operator() {
|
||||||
|
|
||||||
#[async_std::test]
|
#[async_std::test]
|
||||||
pub async fn test_guard_or_operator() {
|
pub async fn test_guard_or_operator() {
|
||||||
|
|
||||||
#[derive(SimpleObject)]
|
#[derive(SimpleObject)]
|
||||||
struct Query {
|
struct Query {
|
||||||
#[graphql(guard(or(RoleGuard(role = "Role::Admin"), UserGuard(username = r#""test""#))))]
|
#[graphql(guard(or(RoleGuard(role = "Role::Admin"), UserGuard(username = r#""test""#))))]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user