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
//! This is a lightweight crate for verifying NUBAN numbers
//! for all Nigerian bank accounts as was directed by the CBN.

use std::{cell::Cell, collections::HashMap, fmt, sync::Once};

pub const BANKS: [(&'static str, &'static str); 24] = [
    ("044", "Access Bank"),
    ("014", "Afribank"),
    ("023", "Citibank"),
    ("063", "Diamond Bank"),
    ("050", "Ecobank"),
    ("040", "Equitorial Trust Bank"),
    ("011", "First Bank"),
    ("214", "FCMB"),
    ("070", "Fidelity"),
    ("085", "FinBank"),
    ("058", "Guaranty Trust Bank"),
    ("069", "Intercontinentl Bank"),
    ("056", "Oceanic Bank"),
    ("082", "BankPhb"),
    ("076", "Skye Bank"),
    ("084", "SpringBank"),
    ("221", "StanbicIBTC"),
    ("068", "Standard Chartered Bank"),
    ("232", "Sterling Bank"),
    ("033", "United Bank For Africa"),
    ("032", "Union Bank"),
    ("035", "Wema Bank"),
    ("057", "Zenith Bank"),
    ("215", "Unity Bank"),
];

struct LazyBanks(Once, Cell<Option<HashMap<&'static str, &'static str>>>);

unsafe impl Sync for LazyBanks {}

static LAZY_BANKS: LazyBanks = LazyBanks(Once::new(), Cell::new(None));

#[derive(Eq, Clone, Debug, PartialEq)]
pub struct Nuban<'a>(&'a str, &'a str, &'a str);

#[derive(Eq, Copy, Clone, Debug, PartialEq)]
pub enum Error {
    InvalidBankCode,
    InvalidAccountNumber,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let reason = match self {
            Error::InvalidBankCode => "invalid bank code",
            Error::InvalidAccountNumber => "invalid account number",
        };
        write!(f, "{}", reason)
    }
}

impl<'a> Nuban<'a> {
    pub fn new(bank_code: &'a str, account_number: &'a str) -> Result<Self, Error> {
        #[rustfmt::skip] {
            if !Self::is_valid_bank(bank_code) { Err(Error::InvalidBankCode)? }
            if account_number.len() != 10  { Err(Error::InvalidAccountNumber)? }
        };

        let (_, check_digit) = Self::account_number_parts(bank_code, account_number)?;
        Ok(Nuban(bank_code, account_number, check_digit))
    }

    fn account_number_parts(
        bank_code: &'a str,
        account_number: &'a str,
    ) -> Result<(&'a str, &'a str), Error> {
        let (account_number, check_digit) = account_number.split_at(9);
        match (
            check_digit.chars().next().unwrap().to_digit(10),
            Self::calculate_check_digit(bank_code, account_number),
        ) {
            (Some(l), r) if l != r => Err(Error::InvalidAccountNumber),
            _ => Ok((account_number, check_digit)),
        }
    }

    pub fn is_valid_bank(bank_code: &str) -> bool {
        bank_code.len() == 3 && Self::banks().contains_key(bank_code)
    }

    pub fn is_valid_account(bank_code: &str, account_number: &str) -> bool {
        Nuban::new(bank_code, account_number).is_err()
    }

    pub fn bank_code(&self) -> &str {
        self.0
    }

    pub fn bank_name(&self) -> &str {
        Self::banks().get(self.0).unwrap()
    }

    pub fn account_number(&self) -> &str {
        self.1
    }

    pub fn check_digit(&self) -> &str {
        self.2
    }

    fn calculate_check_digit(bank_code: &'a str, account_number: &'a str) -> u32 {
        // The Approved NUBAN format: [ABC][DEFGHIJKL][M], where
        //   -       ABC : 3-digit Bank Code
        //   - DEFGHIJKL : NUBAN Account Serial Number
        //   -         M : NUBAN Check Digit
        // https://www.cbn.gov.ng/OUT/2011/CIRCULARS/BSPD/NUBAN%20PROPOSALS%20V%200%204-%2003%2009%202010.PDF

        let nuban_chars = bank_code.chars().chain(account_number.chars());
        let nuban_digits = nuban_chars.map(|num| num.to_digit(10).unwrap());
        let seed = [3, 7, 3, 3, 7, 3, 3, 7, 3, 3, 7, 3].iter();
        let check_sum: u32 = seed.zip(nuban_digits).map(|(l, r)| l * r).sum();
        match 10 - (check_sum % 10) {
            10 => 0,
            x => x,
        }
    }

    pub fn banks() -> &'static HashMap<&'static str, &'static str> {
        LAZY_BANKS
            .0
            .call_once(|| LAZY_BANKS.1.set(Some(BANKS.iter().copied().collect())));

        unsafe {
            if let Some(ref banks) = *LAZY_BANKS.1.as_ptr() {
                return banks;
            }
            unreachable!()
        }
    }

    pub fn bank_matches(
        account_number: &'a str,
    ) -> Result<impl Iterator<Item = (&str, &str)>, Error> {
        #[rustfmt::skip] if account_number.len() != 10  { Err(Error::InvalidAccountNumber)? };
        Ok(BANKS
            .iter()
            .copied()
            .filter(move |(code, _)| Self::account_number_parts(code, account_number).is_ok()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_returns_new_nuban_instance() {
        let account = Nuban::new("058", "0152792740");
        assert_eq!(account.unwrap(), Nuban("058", "0152792740", "0"));
    }

    #[test]
    fn test_returns_false_for_invalid_account() {
        let account = Nuban::new("058", "0982736625");
        assert!(account.is_err());
    }

    #[test]
    fn test_returns_true_for_valid_account() {
        let account = Nuban::new("058", "0152792740");
        assert!(account.is_ok());
    }

    #[test]
    fn test_calculate_check_digit() {
        let correct_check_digit = Nuban::calculate_check_digit("058", "0152792740");
        assert_eq!(correct_check_digit, 0);
    }

    #[test]
    fn test_get_bank_name() {
        let account = Nuban::new("058", "0152792740").unwrap();
        assert_eq!(account.bank_name(), String::from("Guaranty Trust Bank"));
    }

    #[test]
    fn bank_matches() {
        let matches = Nuban::bank_matches("2209514399")
            .unwrap()
            .map(|(code, _)| code)
            .collect::<Vec<_>>();
        let map = [
            "068", // Standard Chartered Bank
            "035", // Wema Bank
            "057", // Zenith Bank
        ];
        assert!(map.iter().all(|code| matches.contains(code)));
    }
}