brids/lib.rs
1// lib.rs
2//
3// Copyright 2018 Ricardo Silva Veloso <ricvelozo@gmail.com>
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// https://www.apache.org/licenses/LICENSE-2.0> or the MIT License
7// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10//
11// SPDX-License-Identifier: (MIT OR Apache-2.0)
12
13//! Parse and generate random CPF and CNPJ, Brazil's ID numbers.
14//!
15//! # Usage
16//!
17//! Add the following to your `Cargo.toml`:
18//!
19//! ```toml
20//! [dependencies]
21//! brids = "0.5"
22//! ```
23//!
24//! # Features
25//!
26//! All dependencies are optional and _disabled by default_:
27//!
28//! * [`rand`] - enable to generate random numbers
29//! * [`serde`] - enable to (de)serialize numbers
30//!
31//! [`rand`]: https://crates.io/crates/rand
32//! [`serde`]: https://crates.io/crates/serde
33//!
34//! ## `no_std` mode
35//!
36//! To enable `no_std` mode, just disable the default features:
37//!
38//! ```toml
39//! [dependencies]
40//! brids = { version = "0.5", default-features = false }
41//! ```
42//!
43//! # Examples
44//!
45//! Parse and format:
46//!
47//! ```rust
48//! use brids::Cpf;
49//!
50//! let maybe_valid = "123.456.789-09".parse::<Cpf>();
51//! assert!(maybe_valid.is_ok()); // Checks validity
52//!
53//! let old_format = "123.456.789/09".parse::<Cpf>();
54//! assert!(old_format.is_ok()); // Accepts the old format too
55//!
56//! let unformatted = "12345678909".parse::<Cpf>().expect("Invalid CPF");
57//! let formatted = unformatted.to_string(); // Formats
58//! println!("CPF: {unformatted}"); // Formats too
59//! ```
60//!
61//! Generate random CNPJ and CPF numbers (you must enable the [`rand` feature](#features)):
62//!
63//! ```rust, ignore
64//! use brids::{Cnpj, Cpf};
65//!
66//! println!("Random CNPJ number: {}", Cnpj::generate());
67//! println!("Random CPF number: {}", Cpf::generate());
68//! ```
69//!
70//! If you are using the `no_std` mode, the `::generate()` methods are unavailable; instantiate the
71//! generator directly instead:
72//!
73//! ```rust, ignore
74//! use brids::{Cnpj, Cpf};
75//! use rand::{rngs::StdRng, Rng, SeedableRng};
76//!
77//! let mut rng = StdRng::seed_from_u64(123); // Available in `no_std` mode
78//! println!("Random CNPJ number: {}", rng.gen::<Cnpj>());
79//! println!("Random CPF number: {}", rng.gen::<Cpf>());
80//! ```
81//!
82//! Serialize and deserialize (you must enable the [`serde` feature](#features)):
83//!
84//! ```rust, ignore
85//! use brids::Cnpj;
86//! use serde::{Deserialize, Serialize};
87//! use serde_json;
88//!
89//! #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
90//! struct Company<'a> {
91//! name: &'a str,
92//! cnpj: Cnpj,
93//! }
94//!
95//! let company1 = Company {
96//! name: "Banco do Brasil S/A",
97//! cnpj: "00.000.000/0001-91".parse().expect("Invalid CNPJ"),
98//! };
99//!
100//! // Serializes the struct into JSON
101//! let json = serde_json::to_string(&company1).expect("Failed to serialize");
102//! println!("{json}");
103//!
104//! // Deserializes the struct back
105//! let company2: Company = serde_json::from_str(&json).expect("Failed to deserialize");
106//! assert_eq!(company1, company2);
107//! ```
108
109#![cfg_attr(not(feature = "std"), no_std)]
110
111#[cfg(not(feature = "std"))]
112extern crate alloc;
113
114mod cnpj;
115mod cpf;
116
117pub use cnpj::*;
118pub use cpf::*;