1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//     validbr - Brazilian registry validator, provides structures for representing CPF, CNPJ, RG, CNH, CEP and Credit Card Number!
//
//         The MIT License (MIT)
//
//      Copyright (c) Obliter Software (https://github.com/oblitersoftware/)
//      Copyright (c) contributors
//
//      Permission is hereby granted, free of charge, to any person obtaining a copy
//      of this software and associated documentation files (the "Software"), to deal
//      in the Software without restriction, including without limitation the rights
//      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//      copies of the Software, and to permit persons to whom the Software is
//      furnished to do so, subject to the following conditions:
//
//      The above copyright notice and this permission notice shall be included in
//      all copies or substantial portions of the Software.
//
//      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//      THE SOFTWARE.

//! Representation of Brazilian registries: CPF, CNPJ, RG, CNH, CEP and Credit Card Number.
//!
//! validbr also provides validation for CPJ, CNPJ, CNH and Credit Card Number and a builtin database
//! of brazilian CEP, Cities and States.
//!
//! # Cpf
//!
//! Consist in 9 digits separated in partitions of 3 with `.` and two verifier digits separated by a `-` prefix,
//! for example: `123.456.789-09`.
//!
//! ## Example of usage of CPF struct
//!
//! ```
//! use validbr::Cpf;
//! let cpf = Cpf::parse_str("123.456.789-09");
//! assert_eq!(cpf, Ok(Cpf { digits: [1, 2, 3, 4, 5, 6, 7, 8, 9], verifier_digits: [0, 9]}));
//! ```
//!
//! ## Supported formats
//!
//! [`Cpf::parse_str`] only supports following formats:
//! - `###.###.###-##` (Commonly represented CPF)
//! - `###########` (Only digits CPF).
//!
//! # CNPJ
//!
//! Consists in eight numbers separated by a `.` in partitions for 3 (except for the first two digits
//! which are separated from the last two groups), a company branch number digit composed of four digits,
//! separated by a prefix `/` and two last verifier digits prefixed with `-`, example: `12.345.678/0001-95`.
//!
//! ## Example of usage of CNPJ struct
//!
//! ```
//! use validbr::Cnpj;
//! let cpf = Cnpj::parse_str("12.345.678/0001-95");
//! assert_eq!(cpf, Ok(Cnpj { digits: [1, 2, 3, 4, 5, 6, 7, 8], branch_digits: [0, 0, 0, 1], verifier_digits: [9, 5]}));
//! ```
//!
//! ## Supported formats
//!
//! [`Cnpj::parse_str`] only supports following formats:
//! - `##.###.###/####-##` (Commonly represented CNPJ)
//! - `##############` (Only digits CNPJ).
//!
//! # Features
//!
//! ## [Serde](https://crates.io/crates/serde) support
//!
//! validbr supports [serde](https://crates.io/crates/serde) serialization, which must be enabled with feature flag, for example:
//!
//! ```toml
//! [dependencies]
//! validbr = { version = "0.2", features = ["serde"] }
//! ```
//!
//! ## [rand](https://crates.io/crates/rand) support
//!
//! validbr also supports randomly generated CPF and CNPJ through [rand](https://crates.io/crates/serde) crate,
//! which must be enabled with feature flag, for example:
//!
//! ```toml
//! [dependencies]
//! validbr = { version = "0.2", features = ["rand"] }
//! ```
//!
//! ## Enable all
//!
//! You could enable all features using `complete` flag:
//! ```toml
//! [dependencies]
//! validbr = { version = "0.2", features = ["complete"] }
//! ```
#![feature(doc_cfg)]
#![feature(const_evaluatable_checked, const_generics, const_panic)]
#![allow(incomplete_features)]

#[macro_use]
extern crate lazy_static;

use regex::Regex;

#[macro_use] pub(crate) mod macros;

/// Array append utilities.
pub mod append;
/// Cnpj utility functions
pub mod cnpj;
/// Cpf utility functions
pub mod cpf;
/// RG utility functions
pub mod rg;

#[cfg(feature = "serde")]
use {
    serde::Serialize,
    serde::Deserialize
};
use crate::EmitterOrg::SSP;
use std::fmt::Formatter;


lazy_static! {
    pub(crate) static ref NOT_NUMBERS: Regex = Regex::new(r"[^0-9]+").unwrap();
    pub(crate) static ref ONLY_NUMBERS: Regex = Regex::new(r"^[0-9]+$").unwrap();
}


/// CPF consists of nine digits and two verifier digits.
///
/// The algorithm to calculate the first verifier digit is:
///
/// ```
/// let digits = [0u16; 9];
/// let checker_digits = [0u16; 2];
///
/// let sum_of_mul = ((digits[8] * 10) + (digits[7] * 9) + (digits[6] * 8) + (digits[5] * 7) + (digits[4] * 6) +
/// (digits[3] * 5) + (digits[2] * 4) + (digits[1] * 3) + (digits[0] * 2)) as u16;
/// let pre_first_digit = ((sum_of_mul * 10) % 11) as u8;
/// let first_digit = if pre_first_digit == 10 {
///     0
/// } else {
///     pre_first_digit
/// };
/// ```
///
/// And the algorithm to calculate the second verifier digit is:
///
/// ```
/// let digits = [0u16; 9];
/// let checker_digits = [0u16; 2];
///
/// let sum_of_mul = ((digits[8] * 11) + (digits[7] * 10) + (digits[6] * 9) + (digits[5] * 8) + (digits[4] * 7) +
/// (digits[3] * 6) + (digits[2] * 5) + (digits[1] * 4) + (digits[0] * 3) + (checker_digits[0] * 2)) as u16;
/// let pre_second_digit = ((sum_of_mul * 10) % 11) as u8;
/// let second_digit = if pre_second_digit == 10 {
///     0
/// } else {
///     pre_second_digit
/// };
/// ```
///
///
/// These numbers could be obtained through `[calculate_verifier_digits]`.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cpf {
    /// First 9 digits of CPF.
    pub digits: [u8; 9],
    /// Last 2 digits of CPF (the verifier digits).
    pub verifier_digits: [u8; 2],
}

/// CNPJ consists of eight based digits, four digits for the branch (the number of the registered
/// company) and two verifier digits.
///
/// The algorithm to calculate the first verifier digit is:
///
/// ```
/// let digits = [0u16; 8];
/// let branch_num_digits = [0u16; 4];
/// let checker_digits = [0u16; 2];
///
/// let sum_of_mul = ((digits[0] * 5) + (digits[1] * 4) + (digits[2] * 3) + (digits[3] * 2) + (digits[4] * 9) +
/// (digits[5] * 8) + (digits[6] * 7) + (digits[7] * 6) + (branch_num_digits[0] * 5) + (branch_num_digits[1] * 4) +
/// (branch_num_digits[2] * 3) + (branch_num_digits[3] * 2)) as u16;
/// let pre_first_digit = (sum_of_mul % 11) as u8;
/// let first_digit = if pre_first_digit  < 2 {
///     0
/// } else {
///     11 - pre_first_digit
/// };
///
/// // And the algorithm to calculate the second verifier digit is:
///
/// let sum_of_mul_2 = ((digits[0] * 5) + (digits[1] * 4) + (digits[2] * 3) + (digits[3] * 2) + (digits[4] * 9) +
/// (digits[5] * 8) + (digits[6] * 7) + (digits[7] * 6) + (branch_num_digits[0] * 5) + (branch_num_digits[1] * 4) +
/// (branch_num_digits[2] * 3) + (branch_num_digits[3] * 2)) as u16;
/// let pre_second_digit = (sum_of_mul_2 % 11) as u8;
/// let second_digit = if pre_second_digit  < 2 {
///     0
/// } else {
///     11 - pre_second_digit
/// };
/// ```
///
///
/// These numbers could be obtained through `[calculate_verifier_digits]`.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cnpj {
    /// First 8 digits of Cnpj.
    pub digits: [u8; 8],
    /// Four digits of branch.
    pub branch_digits: [u8; 4],
    /// Last 2 digits of CPF (the verifier digits).
    pub verifier_digits: [u8; 2],
}

/// RG does not have a standard for its format,
/// so validbr uses [`String`] representing the RG code.
/// Some emitters uses modulo 11 validation, others don't, some of them uses number only
/// with verifier digit, others don't and includes Letters and special chars.
///
/// See [`Rg::new`] for examples of Rg construction.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Rg {
    /// RG code/Number.
    pub code: String,
    // RG emitter organization.
    pub emitter_org: EmitterOrg
}


/// List of governmental organizations which emits Brazilian Registries.
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum EmitterOrg {
    SSP(UF),
    PoliciaFedaral,
    CartorioCivil,
    Other(String)
}

impl std::fmt::Display for EmitterOrg {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        return match self {
            SSP(uf) => {
                write!(f, "SSP{}", uf)
            },
            EmitterOrg::PoliciaFedaral => {
                write!(f, "Polícia Federal")
            },
            EmitterOrg::CartorioCivil => {
                write!(f, "Cartório Civil")
            }
            EmitterOrg::Other(other) => {
                write!(f, "{}", other)
            }
        }
    }
}

/// List of Brazilian Federative Units (Unidades Federativas).
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum UF {
    AC,
    AL,
    AP,
    AM,
    BA,
    CE,
    DF,
    ES,
    GO,
    MA,
    MT,
    MS,
    MG,
    PA,
    PB,
    PR,
    PE,
    PI,
    RJ,
    RN,
    RS,
    RO,
    RR,
    SC,
    SP,
    SE,
    TO
}

impl std::fmt::Display for UF {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[cfg(test)]
mod tests {
    use crate::EmitterOrg::SSP;
    use crate::UF::SP;

    #[cfg(feature = "rand")]
    #[test]
    fn random_cpf() {
        use rand::Rng;
        use crate::Cpf;

        let mut rng = rand::thread_rng();
        let cpf: Cpf = rng.gen();
        let verifier = crate::cpf::calculate_verifier_digits(cpf.digits);
        assert_eq!(verifier.0, cpf.verifier_digits[0]);
        assert_eq!(verifier.1, cpf.verifier_digits[1]);
    }

    #[cfg(feature = "rand")]
    #[test]
    fn random_cnpj() {
        use rand::Rng;
        use crate::Cnpj;

        let mut rng = rand::thread_rng();
        let cnpj: Cnpj = rng.gen();
        let verifier = crate::cnpj::calculate_verifier_digits(cnpj.digits, cnpj.branch_digits);
        assert_eq!(verifier.0, cnpj.verifier_digits[0]);
        assert_eq!(verifier.1, cnpj.verifier_digits[1]);
    }

    #[cfg(feature = "rand")]
    #[test]
    fn random_cnpj_with_specific_branch() {
        use rand::Rng;
        use crate::Cnpj;
        use crate::cnpj::Branch;

        let mut rng = rand::thread_rng();
        let branch = Branch::from_u8(0012).unwrap();
        let cnpj: Cnpj = rng.sample(branch);

        assert_eq!([0, 0, 1, 2], cnpj.branch_digits);

        let verifier = crate::cnpj::calculate_verifier_digits(cnpj.digits, cnpj.branch_digits);
        assert_eq!(verifier.0, cnpj.verifier_digits[0]);
        assert_eq!(verifier.1, cnpj.verifier_digits[1]);
    }

    #[test]
    fn rg() {
        use crate::Rg;

        let rg = Rg::new("A8974B-X", SSP(SP));

        assert_eq!(format!("{}", rg), "A8974B-X SSPSP");
    }
}