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
//! # rifve
//!
//! Venezuelan RIF implementation useful for creating and validating RIF numbers
//!
//! <div align="center">
//! 
//!   [![Crates.io](https://img.shields.io/crates/v/rifven.svg)](https://crates.io/crates/rifven)
//!   [![Documentation](https://docs.rs/rifven/badge.svg)](https://docs.rs/rifven)
//!   ![Build](https://github.com/EstebanBorai/rifven/workflows/build/badge.svg)
//!   ![Lint](https://github.com/EstebanBorai/rifven/workflows/clippy/fmt/badge.svg)
//!   ![Tests](https://github.com/EstebanBorai/rifven/workflows/tests/badge.svg)
//! 
//! </div>
//! 
//! ## What are RIF numbers?
//! 
//! RIF (Registro de Informacion Fiscal) in english _Fiscal Information Registry_ is a number
//! provided by a Venezuelan entity SAIME used to identify multiple entities for taxable purposes.
//! 
//! The RIF is composed by a kind which could be:
//! 
//! - C: Township or Communal Council
//! - E: Represents a foreigner natural person and stands for
//! "Extranjera" and "Extranjero"
//! - G: Represents a goverment entity and stands for
//! "Gubernamental"
//! - J: Used for a legal entity. Could be a natural person
//! or a corporate entity and stands for "Jurídico"
//! - P: Used on RIF numbers which belongs to passports
//! - V: Represents a person with venezuelan citizenship and stands
//! for "Venezolana" and "Venezolano"
//! 
//! An identifier number followed by a hyphen symbol and finally a checksum digit, as well followed
//! by a hyphen symbol.
//! 
//! <div align="center">
//!   <img src="https://raw.githubusercontent.com/EstebanBorai/rifven/main/docs/rif_parts.png" />
//! </div>
//! 
//! ## Motivation
//! 
//! Implement a crate to help create instances of valid RIF numbers
//! 
//! ## Usage
//! 
//! Creating a new `Rif` instance providing each of its parts values
//! such as `Kind` (J; V; P; G; C), identifier (tax payer ID), check number.
//!
//! The following code, creates an instance of `Rif` for a RIF string which
//! looks like `J-07013380-5`:
//!
//! ```
//! use rifven::{Kind, Rif};
//!
//! let rif = Rif::new(Kind::Legal, 07013380, 5).unwrap();
//!
//! assert_eq!(rif.kind(), Kind::Legal);
//! assert_eq!(rif.identifier(), 7013380);
//! assert_eq!(rif.checksum_digit(), 5);
//! ```
//!
//! You can also create instances of `Rif` from its string representation
//!
//! ```
//! use rifven::{Kind, Rif};
//! use std::str::FromStr;
//!
//! let myrif = Rif::from_str("J-07013380-5").unwrap();
//!
//! assert_eq!(Rif::new(Kind::Legal, 07013380, 5).unwrap(), myrif);
//! ```
//!
mod error;
mod kind;
mod rif;

pub use kind::*;
pub use rif::*;

#[allow(unused_imports)]
mod tests {
    use std::str::FromStr;

    use crate::error::*;
    use crate::kind::*;
    use crate::rif::*;

    #[test]
    fn calcs_the_checksum_digit() {
        let rif_identifier = vec![
            (Kind::Legal, 00019361),
            (Kind::Legal, 07013380),
            (Kind::Legal, 31286704),
            (Kind::Legal, 40512535),
            (Kind::Legal, 40119253),
            (Kind::Government, 20009997),
            (Kind::Government, 20000001),
            (Kind::Government, 20000002),
            (Kind::Government, 20000004),
            (Kind::Government, 20000044),
        ];

        let rif_checksum_digit = vec![4, 5, 3, 7, 0, 6, 5, 3, 0, 9];

        for (idx, (kind, identifier)) in rif_identifier.iter().enumerate() {
            assert_eq!(
                rif_checksum_digit[idx],
                Rif::calc_checksum_digit(kind, *identifier),
            );
        }
    }

    #[test]
    fn creates_rif_from_str() {
        let candidates = vec![
            Rif::new(Kind::Legal, 000019361, 4).unwrap(),
            Rif::new(Kind::Legal, 07013380, 5).unwrap(),
            Rif::new(Kind::Legal, 31286704, 3).unwrap(),
            Rif::new(Kind::Government, 20000044, 9).unwrap(),
            Rif::new(Kind::Government, 20000004, 0).unwrap(),
            Rif::new(Kind::Government, 20000002, 3).unwrap(),
        ];

        let expects = vec![
            Rif::from_str("J-00019361-4").unwrap(),
            Rif::from_str("J-07013380-5").unwrap(),
            Rif::from_str("J-31286704-3").unwrap(),
            Rif::from_str("G-20000044-9").unwrap(),
            Rif::from_str("G-20000004-0").unwrap(),
            Rif::from_str("G-20000002-3").unwrap(),
        ];

        for (idx, rif) in candidates.iter().enumerate() {
            assert_eq!(*rif, expects[idx]);
        }
    }

    #[test]
    fn checks_for_invalid_rifs_from_str() {
        let have = vec![
            Rif::from_str("J-00018461-4"),
            Rif::from_str("E-12312312-5"),
            Rif::from_str("M-00000001-3"),
            Rif::from_str("X-00029383-7"),
            Rif::from_str("V-AA348932-1"),
            Rif::from_str("G-X0000002-3"),
            Rif::from_str("G200000040"),
        ];

        let expected_error = vec![
          Error::UnexpectedCheckNum(5, 4),
          Error::UnexpectedCheckNum(6, 5),
          Error::InvalidRifKind(String::from("M")),
          Error::InvalidRifKind(String::from("X")),
          Error::InvalidRifIdentifier(String::from("invalid digit found in string")),
          Error::InvalidRifIdentifier(String::from("invalid digit found in string")),
          Error::InvalidRif(String::from("RIF must be splitted into 3 parts separated by dashes. Eg. J-123456789-1. Provided G200000040")),
      ];

        for (idx, rif) in have.into_iter().enumerate() {
            assert_eq!(rif.err().unwrap(), expected_error[idx]);
        }
    }
}