Role

Struct Role 

Source
pub struct Role { /* private fields */ }
Expand description

A role represents a collection of permissions that can be assigned to subjects.

Implementations§

Source§

impl Role

Source

pub fn new(name: impl Into<String>) -> Role

Create a new role with the given name.

Source

pub fn with_id(id: impl Into<String>, name: impl Into<String>) -> Role

Create a new role with a specific ID.

Source

pub fn id(&self) -> &str

Get the role’s unique identifier.

Source

pub fn name(&self) -> &str

Get the role’s name.

Source

pub fn with_description(self, description: impl Into<String>) -> Role

Set the role’s description.

Source

pub fn description(&self) -> Option<&str>

Get the role’s description.

Source

pub fn add_permission(self, permission: Permission) -> Role

Add a permission to this role.

Source

pub fn add_permissions( self, permissions: impl IntoIterator<Item = Permission>, ) -> Role

Add multiple permissions to this role.

Source

pub fn remove_permission(&mut self, permission: &Permission)

Remove a permission from this role.

Source

pub fn has_permission_exact(&self, permission: &Permission) -> bool

Check if this role has a specific permission.

Source

pub fn has_permission( &self, action: &str, resource_type: &str, context: &HashMap<String, String>, ) -> bool

Check if this role grants permission for an action on a resource type.

Source

pub fn permissions(&self) -> &PermissionSet

Get all permissions granted by this role.

Source

pub fn with_metadata( self, key: impl Into<String>, value: impl Into<String>, ) -> Role

Set metadata for this role.

Source

pub fn metadata(&self, key: &str) -> Option<&str>

Get metadata value for a key.

Source

pub fn all_metadata(&self) -> &HashMap<String, String>

Get all metadata.

Source

pub fn set_active(&mut self, active: bool)

Set whether this role is active.

Source

pub fn is_active(&self) -> bool

Check if this role is active.

Source

pub fn deactivate(self) -> Role

Deactivate this role.

Source

pub fn merge_permissions(&mut self, other: &Role)

Merge permissions from another role into this one.

Source

pub fn parent_role_id(&self) -> Option<&str>

Get the parent role ID if this role has a parent in the hierarchy.

This method provides access to the direct parent relationship, enabling use cases like API responses, admin interfaces, and JWT token generation.

Returns None if:

  • This role has no parent (it’s a root role)
  • Hierarchy access is disabled in configuration
§Example
use role_system::Role;

let role = Role::new("junior_dev");
if let Some(parent_id) = role.parent_role_id() {
    println!("Parent role: {}", parent_id);
}
§Note

This method returns None by default to maintain backward compatibility. The actual parent relationship is managed by the RoleHierarchy system. To use this feature, the role must be created through a system that tracks hierarchy relationships and enables hierarchy access.

Source

pub fn child_role_ids(&self) -> Vec<&str>

Get the child role IDs if this role has children in the hierarchy.

This method provides access to direct child relationships, useful for building admin interfaces, API responses, and permission visualization.

Returns empty vector if:

  • This role has no children
  • Hierarchy access is disabled in configuration
§Example
use role_system::Role;

let role = Role::new("team_lead");
let children = role.child_role_ids();
for child_id in children {
    println!("Child role: {}", child_id);
}
§Note

This method returns an empty vector by default to maintain backward compatibility. The actual child relationships are managed by the RoleHierarchy system. To use this feature, the role must be created through a system that tracks hierarchy relationships and enables hierarchy access.

Source

pub fn is_root_role(&self) -> bool

Check if this role is a root role (has no parent).

A root role is one that sits at the top of a hierarchy branch and doesn’t inherit from any other role.

§Example
use role_system::Role;

let admin_role = Role::new("admin");
if admin_role.is_root_role() {
    println!("This is a top-level role");
}
§Note

Returns true by default since individual Role instances don’t track hierarchy relationships. Use RoleHierarchy or AsyncRoleSystem for actual hierarchy-aware operations.

Source

pub fn is_leaf_role(&self) -> bool

Check if this role is a leaf role (has no children).

A leaf role is one that doesn’t have any roles inheriting from it.

§Example
use role_system::Role;

let intern_role = Role::new("intern");
if intern_role.is_leaf_role() {
    println!("This role has no children");
}
§Note

Returns true by default since individual Role instances don’t track hierarchy relationships. Use RoleHierarchy or AsyncRoleSystem for actual hierarchy-aware operations.

Source

pub fn hierarchy_depth(&self) -> usize

Get the depth of this role in the hierarchy.

Root roles have depth 0, their children have depth 1, etc. This is useful for visualization and API responses.

§Example
use role_system::Role;

let role = Role::new("senior_dev");
let depth = role.hierarchy_depth();
println!("Role depth: {}", depth);
§Note

Returns 0 by default since individual Role instances don’t track hierarchy relationships. Use RoleHierarchy or AsyncRoleSystem for actual hierarchy-aware depth calculation.

Source

pub fn hierarchy_metadata(&self) -> HashMap<String, String>

Get metadata about this role’s position in the hierarchy.

Returns a HashMap containing hierarchy-related metadata such as:

  • “parent_count”: Number of ancestors
  • “child_count”: Number of direct children
  • “descendant_count”: Total number of descendants
  • “depth”: Depth in hierarchy
§Example
use role_system::Role;

let role = Role::new("manager");
let hierarchy_meta = role.hierarchy_metadata();
if let Some(depth) = hierarchy_meta.get("depth") {
    println!("Hierarchy depth: {}", depth);
}
§Note

Returns minimal metadata by default. Use RoleHierarchy or AsyncRoleSystem for complete hierarchy metadata.

Trait Implementations§

Source§

impl Clone for Role

Source§

fn clone(&self) -> Role

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Role

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Role

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Role, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for Role

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Role

§

impl !RefUnwindSafe for Role

§

impl Send for Role

§

impl Sync for Role

§

impl Unpin for Role

§

impl !UnwindSafe for Role

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,