Skip to content

Rust crate to generate 128 bits id structs easily

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

MathieuTricoire/identifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Identifier

doc minimum rustc 1.31.0 version GitHub MathieuTricoire/identifier

Generate 128 bits id structs easily

Installation

[dependencies]
identifier = "0.1"

Version requirement: rustc 1.31+

Simple Example

Go to the generator section to see how to create one

use generators::uuid;
use identifier::{Display, FromStr, Identifier};

#[derive(Identifier, Eq, PartialEq, Display, FromStr)]
#[identifier(with = "uuid")]
pub struct UserId(u128);

fn main() {
    let id = UserId::generate();
    println!("generated user id: {}", id);

    let parsed_id = "5ed9a942-223e-4639-a97c-6b6c41ac48d3".parse::<UserId>();
    assert!(parsed_id.is_ok());
}

Example with a generator requiring params

use generators::random_id;
use identifier::{Display, FromStr, Identifier};

const KIND: u32 = 0x1111_ffff;

#[derive(Identifier, Eq, PartialEq, FromStr, Display)]
#[identifier(with = "random_id", params = "KIND, 0xabcd")]
pub struct PostId(u128);

fn main() {
    let id = PostId::generate();
    println!("generated post id: {}", id);
}

Generator

No params (with the uuid crate)

mod uuid {
    use uuid::Uuid;

    pub fn generate() -> u128 {
        Uuid::new_v4().as_u128()
    }

    pub fn validate(value: u128) -> bool {
        if let Some(Version::Random) = Uuid::from_u128(value).get_version() {
            true
        } else {
            false
        }
    }
}

With params

mod random_id {
    pub fn generate(kind: u32, seed: u16) -> u128 {
        /* generate a random id with the given params ... */
    }

    pub fn validate(value: u128, kind: u32, _: u16) -> bool {
        /* check it's a correct random id ... */
    }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Rust crate to generate 128 bits id structs easily

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages