Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zignis/bit-roles
🔐 Granular role and permission management based on bit flags
https://github.com/zignis/bit-roles
macros-rust permission-management role-management
Last synced: 27 days ago
JSON representation
🔐 Granular role and permission management based on bit flags
- Host: GitHub
- URL: https://github.com/zignis/bit-roles
- Owner: zignis
- Created: 2024-04-13T18:04:37.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-08T14:06:34.000Z (5 months ago)
- Last Synced: 2024-09-17T23:57:03.515Z (about 2 months ago)
- Topics: macros-rust, permission-management, role-management
- Language: Rust
- Homepage: https://docs.rs/bit_roles
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bit Roles
[![Latest Version](https://img.shields.io/crates/v/bit_roles.svg)](https://crates.io/crates/bit_roles)
[![Rust Documentation](https://docs.rs/bit_roles/badge.svg)](https://docs.rs/bit_roles)This crate enables you to implement granular role and permission management based on bit flags.
Using discrete fields
Using bit roles
```rust
#[derive(Debug)]
struct User {
can_send_message: bool,
can_edit_message: bool
}fn main() {
let user = User {
can_send_message: true,
can_edit_message: false
};
}
``````rust
use bit_roles::BitRole;#[derive(Debug)]
struct User {
permissions: usize
}#[derive(Debug, BitRole, Copy, Clone)]
enum Permission {
None = 0,
SendMessage = 1,
EditMessage = 2,
}fn main() {
let mut permissions = Permission::empty();
permissions.add_one(Permission::SendMessage);let user = User {
permissions: permissions.get_value()
};
}
```## Getting started
Add `bit_roles` to your project:
```shell
cargo add bit_roles
```## Usage
You can derive the `BitRole` trait for your role enum. It ensures compile-time validation for enum discriminants. Ensure
you specify a discriminant for every enum variant; it must be either zero or a power of two. Also, remember to derive
the `Copy` and `Clone` traits for your enum.If you need a manager without compile-time checks, it's also exported as `BitRoleUnchecked` trait. This is useful if you
want to use raw integer values for roles or have a complex role enum definition. You will need to implement
the `Into` trait for your role enum, along with deriving the `Copy` and `Clone` traits for it.