GithubHelp home page GithubHelp logo

zignis / bit-roles Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 18 KB

๐Ÿ” Granular role and permission management based on bit flags

Home Page: https://docs.rs/bit_roles

Rust 100.00%
macros-rust permission-management role-management

bit-roles's Introduction

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.

bit-roles's People

Contributors

zignis avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.