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
//! # CNPJ util
//!
//! `cnpj util` is a library focused on solving a common problems
//! that we face daily in the development of applications using 
//! CNPJ (Brazil companies ID number).

use std::cmp;
use std::cmp::Ordering;

const CNPJ_LENGTH: usize = 14;

fn get_separator(x: usize) -> &'static str {
    match x {
        2 | 5 => ".",
        8 => "/",
        12 => "-",
        _ => ""
    }
}

/// Format string with CNPJ mask.
///
/// # Examples
///
/// ```rust
/// let cnpj_without_mask = "46843485000186";
/// let cnpj_with_mask = cnpj_util::format(cnpj_without_mask);
///
/// assert_eq!("46.843.485/0001-86", cnpj_with_mask);
/// ```
///
/// ```rust
/// let cnpj_without_mask = "468434850001860000000000";
/// let cnpj_with_mask = cnpj_util::format(cnpj_without_mask);
///
/// assert_eq!("46.843.485/0001-86", cnpj_with_mask);
/// ```
///
/// ```rust
/// let cnpj_without_mask = "46.?ABC843.485/0001-86abc";
/// let cnpj_with_mask = cnpj_util::format(cnpj_without_mask);
///
/// assert_eq!("46.843.485/0001-86", cnpj_with_mask);
/// ```
pub fn format(cnpj: &str) -> String {
    let cnpj = cnpj.matches(char::is_numeric).collect::<Vec<_>>();

    let mut cnpj_with_mask: String = String::from("");

    for x in 0..cmp::min(cnpj.len(), 14) {
        cnpj_with_mask.push_str(get_separator(x));
        cnpj_with_mask.push_str(cnpj[x]);
    }

    cnpj_with_mask
}

pub fn reserved_numbers() -> Vec<String> {
    vec![
        String::from("00000000000000"),
        String::from("11111111111111"),
        String::from("22222222222222"),
        String::from("33333333333333"),
        String::from("44444444444444"),
        String::from("55555555555555"),
        String::from("66666666666666"),
        String::from("77777777777777"),
        String::from("88888888888888"),
        String::from("99999999999999"),
    ]
}


fn check_sum(cnpj: &Vec<&str>, factors: Vec<u32>) -> u32 {
    let mut sum: u32 = 0;
    for x in 0..factors.len() {
        sum = sum + cnpj[x].parse::<u32>().unwrap() * factors[x];
    }

    let mod_11 = sum % 11;
    
    match mod_11.cmp(&2) {
        Ordering::Less => 0,
        _ => 11 - mod_11
    }
}

fn validate(cnpj: String) -> bool {
    let cnpj = cnpj.matches(char::is_numeric).collect::<Vec<_>>();
    
    let factors = vec![5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
    let digito_1 = check_sum(&cnpj, factors);
    
    let factors = vec![6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
    let digito_2 = check_sum(&cnpj, factors);

    digito_1 == cnpj[CNPJ_LENGTH - 2].parse::<u32>().unwrap()
        && digito_2 == cnpj[CNPJ_LENGTH - 1].parse::<u32>().unwrap()
}


/// Check if CNPJ is valid.
///
/// # Examples
///
/// ```rust
/// use cnpj_util as cnpj;
/// assert_eq!(false, cnpj::is_valid("12312312312"));
/// assert_eq!(false, cnpj::is_valid("6ad0.t391.9asd47/0ad001-00"));
/// assert_eq!(true, cnpj::is_valid("13723705000189"));
/// assert_eq!(true, cnpj::is_valid("60.391.947/0001-00"));
/// ```
pub fn is_valid(cnpj: &str) -> bool {
    if cnpj.matches(char::is_lowercase).count() > 0 
        || cnpj.matches(char::is_uppercase).count() > 0{
        return false;
    }
    
    let cnpj = cnpj.matches(char::is_numeric).collect::<Vec<_>>().concat();


    cnpj.len() == CNPJ_LENGTH
        && !reserved_numbers().contains(&cnpj)
        && !cnpj.is_empty()
        && validate(cnpj)
}

#[cfg(test)]
mod test_is_valid {
    use super::*;

    #[test]
    fn should_return_false_when_it_is_on_reserved_numbers() {
        for reserved_number in reserved_numbers() {
            assert_eq!(is_valid(&reserved_number), false);
        }
    }

    #[test]
    fn should_return_false_when_is_a_empty_string() {
        assert_eq!(is_valid(""), false);
    }

    #[test]
    fn should_return_false_when_dont_match_with_cnpj_length() {
        assert_eq!(is_valid("12312312312"), false);
    }

    #[test]
    fn should_return_false_when_contains_only_letters_or_special_characters() {
        assert_eq!(is_valid("ababcabcabcdab"), false);
    }

    #[test]
    fn should_return_false_when_is_a_cnpj_invalid_test_numbers_with_letters() {
        assert_eq!(is_valid("6ad0.t391.9asd47/0ad001-00"), false);
    }

    #[test]
    fn should_return_false_when_is_a_cnpj_invalid() {
        assert_eq!(is_valid("11257245286531"), false);
    }

    #[test]
    fn should_return_true_when_is_a_valid_cnpj_without_mask() {
        assert_eq!(is_valid("13723705000189"), true);
    }

    #[test]
    fn should_return_true_when_is_a_cnpj_valid_with_mask() {
        assert_eq!(is_valid("60.391.947/0001-00"), true);
    }
}

#[cfg(test)]
mod test_format {
    use super::*;

    #[test]
    fn should_format_cnpj_with_mask() {
        assert_eq!(format(""), "");
        assert_eq!(format("4"), "4");
        assert_eq!(format("46"), "46");
        assert_eq!(format("468"), "46.8");
        assert_eq!(format("4684"), "46.84");
        assert_eq!(format("46843"), "46.843");
        assert_eq!(format("468434"), "46.843.4");
        assert_eq!(format("4684348"), "46.843.48");
        assert_eq!(format("46843485"), "46.843.485");
        assert_eq!(format("468434850"), "46.843.485/0");
        assert_eq!(format("4684348500"), "46.843.485/00");
        assert_eq!(format("46843485000"), "46.843.485/000");
        assert_eq!(format("468434850001"), "46.843.485/0001");
        assert_eq!(format("4684348500018"), "46.843.485/0001-8");
        assert_eq!(format("46843485000186"), "46.843.485/0001-86");
    }

    #[test]
    fn should_not_add_digits_after_the_cnpj_length() {
        assert_eq!(format("468434850001860000000000"), "46.843.485/0001-86");
    }

    #[test]
    fn should_remove_all_non_numeric_characters() {
        assert_eq!(format("46.?ABC843.485/0001-86abc"), "46.843.485/0001-86");
    }
}