Crates.io | cnpj |
lib.rs | cnpj |
version | 0.2.2 |
source | src |
created_at | 2021-04-12 02:40:38.182593 |
updated_at | 2021-06-12 14:05:02.132529 |
description | Brazilian CNPJ parsing, validating and formatting library. |
homepage | |
repository | https://github.com/reu/cnpj-rs |
max_upload_size | |
id | 382224 |
size | 15,956 |
Brazilian CNPJ parsing, validating and formatting library.
use cnpj::Cnpj;
// Use the `valid` function if all you need is validating a CNPJ number
assert!(cnpj::valid("96.769.900/0001-77"));
assert!(cnpj::valid("96769900000177"));
assert!(!cnpj::valid("00.000.000/0000-00"));
// Parse into a Cnpj struct if you need formatting or other metadata
let cnpj: Cnpj = "96769900000177".parse()?;
assert_eq!(format!("{}", cnpj), "96.769.900/0001-77");
assert_eq!(cnpj.digits(), [9, 6, 7, 6, 9, 9, 0, 0, 0, 0, 0, 1, 7, 7]);
// Note that the Cnpj struct is guaranteed to always be valid
assert!("00.000.000/0000-00".parse::<Cnpj>().is_err());
assert!(cnpj::valid("96.769.900/0001-77".parse::<Cnpj>()?));
The library makes no dinamic allocation and can be used on no_std
environments by disabling the std
flag:
[dependencies]
cnpj = { version = "0.2", default-features = false }
The rand
feature flag enables random CNPJ generation:
[dependencies]
cnpj = { version = "0.2", features = ["rand"] }
rand = "0.8"
use cnpj::Cnpj;
use rand;
use rand::Rng;
let cnpj: Cnpj = rand::thread_rng().gen();