Skip to content

Commit

Permalink
Reorganiza código
Browse files Browse the repository at this point in the history
  • Loading branch information
brenomfviana authored Feb 3, 2024
1 parent c7791e7 commit dd027b9
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 51 deletions.
6 changes: 3 additions & 3 deletions brado/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ SHELL := /bin/bash

fmt:
reset
cargo fmt
cargo fmt $(args)

test:
reset
cargo test
cargo test $(args)

clippy:
reset
cargo clippy
cargo clippy $(args)
39 changes: 28 additions & 11 deletions brado/src/cnh/utils.rs → brado/src/cnh.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::utils::{get_digits, get_symbols, is_repeated};
use crate::common::{get_digits, get_symbols, is_repeated};

pub fn validate(cnh: &str) -> bool {
let size: usize = cnh.chars().count();
Expand All @@ -7,7 +7,7 @@ pub fn validate(cnh: &str) -> bool {
return false;
}

let digits: Vec<u8> = get_digits(cnh);
let digits: Vec<u16> = get_digits(cnh);

if digits.len() != 11 {
return false;
Expand All @@ -17,41 +17,55 @@ pub fn validate(cnh: &str) -> bool {
return false;
}

let (d10, d11) = generate_digits(&digits);
let (d10, d11): (u16, u16) = generate_digits(&digits);

(d10, d11) == (digits[9], digits[10])
}

fn generate_digits(cnh: &[u8]) -> (u8, u8) {
// First Digit
fn generate_digits(cpf_slice: &[u16]) -> (u16, u16) {
let (d10, dsc): (u16, u16) = generate_first_digit(cpf_slice);
let d11: u16 = generate_second_digit(cpf_slice, dsc);

(d10, d11)
}

fn generate_first_digit(cnh: &[u16]) -> (u16, u16) {
let mut sum: u16 = 0;
let mut dsc: u16 = 0;

for i in (0..=9).rev() {
sum += (cnh[9 - i] as u16) * (i as u16);
sum += cnh[9 - i] * (i as u16);
}

let mut first = sum % 11;
let mut first: u16 = sum % 11;

if first >= 10 {
first = 0;
dsc = 2;
}

// Second Digit
(first, dsc)
}

fn generate_second_digit(
cnh: &[u16],
dsc: u16,
) -> u16 {
let mut sum: u16 = 0;

for i in 1..=9 {
sum += (cnh[i - 1] as u16) * (i as u16);
sum += cnh[i - 1] * (i as u16);
}

let mut second: i16 = (sum % 11) as i16 - dsc as i16;
let mut second: i16 = ((sum % 11) as i16) - (dsc as i16);

if second < 0 {
second += 11;
} else if second >= 10 {
second = 0;
}

(first as u8, second as u8)
second as u16
}

pub fn is_bare(cnh: &str) -> bool {
Expand All @@ -60,16 +74,19 @@ pub fn is_bare(cnh: &str) -> bool {

pub fn is_masked(cnh: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(cnh);

if symbols.len() != 3 {
return false;
}

symbols[0] == (3, ' ') && symbols[1] == (7, ' ') && symbols[2] == (11, ' ')
}

pub fn mask(cnh: &str) -> String {
if !is_bare(cnh) {
panic!("The given string cannot be masked as CNH!")
}

format!(
"{} {} {} {}",
&cnh[0..3],
Expand Down
2 changes: 0 additions & 2 deletions brado/src/cnh/mod.rs

This file was deleted.

30 changes: 16 additions & 14 deletions brado/src/cnpj/utils.rs → brado/src/cnpj.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::utils::{get_digits, get_symbols};
use crate::common::{get_digits, get_symbols};

pub fn validate(cnpj: &str) -> bool {
let size: usize = cnpj.chars().count();
Expand All @@ -7,7 +7,7 @@ pub fn validate(cnpj: &str) -> bool {
return false;
}

let digits: Vec<u8> = get_digits(cnpj);
let digits: Vec<u16> = get_digits(cnpj);

if digits.len() != 14 {
return false;
Expand All @@ -19,35 +19,34 @@ pub fn validate(cnpj: &str) -> bool {
}
}

let (d13, d14): (u8, u8) = generate_digits(&digits[..12]);
let (d13, d14): (u16, u16) = generate_digits(&digits[..12]);

(d13, d14) == (digits[12], digits[13])
}

fn generate_digits(cnpj_slice: &[u8]) -> (u8, u8) {
let mut cnpj_slice = cnpj_slice.to_vec();
fn generate_digits(cnpj_slice: &[u16]) -> (u16, u16) {
let mut cnpj_slice: Vec<u16> = cnpj_slice.to_vec();

let weights = vec![5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
let d13 = generate_digit(&cnpj_slice, 12, weights);
let weights: Vec<u16> = vec![5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
let d13: u16 = generate_digit(&cnpj_slice, 12, weights);

cnpj_slice.push(d13);

let weights = vec![6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
let d14 = generate_digit(&cnpj_slice, 13, weights);
let weights: Vec<u16> = vec![6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
let d14: u16 = generate_digit(&cnpj_slice, 13, weights);

(d13, d14)
}

fn generate_digit(
cnpj_slice: &[u8],
cnpj_slice: &[u16],
max: usize,
weights: Vec<u16>,
) -> u8 {
) -> u16 {
let mut sum: u16 = 0;

for i in 0..max {
let digit = cnpj_slice[i] as u16;
sum += digit * weights[i];
sum += cnpj_slice[i] * weights[i];
}

sum %= 11;
Expand All @@ -58,7 +57,7 @@ fn generate_digit(
sum = 11 - sum;
}

sum as u8
sum
}

pub fn is_bare(cnpj: &str) -> bool {
Expand All @@ -67,9 +66,11 @@ pub fn is_bare(cnpj: &str) -> bool {

pub fn is_masked(cnpj: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(cnpj);

if symbols.len() != 4 {
return false;
}

symbols[0] == (2, '.')
&& symbols[1] == (6, '.')
&& symbols[2] == (10, '/')
Expand All @@ -80,6 +81,7 @@ pub fn mask(cnpj: &str) -> String {
if !is_bare(cnpj) {
panic!("The given string cannot be masked as CNPJ!")
}

format!(
"{}.{}.{}/{}-{}",
&cnpj[0..2],
Expand Down
2 changes: 0 additions & 2 deletions brado/src/cnpj/mod.rs

This file was deleted.

8 changes: 4 additions & 4 deletions brado/src/common/utils.rs → brado/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use std::collections::HashSet;

const RADIX: u32 = 10;

pub fn is_repeated(digits: &[u8]) -> bool {
let a_set: HashSet<u8> = HashSet::from_iter(digits.iter().cloned());
pub fn is_repeated(digits: &[u16]) -> bool {
let a_set: HashSet<u16> = HashSet::from_iter(digits.iter().cloned());
a_set.len() == 1
}

pub fn get_digits(document: &str) -> Vec<u8> {
pub fn get_digits(document: &str) -> Vec<u16> {
document
.chars()
.filter_map(|c| c.to_digit(RADIX).map(|c| c as u8))
.filter_map(|c| c.to_digit(RADIX).map(|c| c as u16))
.collect()
}

Expand Down
1 change: 0 additions & 1 deletion brado/src/common/mod.rs

This file was deleted.

26 changes: 14 additions & 12 deletions brado/src/cpf/utils.rs → brado/src/cpf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::utils::{get_digits, get_symbols, is_repeated};
use crate::common::{get_digits, get_symbols, is_repeated};

pub fn validate(cpf: &str) -> bool {
let size: usize = cpf.chars().count();
Expand All @@ -7,34 +7,33 @@ pub fn validate(cpf: &str) -> bool {
return false;
}

let digits: Vec<u8> = get_digits(cpf);
let digits: Vec<u16> = get_digits(cpf);

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

let (d10, d11): (u8, u8) = generate_digits(&digits[..10]);
let (d10, d11): (u16, u16) = generate_digits(&digits[..10]);

(d10, d11) == (digits[9], digits[10])
}

fn generate_digits(cpf_slice: &[u8]) -> (u8, u8) {
let d10 = generate_digit(cpf_slice, 10);
let d11 = generate_digit(cpf_slice, 11);
fn generate_digits(cpf_slice: &[u16]) -> (u16, u16) {
let d10: u16 = generate_digit(cpf_slice, 10);
let d11: u16 = generate_digit(cpf_slice, 11);

(d10, d11)
}

fn generate_digit(
cpf_slice: &[u8],
cpf_slice: &[u16],
max: u16,
) -> u8 {
) -> u16 {
let mut sum: u16 = 0;

for i in (2..=max).rev() {
let idx = (max - i) as usize;
let digit = cpf_slice[idx] as u16;
sum += digit * i;
let idx: usize = (max - i) as usize;
sum += cpf_slice[idx] * i;
}

sum = (sum * 10) % 11;
Expand All @@ -43,7 +42,7 @@ fn generate_digit(
sum = 0;
}

sum as u8
sum
}

pub fn is_bare(cpf: &str) -> bool {
Expand All @@ -52,16 +51,19 @@ pub fn is_bare(cpf: &str) -> bool {

pub fn is_masked(cpf: &str) -> bool {
let symbols: Vec<(usize, char)> = get_symbols(cpf);

if symbols.len() != 3 {
return false;
}

symbols[0] == (3, '.') && symbols[1] == (7, '.') && symbols[2] == (11, '-')
}

pub fn mask(cpf: &str) -> String {
if !is_bare(cpf) {
panic!("The given string cannot be masked as CPF!")
}

format!(
"{}.{}.{}-{}",
&cpf[0..3],
Expand Down
2 changes: 0 additions & 2 deletions brado/src/cpf/mod.rs

This file was deleted.

15 changes: 15 additions & 0 deletions brado/src/docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::cnh;
use crate::cnpj;
use crate::cpf;

pub fn is_cpf(document: &str) -> bool {
cpf::validate(document)
}

pub fn is_cnpj(document: &str) -> bool {
cnpj::validate(document)
}

pub fn is_cnh(document: &str) -> bool {
cnh::validate(document)
}
1 change: 1 addition & 0 deletions brado/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ pub mod cnh;
pub mod cnpj;
pub mod common;
pub mod cpf;
pub mod docs;
Loading

0 comments on commit dd027b9

Please sign in to comment.