bit_roles_macros

Crates.iobit_roles_macros
lib.rsbit_roles_macros
version0.2.2
sourcesrc
created_at2024-04-13 18:17:23.782604
updated_at2024-06-08 14:03:55.558231
descriptionGranular role management based on bit flags
homepagehttps://github.com/zignis/bit-roles
repositoryhttps://github.com/zignis/bit-roles
max_upload_size
id1207748
size13,302
Coro (zignis)

documentation

https://docs.rs/bit_roles

README

Bit Roles

Latest Version Rust Documentation

This crate enables you to implement granular role and permission management based on bit flags.

Using discrete fields Using bit roles
#[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
    };
}
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:

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<usize> trait for your role enum, along with deriving the Copy and Clone traits for it.

Commit count: 8

cargo fmt