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

refactor!: rename type to ty #17

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/enum-role/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rocket_grants::GrantsFairing;
mod role;

// `proc-macro` way require specify your type. It can be an import or a full path.
#[rocket_grants::has_any_role("ADMIN", "role::Role::MANAGER", type = "Role")]
#[rocket_grants::has_any_role("ADMIN", "role::Role::MANAGER", ty = "Role")]
// For the `ADMIN` or `MANAGER` - endpoint will give the HTTP status 200, otherwise - 403
#[rocket::get("/macro_secured")]
async fn macro_secured() -> Status {
Expand Down
20 changes: 10 additions & 10 deletions proc-macro/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl ToTokens for HasPermissions {

let check_fn = &self.check_fn;

let args = if self.args.type_.is_some() {
let args = if self.args.ty.is_some() {
let permissions: Vec<syn::Expr> = self
.args
.permissions
Expand All @@ -67,9 +67,9 @@ impl ToTokens for HasPermissions {
}
};

let type_ = self
let ty = self
.args
.type_
.ty
.as_ref()
.map(|t| t.to_token_stream())
.unwrap_or(quote! {String});
Expand All @@ -83,7 +83,7 @@ impl ToTokens for HasPermissions {
let stream = quote! {
#(#fn_attrs)*
#func_vis #fn_async fn #fn_name #fn_generics(
_auth_details_: rocket_grants::permissions::AuthDetails<#type_>,
_auth_details_: rocket_grants::permissions::AuthDetails<#ty>,
#fn_args
) -> Result<#fn_output, rocket::http::Status> {
use rocket_grants::permissions::{PermissionsCheck, RolesCheck};
Expand All @@ -103,14 +103,14 @@ impl ToTokens for HasPermissions {
struct Args {
permissions: Vec<syn::LitStr>,
secure: Option<syn::Expr>,
type_: Option<syn::Expr>,
ty: Option<syn::Expr>,
}

impl Args {
fn new(args: AttributeArgs) -> syn::Result<Self> {
let mut permissions = Vec::with_capacity(args.len());
let mut secure = None;
let mut type_ = None;
let mut ty = None;
for arg in args {
match arg {
NestedMeta::Lit(syn::Lit::Str(lit)) => {
Expand All @@ -124,13 +124,13 @@ impl Args {
if path.is_ident("secure") {
let expr = lit_str.parse().unwrap();
secure = Some(expr);
} else if path.is_ident("type") {
} else if path.is_ident("ty") {
let expr = lit_str.parse().unwrap();
type_ = Some(expr);
ty = Some(expr);
} else {
return Err(syn::Error::new_spanned(
path,
"Unknown identifier. Available: 'secure' and 'type'",
"Unknown identifier. Available: 'secure' and 'ty'",
));
}
}
Expand All @@ -143,7 +143,7 @@ impl Args {
Ok(Args {
permissions,
secure,
type_,
ty,
})
}
}
4 changes: 2 additions & 2 deletions proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const HAS_ANY_ROLE: &str = "has_any_role";
/// Allow to add a conditional restriction based on handlers parameters.
/// Add the `secure` attribute followed by the the boolean expression to validate based on parameters
///
/// Also you can use you own types instead of Strings, just add `type` attribute with path to type
/// Also you can use you own types instead of Strings, just add `ty` attribute with path to type
/// # Examples
/// ```
/// use rocket::serde::json::Json;
Expand All @@ -44,7 +44,7 @@ const HAS_ANY_ROLE: &str = "has_any_role";
/// }
///
/// // User must have MyPermissionEnum::OpGetSecret (you own enum example)
/// #[rocket_grants::has_permissions("MyPermissionEnum::OpGetSecret", type = "MyPermissionEnum")]
/// #[rocket_grants::has_permissions("MyPermissionEnum::OpGetSecret", ty = "MyPermissionEnum")]
/// async fn macro_enum_secured() -> &'static str {
/// "some secured info"
/// }
Expand Down
4 changes: 2 additions & 2 deletions rocket-grants/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ async fn macro_secured() -> &'static str {
<br/>


Here is an example using the `type` and `secure` attributes. But these are independent features.
Here is an example using the `ty` and `secure` attributes. But these are independent features.

`secure` allows you to include some checks in the macro based on function params.

`type` allows you to use a custom type for the roles and permissions (then the fairing needs to be configured).
`ty` allows you to use a custom type for the roles and permissions (then the fairing needs to be configured).
Take a look at an [enum-role example](../examples/enum-role/src/main.rs)

```rust,ignore
Expand Down
2 changes: 1 addition & 1 deletion rocket-grants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub use fairing::GrantsFairing;
/// struct User { id: i32 }
///
/// // You own type is also supported (need to configure fairing for this type as well):
/// #[rocket_grants::has_roles["Role::Admin", "Role::Manager", type = "Role"]]
/// #[rocket_grants::has_roles["Role::Admin", "Role::Manager", ty = "Role"]]
/// #[rocket::get("/enum")]
/// async fn role_enum_macro_secured() -> &'static str {
/// "some secured info"
Expand Down
4 changes: 2 additions & 2 deletions rocket-grants/tests/proc_macro/type_feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use rocket::local::asynchronous::{Client, LocalResponse};
use rocket_grants::{has_roles, GrantsFairing};

// Using imported custom type (in `use` section)
#[has_roles("ADMIN", type = "Role")]
#[has_roles("ADMIN", ty = "Role")]
#[rocket::get("/imported_enum_secure")]
async fn imported_path_enum_secure() -> Status {
Status::Ok
}

// Using a full path to a custom type (enum)
#[has_roles("crate::common::Role::ADMIN", type = "crate::common::Role")]
#[has_roles("crate::common::Role::ADMIN", ty = "crate::common::Role")]
#[rocket::get("/full_path_enum_secure")]
async fn full_path_enum_secure() -> Status {
Status::Ok
Expand Down
Loading