Skip to content

Commit

Permalink
Refatora manipulação de erros (#53)
Browse files Browse the repository at this point in the history
* adiciona novos testes

* troca macro panic por enum Result

* atualiza versão

* remove Result das funções de geração de documentos

* atualiza README.md

* remove verificação desnecessária nas funções mask
  • Loading branch information
brenomfviana authored Mar 16, 2024
1 parent 5a8fab2 commit b6b8e04
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 57 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ cpf::validate("639.292.470-10"); // false

### mask

Mascara o documento passado como parâmetro (`&str`). Retorna a string (`String`) correspondente ao documento mascarado. A string passada não deve possuir símbolos.
Mascara o documento passado como parâmetro (`&str`). Retorna uma string (`Result<String, &'static str>`) correspondente ao documento mascarado ou um erro. A string passada não deve possuir símbolos.

```rust
use brado::cpf;

cpf::mask("63929247011"); // "639.292.470-11"
cpf::mask("63929247011"); // Ok("639.292.470-11")

cpf::mask("639.292.470-11"); // panic!
cpf::mask("639.292.470-11"); // Err("The given string cannot be masked as CPF!")
cpf::mask("639292470"); // Err("The given string cannot be masked as CPF!")
```

### is_bare
Expand Down
2 changes: 1 addition & 1 deletion brado/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brado"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
license = "MIT"
readme = "../README.md"
Expand Down
22 changes: 15 additions & 7 deletions brado/src/cnh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,36 @@ pub fn is_masked(doc: &str) -> bool {
/// ```
/// use brado::cnh;
///
/// let result = cnh::mask("84718735264"); // "847 187 352 64"
/// let result = match cnh::mask("84718735264") { // Ok("847 187 352 64")
/// Ok(doc) => doc,
/// Err(e) => panic!("{}", e),
/// };
/// assert!(cnh::is_masked(&result)); // true
/// ```
///
/// Documento de 11 dígitos com máscara:
/// ```should_panic
/// use brado::cnh;
///
/// cnh::mask("847 187 352 64"); // panic!
/// let result = match cnh::mask("847 187 352 64") { // It panics!
/// Ok(doc) => doc,
/// Err(e) => panic!("{}", e),
/// };
/// ```
pub fn mask(doc: &str) -> String {
pub fn mask(doc: &str) -> Result<String, &'static str> {
if !is_bare(doc) {
panic!("The given string cannot be masked as CNH!")
return Err("The given string cannot be masked as CNH!");
}

format!(
let masked_doc = format!(
"{} {} {} {}",
&doc[0..3],
&doc[3..6],
&doc[6..9],
&doc[9..11],
)
);

Ok(masked_doc)
}

/// Gera e retorna um CNH aleatório sem máscara.
Expand Down Expand Up @@ -226,5 +234,5 @@ pub fn generate() -> String {
/// assert!(cnh::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate())
mask(&generate()).expect("Valid CNH!")
}
22 changes: 15 additions & 7 deletions brado/src/cnpj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,37 @@ pub fn is_masked(doc: &str) -> bool {
/// ```
/// use brado::cnpj;
///
/// let result = cnpj::mask("05200851000100"); // "05.200.851/0001-00"
/// let result = match cnpj::mask("05200851000100") { // Ok("05.200.851/0001-00")
/// Ok(doc) => doc,
/// Err(e) => panic!("{}", e),
/// };
/// assert!(cnpj::is_masked(&result)); // true
/// ```
///
/// Documento de 14 dígitos com máscara:
/// ```should_panic
/// use brado::cnpj;
///
/// cnpj::mask("05.200.851/0001-00"); // panic!
/// let result = match cnpj::mask("05.200.851/0001-00") { // It panics!
/// Ok(doc) => doc,
/// Err(e) => panic!("{}", e),
/// };
/// ```
pub fn mask(doc: &str) -> String {
pub fn mask(doc: &str) -> Result<String, &'static str> {
if !is_bare(doc) {
panic!("The given string cannot be masked as CNPJ!")
return Err("The given string cannot be masked as CNPJ!");
}

format!(
let masked_doc = format!(
"{}.{}.{}/{}-{}",
&doc[0..2],
&doc[2..5],
&doc[5..8],
&doc[8..12],
&doc[12..14],
)
);

Ok(masked_doc)
}

/// Gera e retorna um CNPJ aleatório sem máscara.
Expand Down Expand Up @@ -218,5 +226,5 @@ pub fn generate() -> String {
/// assert!(cnpj::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate())
mask(&generate()).expect("Valid CNPJ!")
}
22 changes: 15 additions & 7 deletions brado/src/cpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,28 +147,36 @@ pub fn is_masked(doc: &str) -> bool {
/// ```
/// use brado::cpf;
///
/// let result = cpf::mask("63929247011"); // "639.292.470-11"
/// let result = match cpf::mask("63929247011") { // Ok("639.292.470-11")
/// Ok(doc) => doc,
/// Err(e) => panic!("{}", e),
/// };
/// assert!(cpf::is_masked(&result)); // true
/// ```
///
/// Documento de 11 dígitos com máscara:
/// ```should_panic
/// use brado::cpf;
///
/// cpf::mask("639.292.470-11"); // panic!
/// let result = match cpf::mask("639.292.470-11") { // It panics!
/// Ok(doc) => doc,
/// Err(e) => panic!("{}", e),
/// };
/// ```
pub fn mask(doc: &str) -> String {
pub fn mask(doc: &str) -> Result<String, &'static str> {
if !is_bare(doc) {
panic!("The given string cannot be masked as CPF!")
return Err("The given string cannot be masked as CPF!");
}

format!(
let masked_doc = format!(
"{}.{}.{}-{}",
&doc[0..3],
&doc[3..6],
&doc[6..9],
&doc[9..11],
)
);

Ok(masked_doc)
}

/// Gera e retorna um CPF aleatório sem máscara.
Expand Down Expand Up @@ -201,5 +209,5 @@ pub fn generate() -> String {
/// assert!(cpf::is_masked(&result)); // true
/// ```
pub fn generate_masked() -> String {
mask(&generate())
mask(&generate()).expect("Valid CPF!")
}
77 changes: 65 additions & 12 deletions brado/tests/cnh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,55 @@ mod cnh_tests {
#[test]
fn cnh_validate_1_valid_bare_cnh() {
let bare_cnh: &str = "84718735264";
assert_eq!(true, brado::cnh::validate(bare_cnh));
assert_eq!(brado::cnh::validate(bare_cnh), true);
}

#[test]
fn cnh_validate_2_valid_masked_cnh() {
let masked_cnh: &str = "847 187 352 64";
assert_eq!(true, brado::cnh::validate(masked_cnh));
assert_eq!(brado::cnh::validate(masked_cnh), true);
}

#[test]
fn cnh_validate_3_invalid_mask() {
fn cnh_validate_3_invalid_repeated_numbers() {
for i in 1..=9 {
let document: String = (vec![i.to_string(); 11]).join("");
assert_eq!(brado::cnh::validate(&document), false);
}
}

#[test]
fn cnh_validate_4_invalid_bare_cnh() {
let bare_cnh: &str = "84718735265";
assert_eq!(brado::cnh::validate(bare_cnh), false);
}

#[test]
fn cnh_validate_5_invalid_masked_cnh() {
let masked_cnh: &str = "847 187 352 65";
assert_eq!(brado::cnh::validate(masked_cnh), false);
}

#[test]
fn cnh_validate_6_invalid_mask() {
let document: &str = "84 718 735 264";
assert_eq!(brado::cnh::validate(document), false);
}

#[test]
fn cnh_validate_4_invalid_other_document_1() {
fn cnh_validate_7_invalid_other_document_1() {
let document: &str = "847187352";
assert_eq!(brado::cnh::validate(document), false);
}

#[test]
fn cnh_validate_5_invalid_other_document_2() {
fn cnh_validate_8_invalid_other_document_2() {
let document: &str = "084 718 735 264";
assert_eq!(brado::cnh::validate(document), false);
}

#[test]
fn cnh_validate_6_invalid_other_document_3() {
fn cnh_validate_9_invalid_other_document_3() {
let document: &str = "847 187 352 6:4";
assert_eq!(brado::cnh::validate(document), false);
}
Expand All @@ -50,6 +70,24 @@ mod cnh_tests {
assert_eq!(brado::cnh::is_bare(masked_cnh), false);
}

#[test]
fn cnh_is_bare_3_other_document() {
let bare_document: &str = "84718735265";
assert_eq!(brado::cnh::is_bare(bare_document), true);
}

#[test]
fn cnh_is_bare_4_other_document() {
let bare_document: &str = "847 18735264";
assert_eq!(brado::cnh::is_bare(bare_document), false);
}

#[test]
fn cnh_is_bare_5_other_document() {
let bare_document: &str = "847187352645";
assert_eq!(brado::cnh::is_bare(bare_document), false);
}

#[test]
fn cnh_is_masked_1_masked_cnh() {
let masked_cnh: &str = "847 187 352 64";
Expand All @@ -62,24 +100,39 @@ mod cnh_tests {
assert_eq!(brado::cnh::is_masked(bare_cnh), false);
}

#[test]
fn cnh_is_masked_3_other_document() {
let masked_document: &str = "8471 187 352 64";
assert_eq!(brado::cnh::is_masked(masked_document), false);
}

#[test]
fn cnh_is_masked_4_other_document() {
let masked_document: &str = "847 187 352 645";
assert_eq!(brado::cnh::is_masked(masked_document), false);
}

#[test]
fn cnh_mask_1_bare_cnh() {
let bare_cnh: &str = "84718735264";
assert_eq!(brado::cnh::mask(bare_cnh), String::from("847 187 352 64"),);
assert_eq!(
brado::cnh::mask(bare_cnh),
Ok(String::from("847 187 352 64"))
);
}

#[test]
#[should_panic(expected = "The given string cannot be masked as CNH")]
fn cnh_mask_2_masked_cnh() {
let masked_cnh: &str = "847 187 352 64";
brado::cnh::mask(masked_cnh);
let result = brado::cnh::mask(masked_cnh);
assert_eq!(result, Err("The given string cannot be masked as CNH!"),);
}

#[test]
#[should_panic(expected = "The given string cannot be masked as CNH")]
fn cnh_mask_3_invalid_cnh() {
let document: &str = "847187352";
brado::cnh::mask(document);
let result = brado::cnh::mask(document);
assert_eq!(result, Err("The given string cannot be masked as CNH!"),);
}

#[test]
Expand All @@ -90,7 +143,7 @@ mod cnh_tests {
}

#[test]
fn cpf_generate_masked_1() {
fn cnh_generate_masked_1() {
let cnh = brado::cnh::generate_masked();
assert_eq!(brado::cnh::validate(&cnh), true);
assert_eq!(brado::cnh::is_masked(&cnh), true);
Expand Down
Loading

0 comments on commit b6b8e04

Please sign in to comment.