Skip to content

Commit

Permalink
ajustes de código
Browse files Browse the repository at this point in the history
  • Loading branch information
brenomfviana committed Jul 30, 2024
1 parent 2bf772a commit db47733
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 98 deletions.
20 changes: 11 additions & 9 deletions brado/src/certidao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use crate::common::{get_digits, get_symbols, random_digit_vector, to_decimal};

const CERTIDAO_SIZE: usize = 32;

/// Realiza validação de Certidão, máscarada ou não.
/// Retorna `true` se o argumento `doc` for uma Certidão válido, caso contrário,
/// retorna `false`.
Expand Down Expand Up @@ -32,13 +34,13 @@ use crate::common::{get_digits, get_symbols, random_digit_vector, to_decimal};
pub fn validate(doc: &str) -> bool {
let size: usize = doc.chars().count();

if size != 32 && !is_masked(doc) {
if size != CERTIDAO_SIZE && !is_masked(doc) {
return false;
}

let digits: Vec<u16> = get_digits(doc, Box::new(to_decimal));
let digits: Vec<u16> = get_digits(doc, &to_decimal);

if digits.len() != 32 {
if digits.len() != CERTIDAO_SIZE {
return false;
}

Expand Down Expand Up @@ -102,8 +104,8 @@ fn generate_digit(doc_slice: &[u16]) -> u16 {
/// assert!(result);
/// ```
pub fn is_bare(doc: &str) -> bool {
doc.chars().count() == 32
&& get_digits(doc, Box::new(to_decimal)).len() == 32
doc.chars().count() == CERTIDAO_SIZE
&& get_digits(doc, &to_decimal).len() == CERTIDAO_SIZE
}

/// Verifica se o argumento `doc` pode ser uma Certidão com símbolos.
Expand All @@ -130,10 +132,10 @@ pub fn is_bare(doc: &str) -> bool {
/// assert!(result);
/// ```
pub fn is_masked(doc: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(doc, Box::new(to_decimal));
let digits: Vec<u16> = get_digits(doc, Box::new(to_decimal));
let symbols: Vec<(usize, char)> = get_symbols(doc, &to_decimal);
let digits: Vec<u16> = get_digits(doc, &to_decimal);

if symbols.len() != 8 || digits.len() != 32 {
if symbols.len() != 8 || digits.len() != CERTIDAO_SIZE {
return false;
}

Expand Down Expand Up @@ -225,5 +227,5 @@ pub fn generate() -> String {
/// assert!(certidao::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate()).expect("Valid Certidão!")
mask(&generate()).expect("Invalid Certidão!")
}
20 changes: 11 additions & 9 deletions brado/src/cnh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use crate::common::{
get_digits, get_symbols, is_repeated, random_digit_vector, to_decimal,
};

const CNH_SIZE: usize = 11;

/// Realiza validação de CNH, máscarado ou não.
/// Retorna `true` se o argumento `doc` for uma CNH válida, caso contrário,
/// retorna `false`.
Expand Down Expand Up @@ -34,13 +36,13 @@ use crate::common::{
pub fn validate(doc: &str) -> bool {
let size: usize = doc.chars().count();

if size != 11 && !is_masked(doc) {
if size != CNH_SIZE && !is_masked(doc) {
return false;
}

let digits: Vec<u16> = get_digits(doc, Box::new(to_decimal));
let digits: Vec<u16> = get_digits(doc, &to_decimal);

if digits.len() != 11 || is_repeated(&digits) {
if digits.len() != CNH_SIZE || is_repeated(&digits) {
return false;
}

Expand Down Expand Up @@ -120,8 +122,8 @@ fn generate_second_digit(
/// assert!(result);
/// ```
pub fn is_bare(doc: &str) -> bool {
doc.chars().count() == 11
&& get_digits(doc, Box::new(to_decimal)).len() == 11
doc.chars().count() == CNH_SIZE
&& get_digits(doc, &to_decimal).len() == CNH_SIZE
}

/// Verifica se o argumento `doc` pode ser uma CNH com símbolos.
Expand All @@ -148,10 +150,10 @@ pub fn is_bare(doc: &str) -> bool {
/// assert!(result);
/// ```
pub fn is_masked(doc: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(doc, Box::new(to_decimal));
let digits: Vec<u16> = get_digits(doc, Box::new(to_decimal));
let symbols: Vec<(usize, char)> = get_symbols(doc, &to_decimal);
let digits: Vec<u16> = get_digits(doc, &to_decimal);

if symbols.len() != 3 || digits.len() != 11 {
if symbols.len() != 3 || digits.len() != CNH_SIZE {
return false;
}

Expand Down Expand Up @@ -232,5 +234,5 @@ pub fn generate() -> String {
/// assert!(cnh::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate()).expect("Valid CNH!")
mask(&generate()).expect("Invalid CNH!")
}
31 changes: 16 additions & 15 deletions brado/src/cnpj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
use crate::common::{get_digits, get_symbols, random_digit_vector, to_decimal};

const CNPJ_SIZE: usize = 14;

fn to_cnpj_digit(
i: usize,
c: char,
Expand Down Expand Up @@ -43,18 +45,18 @@ fn to_cnpj_digit(
pub fn validate(doc: &str) -> bool {
let size: usize = doc.chars().count();

if size != 14 && !is_masked(doc) {
if size != CNPJ_SIZE && !is_masked(doc) {
return false;
}

let digits: Vec<u16> = get_digits(doc, Box::new(to_cnpj_digit));
let digits: Vec<u16> = get_digits(doc, &to_cnpj_digit);

if digits.len() != 14 {
if digits.len() != CNPJ_SIZE {
return false;
}

for i in 0..10 {
if digits.iter().filter(|&n| *n == i).count() == 14 {
for i in 0..=9 {
if digits.iter().filter(|&n| *n == i).count() == CNPJ_SIZE {
return false;
}
}
Expand Down Expand Up @@ -88,10 +90,9 @@ fn generate_digit(

let rest: u16 = sum % 11;

if rest < 2 {
0
} else {
11 - rest
match rest < 2 {
true => 0,
false => 11 - rest,
}
}

Expand Down Expand Up @@ -119,8 +120,8 @@ fn generate_digit(
/// assert!(result);
/// ```
pub fn is_bare(doc: &str) -> bool {
doc.chars().count() == 14
&& get_digits(doc, Box::new(to_cnpj_digit)).len() == 14
doc.chars().count() == CNPJ_SIZE
&& get_digits(doc, &to_cnpj_digit).len() == CNPJ_SIZE
}

/// Verifica se o argumento `doc` pode ser um CNPJ com símbolos.
Expand All @@ -147,10 +148,10 @@ pub fn is_bare(doc: &str) -> bool {
/// assert!(result);
/// ```
pub fn is_masked(doc: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(doc, Box::new(to_cnpj_digit));
let digits: Vec<u16> = get_digits(doc, Box::new(to_cnpj_digit));
let symbols: Vec<(usize, char)> = get_symbols(doc, &to_cnpj_digit);
let digits: Vec<u16> = get_digits(doc, &to_cnpj_digit);

if symbols.len() != 4 || digits.len() != 14 {
if symbols.len() != 4 || digits.len() != CNPJ_SIZE {
return false;
}

Expand Down Expand Up @@ -233,5 +234,5 @@ pub fn generate() -> String {
/// assert!(cnpj::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate()).expect("Valid CNPJ!")
mask(&generate()).expect("Invalid CNPJ!")
}
20 changes: 11 additions & 9 deletions brado/src/cns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::common::{
to_decimal,
};

const CNS_SIZE: usize = 15;

/// Realiza validação de CNS, máscarado ou não.
/// Retorna `true` se o argumento `doc` for um CNS válido, caso contrário,
/// retorna `false`.
Expand Down Expand Up @@ -35,13 +37,13 @@ use crate::common::{
pub fn validate(doc: &str) -> bool {
let size: usize = doc.chars().count();

if size != 15 && !is_masked(doc) {
if size != CNS_SIZE && !is_masked(doc) {
return false;
}

let digits: Vec<u16> = get_digits(doc, Box::new(to_decimal));
let digits: Vec<u16> = get_digits(doc, &to_decimal);

if digits.len() != 15 || is_first_digit_invalid(&digits[0]) {
if digits.len() != CNS_SIZE || is_first_digit_invalid(&digits[0]) {
return false;
}

Expand Down Expand Up @@ -127,8 +129,8 @@ fn generate_last_four_digits(doc_slice: &[u16]) -> Vec<u16> {
/// assert!(result);
/// ```
pub fn is_bare(doc: &str) -> bool {
doc.chars().count() == 15
&& get_digits(doc, Box::new(to_decimal)).len() == 15
doc.chars().count() == CNS_SIZE
&& get_digits(doc, &to_decimal).len() == CNS_SIZE
}

/// Verifica se o argumento `doc` pode ser um CNS com símbolos.
Expand All @@ -155,10 +157,10 @@ pub fn is_bare(doc: &str) -> bool {
/// assert!(result);
/// ```
pub fn is_masked(doc: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(doc, Box::new(to_decimal));
let digits: Vec<u16> = get_digits(doc, Box::new(to_decimal));
let symbols: Vec<(usize, char)> = get_symbols(doc, &to_decimal);
let digits: Vec<u16> = get_digits(doc, &to_decimal);

if symbols.len() != 3 || digits.len() != 15 {
if symbols.len() != 3 || digits.len() != CNS_SIZE {
return false;
}

Expand Down Expand Up @@ -297,5 +299,5 @@ fn generate_second_case(first_digit: u16) -> Vec<u16> {
/// assert!(cns::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate()).expect("Valid CNS!")
mask(&generate()).expect("Invalid CNS!")
}
41 changes: 25 additions & 16 deletions brado/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,22 @@ pub fn to_decimal(
/// ```
/// use brado::common::{get_digits, to_decimal};
///
/// let result = get_digits("111", Box::new(to_decimal));
/// let result = get_digits("111", &to_decimal);
/// assert_eq!(result, vec![1, 1, 1]);
///
/// let result = get_digits("121", Box::new(to_decimal));
/// let result = get_digits("121", &to_decimal);
/// assert_eq!(result, vec![1, 2, 1]);
/// ```
pub fn get_digits(
pub fn get_digits<F>(
doc: &str,
cfn: Box<dyn Fn(usize, char) -> Option<u16>>,
) -> Vec<u16> {
convert: &F,
) -> Vec<u16>
where
F: Fn(usize, char) -> Option<u16>,
{
doc.chars()
.enumerate()
.filter_map(|(i, c)| cfn(i, c))
.filter_map(|(i, c)| convert(i, c))
.collect()
}

Expand All @@ -78,16 +81,19 @@ pub fn get_digits(
/// ```
/// use brado::common::{get_symbols, to_decimal};
///
/// let result = get_symbols("1.1-1", Box::new(to_decimal));
/// let result = get_symbols("1.1-1", &to_decimal);
/// assert_eq!(result, vec![(1, '.'), (3, '-')]);
/// ```
pub fn get_symbols(
pub fn get_symbols<F>(
doc: &str,
cfn: Box<dyn Fn(usize, char) -> Option<u16>>,
) -> Vec<(usize, char)> {
convert: F,
) -> Vec<(usize, char)>
where
F: Fn(usize, char) -> Option<u16>,
{
doc.chars()
.enumerate()
.filter_map(|(i, c)| match cfn(i, c) {
.filter_map(|(i, c)| match convert(i, c) {
Some(_) => None,
None => Some((i, c)),
})
Expand All @@ -103,16 +109,19 @@ pub fn get_symbols(
/// ```
/// use brado::common::{unmask, to_decimal};
///
/// let result = unmask("1.1-1", Box::new(to_decimal));
/// let result = unmask("1.1-1", &to_decimal);
/// assert_eq!(result, String::from("111"));
/// ```
pub fn unmask(
pub fn unmask<F>(
doc: &str,
cfn: Box<dyn Fn(usize, char) -> Option<u16>>,
) -> String {
convert: F,
) -> String
where
F: Fn(usize, char) -> Option<u16>,
{
doc.chars()
.enumerate()
.filter_map(|(i, c)| cfn(i, c).map(|n| n.to_string()))
.filter_map(|(i, c)| convert(i, c).map(|n| n.to_string()))
.collect::<Vec<String>>()
.join("")
}
Expand Down
Loading

0 comments on commit db47733

Please sign in to comment.