#parser #validation #numbers

no-std cpf

Brazilian CPF parsing, validating and formatting library

11 releases

0.3.2 Aug 26, 2023
0.3.1 Aug 26, 2023
0.2.2 Jun 12, 2021
0.2.1 May 22, 2021
0.0.1 Apr 12, 2021

#97 in No standard library

Download history 101/week @ 2024-06-14 111/week @ 2024-06-21 71/week @ 2024-06-28 180/week @ 2024-07-05 60/week @ 2024-07-12 32/week @ 2024-07-19 211/week @ 2024-07-26 145/week @ 2024-08-02 116/week @ 2024-08-09 39/week @ 2024-08-16 95/week @ 2024-08-23 305/week @ 2024-08-30 137/week @ 2024-09-06 140/week @ 2024-09-13 186/week @ 2024-09-20 179/week @ 2024-09-27

672 downloads per month

MIT license

20KB
387 lines

cpf-rs

Brazilian CPF parsing, validating and formatting library.

use cpf::Cpf;

// Use the `valid` function if all you need is validating a CPF number
assert!(cpf::valid("385.211.390-39"));
assert!(cpf::valid("38521139039"));
assert!(!cpf::valid("000.000.000-00"));

// Parse into a Cpf struct if you need formatting or other metadata
let cpf: Cpf = "38521139039".parse()?;
assert_eq!(format!("{cpf}"), "385.211.390-39");
assert_eq!(cpf.as_str(), "38521139039");
assert_eq!(cpf.digits(), [3, 8, 5, 2, 1, 1, 3, 9, 0, 3, 9]);

// Note that the Cpf struct is guaranteed to always be valid
assert!("000.000.000-00".parse::<Cpf>().is_err());
assert!(cpf::valid("385.211.390-39".parse::<Cpf>()?));

no_std support

The library makes no dinamic allocation and can be used on no_std environments by disabling the std flag:

[dependencies]
cpf = { version = "0.3", default-features = false }

Random CPF generation support

The rand feature flag enables random CPF generation:

[dependencies]
cpf = { version = "0.3", features = ["rand"] }
rand = "0.8"
use cpf::Cpf;
use rand;
use rand::Rng;

let cpf = rand::thread_rng().gen::<Cpf>();

Dependencies

~240KB