Skip to content

Commit

Permalink
Closes #3: Added optional parameter to include customs office of decl…
Browse files Browse the repository at this point in the history
…aration in the MRN
  • Loading branch information
psmith committed Aug 2, 2024
1 parent 06123ef commit 6300a29
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mrn-generator"
version = "0.2.2"
version = "0.3.0"
edition = "2021"
authors = ["Pavlos Smith <paulsmith4561+at+gmail.com>"]

Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,22 @@ Options:
- -n, --number-of-mrns <NUMBER_OF_MRNS> Number of MRNs to generate [default: 1]
- -p, --procedure-category <PROCEDURE_CATEGORY> Procedure category
- -C, --combined \<COMBINED\> Combined procedure category
- -o, --declaration-office <DECLARATION_OFFICE> Customs office of declaration
- -h, --help Print help
- -V, --version Print version

### Examples
```mrn-generator -c DK``` to generate an MRN with Denmark as a country code

```mrn-generator -c DK -o 004700``` to generate an MRN with Denmark as a country code and 004700
as the declaration office

```mrn-generator -c NL -n 20``` to generate 20 MRNs with Netherlands as a country code

```mrn-generator -c NL -n 20 -P B1``` to generate 20 MRNs with Netherlands as a country code
```mrn-generator -c NL -n 20 -p B1``` to generate 20 MRNs with Netherlands as a country code
and B1 procedure category

```mrn-generator -c NL -n 20 -P B1 -C A``` to generate 20 MRNs with Netherlands as a country code
```mrn-generator -c NL -n 20 -p B1 -C A``` to generate 20 MRNs with Netherlands as a country code
and B1 procedure category combined with A* procedure category

<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand Down
32 changes: 26 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ use parser::Args;
use rand::{distributions::Alphanumeric, prelude::Distribution};

/// Returns a valid MRN given a country code
fn generate_random_mrn(country_code: &str, procedure: Option<Procedure>) -> String {
fn generate_random_mrn(country_code: &str, procedure: Option<Procedure>, declaration_office: Option<&str>) -> String {
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(14)
.take(random_str_len)
.map(|c| c.to_ascii_uppercase() as char)
.collect();

if country_code.len() != 2 {
panic!("Country code should be 2 characters")
}

let mut mrn = format!("{}{}{}", curr_year, capitalize(country_code), random_str);
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();
Expand Down Expand Up @@ -63,12 +65,13 @@ fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let country_code = args.country_code;
let iterations = args.number_of_mrns;
let declaration_office = args.declaration_office.as_deref();

let combined = args.combined;
let procedure = args.procedure_category.map(|proctg| match_procedure(&proctg, combined.as_deref()));

for _ in 0..iterations {
let mrn: &str = &generate_random_mrn(&country_code, procedure);
let mrn: &str = &generate_random_mrn(&country_code, procedure, declaration_office);
println!("{mrn}");
}

Expand All @@ -82,7 +85,7 @@ mod tests {

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

let country_code: String = mrn.chars().skip(2).take(2).collect();
let actual_year: String = mrn.chars().take(2).collect();
Expand All @@ -92,18 +95,35 @@ mod tests {
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));
}

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

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));
}

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

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));
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ pub struct Args {
/// Combined procedure category
#[arg(short = 'C', long)]
pub combined: Option<String>,

/// Customs office of declaration
#[arg(short = 'o', long)]
pub declaration_office: Option<String>,
}

0 comments on commit 6300a29

Please sign in to comment.