Coder Social home page Coder Social logo

trid's Introduction

Crates.io Build & Test

trid - Turkish Citizenship ID Number crate

This is my first ever written Rust code derived from my own TurkishId package for .NET. I'm trying to use existing code as an excuse to learn about Rust. Despite constant tackling with error messages, Rust has been extremely impressive so far. (How cool are doc-tests!?)

Usage

parse

The crate provides TurkishId type that represents a valid Turkish ID number. It can be instantiated from a string using the parse() method of str type. TurkishId type guarantess that it never contains an invalid Turkish ID number, so there's no need to validate a TurkishId once parsed. It can always be passed around safely.

is_valid

You can just verify whether a string contains a valid Turkish ID or not by calling is_valid(value: &str) function.

from_seq

If you want to generate a Turkish ID from scratch, you can use from_seq(seq: u32) function.

Internals

The type occupies 11 bytes in memory and kept as ASCII representation of the number in order to make string display conversions fast. The other alternative would be to have it in a 40-bit number which would complicate the string representation.

Examples

Validate a Turkish citizenship ID number:

fn main() {
    if trid::is_valid("12345678901") {
        // yayyy!
    }
}

Try parsing a string into TurkishId:

use trid::TurkishId;

fn main() {
    let id : TurkishId = "12345678901".parse()?;
}

Generate infinite number of random but valid Turkish IDs:

use rand::Rng;
use trid::TurkishId;

fn main() {
    let mut rng = rand::thread_rng();
    loop {
        let seq: u32 = rng.gen_range(100_000_000..1_000_000_000);
        println!("{}", TurkishId::from_seq(seq).unwrap());
    }
}

License

Apache License Version 2.0, see LICENSE file for details.

trid's People

Contributors

ssg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

trid's Issues

Impl Hash

So, TurkishId can be used in sets, dictionaries, etc.

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.