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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
use chrono::{Datelike, Utc};
use rand::{distributions::Alphanumeric, prelude::Distribution};
use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum MrnGeneratorError {
    #[error("{0} is not a valid country code, it should be exactly two characters (e.g. 'IT')")]
    CountryCodeLength(String),
    #[error("{0} is not a valid procedure category")]
    InvalidProcedureCategory(String),
    #[error("{procedure_category}-{combination} is not a valid procedure category combination")]
    InvalidProcedureCategoryCombination {
        procedure_category: String,
        combination: String,
    },
    #[error("{0} is not an alphanumeric")]
    NotAlphanumeric(char),
}

/// Returns a valid MRN given a country code
pub fn generate_random_mrn(
    country_code: &str,
    procedure: Option<Procedure>,
    declaration_office: Option<&str>,
) -> Result<String, MrnGeneratorError> {
    use MrnGeneratorError::*;

    let curr_year: String = Utc::now().year().to_string().chars().skip(2).collect();

    let random_str_len = 14 - declaration_office.map_or(0, |decoffice| decoffice.len());

    let random_str: String = Alphanumeric
        .sample_iter(&mut rand::thread_rng())
        .take(random_str_len)
        .map(|c| c.to_ascii_uppercase() as char)
        .collect();

    if country_code.len() != 2 {
        return Err(CountryCodeLength(country_code.to_string()));
    }

    let mut mrn = format!(
        "{}{}{}{}",
        curr_year,
        capitalize(country_code),
        declaration_office.unwrap_or(""),
        random_str
    );

    if let Some(procedure) = procedure {
        let proctgr_char = procecure_category_to_char(procedure).to_string();

        // Replace n-1 char with regime char
        mrn.replace_range(16..17, &proctgr_char);
    }

    // Check MRN, and replace last character if invalid
    let last_digit = is_mrn_valid(&mrn)?;

    if let Some(last_digit) = last_digit {
        Ok(replace_last_char(&mrn, last_digit))
    } else {
        Ok(mrn)
    }
}

/// Returns None if MRN is valid, and correct last character if it's invalid
pub fn is_mrn_valid(mrn: &str) -> Result<Option<char>, MrnGeneratorError> {
    let mut mrn_iter = mrn.chars();
    let last_digit = mrn_iter.next_back().unwrap();

    let mrn_temp: String = mrn_iter.collect();

    // Multiply each char value with it's power of 2 and sum them
    let multiplied_sum: u32 = mrn_temp
        .chars()
        .zip(0..mrn_temp.len())
        .map(|(c, m)| (check_character_value(c).map(|value| (value as u32) << m)))
        .collect::<Result<Vec<u32>, MrnGeneratorError>>()?
        .iter()
        .sum();

    let check_digit: u8 = (multiplied_sum % 11).try_into().unwrap();
    Ok(check_remainder_value(check_digit, last_digit))
}

/// Procedure types
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Procedure {
    ExportOnly,
    ExportAndExitSummaryDeclaration,
    ExitSummaryDeclarationOnly,
    ReExportNotification,
    DispatchOfGoodsInRelationWithSpecialFiscalTerritories,
    TransitDeclarationOnly,
    TransitDeclarationAndExitSummaryDeclaration,
    TransitDeclarationAndEntrySummaryDeclaration,
    ProofOfTheCustomsStatusOfUnionGoods,
    ImportDeclarationOnly,
    ImportDeclarationAndEntrySummaryDeclaration,
    EntrySummaryDeclarationOnly,
    TemporaryStorageDeclaration,
    IntroductionOfGoodsInRelationWithSpecialFiscalTerritories,
    TemporaryStorageDeclarationAndEntrySummaryDeclaration,
}

/// Maps procedure category to a corresponding character
pub fn procecure_category_to_char(procedure: Procedure) -> char {
    match procedure {
        Procedure::ExportOnly => 'A',
        Procedure::ExportAndExitSummaryDeclaration => 'B',
        Procedure::ExitSummaryDeclarationOnly => 'C',
        Procedure::ReExportNotification => 'D',
        Procedure::DispatchOfGoodsInRelationWithSpecialFiscalTerritories => 'E',
        Procedure::TransitDeclarationOnly => 'J',
        Procedure::TransitDeclarationAndExitSummaryDeclaration => 'K',
        Procedure::TransitDeclarationAndEntrySummaryDeclaration => 'L',
        Procedure::ProofOfTheCustomsStatusOfUnionGoods => 'M',
        Procedure::ImportDeclarationOnly => 'R',
        Procedure::ImportDeclarationAndEntrySummaryDeclaration => 'S',
        Procedure::EntrySummaryDeclarationOnly => 'T',
        Procedure::TemporaryStorageDeclaration => 'U',
        Procedure::IntroductionOfGoodsInRelationWithSpecialFiscalTerritories => 'V',
        Procedure::TemporaryStorageDeclarationAndEntrySummaryDeclaration => 'W',
    }
}

/// Matches a procedure category code (optionally combined with another one) and returns
/// the corresponding customs procedure
pub fn match_procedure(
    proctgr: &str,
    combined: Option<&str>,
) -> Result<Procedure, MrnGeneratorError> {
    use MrnGeneratorError::*;

    let exit_combined = ["A"];
    let entry_combined = ["F"];
    match proctgr {
        "B1" | "B2" | "B3" | "C1" if combined.is_none() => Ok(Procedure::ExportOnly),
        "B1" | "B2" | "B3" | "C1" if combined.is_some_and(|c| exit_combined.contains(&c)) => {
            Ok(Procedure::ExportAndExitSummaryDeclaration)
        }
        "A1" | "A2" => Ok(Procedure::ExitSummaryDeclarationOnly),
        "A3" => Ok(Procedure::ReExportNotification),
        "B4" => Ok(Procedure::DispatchOfGoodsInRelationWithSpecialFiscalTerritories),
        "D1" | "D2" | "D3" if combined.is_none() => Ok(Procedure::TransitDeclarationOnly),
        "D1" | "D2" | "D3" if combined.is_some_and(|c| exit_combined.contains(&c)) => {
            Ok(Procedure::TransitDeclarationAndExitSummaryDeclaration)
        }
        "D1" | "D2" | "D3" if combined.is_some_and(|c| entry_combined.contains(&c)) => {
            Ok(Procedure::TransitDeclarationAndEntrySummaryDeclaration)
        }
        "E1" | "E2" => Ok(Procedure::ProofOfTheCustomsStatusOfUnionGoods),
        "H1" | "H2" | "H3" | "H4" | "H6" | "I1" if combined.is_none() => {
            Ok(Procedure::ImportDeclarationOnly)
        }
        "H1" | "H2" | "H3" | "H4" | "H6" | "I1"
            if combined.is_some_and(|c| entry_combined.contains(&c)) =>
        {
            Ok(Procedure::ImportDeclarationAndEntrySummaryDeclaration)
        }
        "F1a" | "F1b" | "F1c" | "F1d" | "F2a" | "F2b" | "F2c" | "F2d" | "F3a" | "F3b" | "F4a"
        | "F4b" | "F4c" | "F5" => Ok(Procedure::EntrySummaryDeclarationOnly),
        "H5" => Ok(Procedure::IntroductionOfGoodsInRelationWithSpecialFiscalTerritories),
        "G4" if combined.is_none() => Ok(Procedure::TemporaryStorageDeclaration),
        "G4" if combined.is_some_and(|c| entry_combined.contains(&c)) => {
            Ok(Procedure::TemporaryStorageDeclarationAndEntrySummaryDeclaration)
        }
        _ => {
            if let Some(c) = combined {
                Err(InvalidProcedureCategoryCombination {
                    procedure_category: proctgr.to_string(),
                    combination: c.to_string(),
                })
            } else {
                Err(InvalidProcedureCategory(proctgr.to_string()))
            }
        }
    }
}

/// Capitalizes string
pub fn capitalize(s: &str) -> String {
    s.chars().map(|c| c.to_ascii_uppercase()).collect()
}

/// Replaces last character of string with new character
pub fn replace_last_char(s: &str, c: char) -> String {
    let mut new_str = s.to_string();
    new_str.pop();
    new_str.push(c);
    new_str
}

/// Remainder values according to tables in ISO 6346
pub fn check_remainder_value(check_digit: u8, last_digit: char) -> Option<char> {
    if check_digit % 10 != last_digit as u8 - 48 {
        char::from_digit((check_digit % 10) as u32, 10)
    } else {
        None
    }
}

/// Character values according to tables in ISO 6346
pub fn check_character_value(c: char) -> Result<u8, MrnGeneratorError> {
    if c.is_ascii_digit() {
        return Ok(c as u8 - 48);
    }
    if c.is_alphabetic() {
        if c == 'A' {
            return Ok(10);
        } else if ('B'..='K').contains(&c) {
            return Ok(c as u8 - 54);
        } else if ('L'..='U').contains(&c) {
            return Ok(c as u8 - 53);
        } else {
            return Ok(c as u8 - 52);
        }
    }

    // Default as fallback, change to an error sometime
    Err(MrnGeneratorError::NotAlphanumeric(c))
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn generate_random_mrn_test() {
        let mrn = generate_random_mrn("DK", Some(Procedure::ExportOnly), None).unwrap();

        let country_code: String = mrn.chars().skip(2).take(2).collect();
        let actual_year: String = mrn.chars().take(2).collect();
        let expected_year: String = Utc::now().year().to_string().chars().skip(2).collect();
        let procedure_char: char = mrn.chars().nth(16).unwrap();
        assert_eq!(18, mrn.len());
        assert_eq!(expected_year, actual_year);
        assert_eq!('A', procedure_char);
        assert_eq!("DK".to_string(), country_code);
        assert_eq!(None, is_mrn_valid(&mrn).unwrap());
    }

    #[test]
    fn generate_random_mrn_test_without_procedure() {
        let mrn = generate_random_mrn("DK", None, None).unwrap();

        let country_code: String = mrn.chars().skip(2).take(2).collect();
        let actual_year: String = mrn.chars().take(2).collect();
        let expected_year: String = Utc::now().year().to_string().chars().skip(2).collect();
        assert_eq!(18, mrn.len());
        assert_eq!(expected_year, actual_year);
        assert_eq!("DK".to_string(), country_code);
        assert_eq!(None, is_mrn_valid(&mrn).unwrap());
    }

    #[test]
    fn generate_random_mrn_test_with_declaration_office() {
        let mrn = generate_random_mrn("DK", None, Some("004700")).unwrap();

        let country_code: String = mrn.chars().skip(2).take(2).collect();
        let actual_year: String = mrn.chars().take(2).collect();
        let declaration_office: String = mrn.chars().skip(4).take(6).collect();
        let expected_year: String = Utc::now().year().to_string().chars().skip(2).collect();
        assert_eq!(18, mrn.len());
        assert_eq!(expected_year, actual_year);
        assert_eq!("DK".to_string(), country_code);
        assert_eq!("004700".to_string(), declaration_office);
        assert_eq!(None, is_mrn_valid(&mrn).unwrap());
    }

    #[test]
    fn is_mrn_valid_test() {
        assert_eq!(None, is_mrn_valid("22ITZXBZYUTJFLJXK6").unwrap());
        assert_eq!(Some('1'), is_mrn_valid("22DK1V0QQK2S6J7TU2").unwrap());
    }

    #[test]
    fn procedure_matched_test() {
        assert_eq!(Procedure::ExportOnly, match_procedure("B1", None).unwrap());
        assert_eq!(
            Procedure::ExportAndExitSummaryDeclaration,
            match_procedure("B2", Some("A")).unwrap()
        );
    }

    #[test]
    fn procedure_not_matched_test() {
        use MrnGeneratorError::*;

        assert_eq!(
            Err(InvalidProcedureCategoryCombination {
                procedure_category: "B2".to_string(),
                combination: "B".to_string()
            }),
            match_procedure("B2", Some("B"))
        );

        let invalid_procedure_category = "not a valid procedure 🤡";

        assert_eq!(
            Err(InvalidProcedureCategory(
                invalid_procedure_category.to_string()
            )),
            match_procedure(invalid_procedure_category, None)
        );
        assert_eq!(
            Err(InvalidProcedureCategoryCombination {
                procedure_category: invalid_procedure_category.to_string(),
                combination: "F".to_string()
            }),
            match_procedure(invalid_procedure_category, Some("F"))
        );
    }

    #[test]
    fn capitalize_test() {
        assert_eq!("BAT", capitalize("bat"))
    }

    #[test]
    fn replace_last_char_test() {
        assert_eq!("bar", replace_last_char("bat", 'r'))
    }

    #[test]
    fn check_remainder_value_test() {
        assert_eq!(None, check_remainder_value(3, '3'));
        assert_eq!(None, check_remainder_value(10, '0'));
        assert_eq!(Some('3'), check_remainder_value(3, '5'));
        assert_eq!(Some('0'), check_remainder_value(10, '9'));
    }

    #[test]
    fn check_character_value_test() {
        assert_eq!(3, check_character_value('3').unwrap());
        assert_eq!(10, check_character_value('A').unwrap());
        assert_eq!(13, check_character_value('C').unwrap());
        assert_eq!(35, check_character_value('W').unwrap());
        assert_eq!(
            Err(MrnGeneratorError::NotAlphanumeric('🤡')),
            check_character_value('🤡')
        );
    }
}