@@ -5,7 +5,7 @@
|
||||
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
||||
*/
|
||||
|
||||
use chrono::{Local, DateTime};
|
||||
use chrono::{DateTime, Local};
|
||||
use locales::t;
|
||||
//use serde::{Deserialize, Serialize};
|
||||
use serde::Deserialize;
|
||||
@@ -17,7 +17,7 @@ use std::{
|
||||
};
|
||||
use tracing::{debug, trace, Level};
|
||||
|
||||
use advotracker::data::structures::{PolicyCode, PolicyList, PolicyDataList, PolicyData};
|
||||
use advotracker::data::structures::{PolicyCode, PolicyData, PolicyDataList, PolicyList};
|
||||
|
||||
// include modules
|
||||
mod parse_args;
|
||||
@@ -77,12 +77,15 @@ fn export(p: &mut String, lang: &String) -> Result<u64, Box<dyn Error>> {
|
||||
/// import from csv format
|
||||
/// https://docs.rs/csv/1.1.3/csv/cookbook/index.html
|
||||
/// https://blog.burntsushi.net/csv/
|
||||
fn import(p: &mut String, data_list: &mut PolicyDataList,
|
||||
policy_numbers: &mut HashMap<u64, PolicyCode>, lang: &String)
|
||||
-> Result<u64, Box<dyn Error>> {
|
||||
fn import(
|
||||
p: &mut String,
|
||||
data_list: &mut PolicyDataList,
|
||||
policy_numbers: &mut HashMap<u64, PolicyCode>,
|
||||
lang: &String,
|
||||
) -> Result<u64, Box<dyn Error>> {
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
use std::ffi::OsStr;
|
||||
|
||||
let mut res = t!("csv.import.started", lang);
|
||||
let mut state = t!("state.started", lang);
|
||||
@@ -142,7 +145,7 @@ fn import(p: &mut String, data_list: &mut PolicyDataList,
|
||||
// push record as new vector elements
|
||||
data_list.push(record);
|
||||
|
||||
count +=1;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
let dt_end: DateTime<Local> = Local::now();
|
||||
@@ -163,10 +166,12 @@ fn import(p: &mut String, data_list: &mut PolicyDataList,
|
||||
#[allow(dead_code)]
|
||||
/// validate a given policy number
|
||||
/// result will return true or false
|
||||
fn is_valid(policy_number: &u64, policy_list: &PolicyDataList,
|
||||
policy_numbers: &mut HashMap<u64, PolicyCode>, lang: &String)
|
||||
-> Result<bool, Box<dyn std::error::Error>> {
|
||||
|
||||
fn is_valid(
|
||||
policy_number: &u64,
|
||||
policy_list: &PolicyDataList,
|
||||
policy_numbers: &mut HashMap<u64, PolicyCode>,
|
||||
lang: &String,
|
||||
) -> Result<bool, Box<dyn std::error::Error>> {
|
||||
let mut res = t!("policy.validation.started", lang);
|
||||
let mut state = t!("state.started", lang);
|
||||
let dt_start: DateTime<Local> = Local::now();
|
||||
@@ -186,7 +191,6 @@ fn is_valid(policy_number: &u64, policy_list: &PolicyDataList,
|
||||
// println!("policy_list: {:?} (with {:?} elements)",
|
||||
// policy_list.name, policy_list.policy_data.len());
|
||||
|
||||
|
||||
// policy_list.into_iter()
|
||||
// .filter(|num| matches(w, w1))
|
||||
// .clone
|
||||
@@ -230,14 +234,13 @@ fn is_valid(policy_number: &u64, policy_list: &PolicyDataList,
|
||||
match policy_numbers.get(&policy_number) {
|
||||
Some(&policy_code) => {
|
||||
let res = t!("policy.validation.success", lang);
|
||||
println!("policy_number: {} ({:?})",
|
||||
policy_number, policy_code);
|
||||
println!("policy_number: {} ({:?})", policy_number, policy_code);
|
||||
result = true;
|
||||
trace!(target: "csv-test",
|
||||
policy_number = ?policy_number,
|
||||
validation = ?res,
|
||||
policy_code = ?policy_code);
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
let res = t!("policy.validation.failed", lang);
|
||||
//println!("Noop! Number isn't valid!");
|
||||
@@ -245,9 +248,8 @@ fn is_valid(policy_number: &u64, policy_list: &PolicyDataList,
|
||||
trace!(target: "csv-test",
|
||||
policy_number = ?policy_number,
|
||||
validation = ?res);
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
let dt_end: DateTime<Local> = Local::now();
|
||||
let duration = dt_end.signed_duration_since(dt_start);
|
||||
@@ -296,9 +298,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
dotenv().ok();
|
||||
match envy::from_env::<Environment>() {
|
||||
Ok(environment) => {
|
||||
if environment.test_lang != lang { lang = environment.test_lang; }
|
||||
},
|
||||
Err(e) => { debug!(target: "csv-test", "{}", e); }
|
||||
if environment.test_lang != lang {
|
||||
lang = environment.test_lang;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
debug!(target: "csv-test", "{}", e);
|
||||
}
|
||||
}
|
||||
// how to handle unumplemented lang resources??
|
||||
res = t!("parse.environment", lang);
|
||||
@@ -331,11 +337,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut policy_data = PolicyDataList::new("Allianz-Import 20200628");
|
||||
println!("Policy Data List {:?} ", policy_data.name);
|
||||
|
||||
let mut policy_numbers : HashMap<u64, PolicyCode> = HashMap::new();
|
||||
let mut policy_numbers: HashMap<u64, PolicyCode> = HashMap::new();
|
||||
|
||||
let mut csv_import_path = v.get::<String>("import_file").unwrap();
|
||||
match import(&mut csv_import_path, &mut policy_data,
|
||||
&mut policy_numbers, &lang) {
|
||||
match import(
|
||||
&mut csv_import_path,
|
||||
&mut policy_data,
|
||||
&mut policy_numbers,
|
||||
&lang,
|
||||
) {
|
||||
Ok(count) => {
|
||||
println!("Imported {:?} records", count);
|
||||
}
|
||||
@@ -347,7 +357,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
// test if policy_number is_valid
|
||||
// type conversion (viperus String -> u64)
|
||||
let test_policy_number = v.get::<String>("test_policy_number").unwrap().parse::<u64>().unwrap();
|
||||
let test_policy_number = v
|
||||
.get::<String>("test_policy_number")
|
||||
.unwrap()
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
trace!(target: "csv-test", test_policy_number = ?test_policy_number);
|
||||
//match is_valid(&policy_number, &policy_data, &mut policy_numbers, &lang) {
|
||||
// Ok(true) => {
|
||||
@@ -356,14 +370,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
Some(&policy_code) => {
|
||||
let res = t!("policy.validation.success", lang);
|
||||
println!("{:?}", res);
|
||||
println!("policy_number: {} ({:?})",
|
||||
test_policy_number, policy_code);
|
||||
println!("policy_number: {} ({:?})", test_policy_number, policy_code);
|
||||
}
|
||||
_ => {
|
||||
let res = t!("policy.validation.failed", lang);
|
||||
println!("{:?}", res);
|
||||
//println!("Nuup! Number isn't valid!");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// export policy code elements to csv-file
|
||||
@@ -389,21 +402,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
#[test]
|
||||
fn test_policy_number() {
|
||||
// Takes a reference and returns Option<&V>
|
||||
let my_policy_numbers : [u64; 2] = [1511111111, 9999999993];
|
||||
let my_policy_numbers: [u64; 2] = [1511111111, 9999999993];
|
||||
assert_eq!(my_policy_numbers, [1511111111, 9999999993]);
|
||||
|
||||
//let mut csv_import_path = v.get::<String>("import_file").unwrap();
|
||||
let mut csv_import_path = String::from("data/POLLFNR_TEST.txt");
|
||||
|
||||
let mut policy_data = PolicyDataList::new("PolicyDataList");
|
||||
let mut policy_numbers : HashMap<u64, PolicyCode> = HashMap::new();
|
||||
let mut policy_numbers: HashMap<u64, PolicyCode> = HashMap::new();
|
||||
let lang = "en".to_string();
|
||||
|
||||
println!("import with Path: {:?} PolicyData: {:?} PolicyNumbers: {:?}, Lang: {:?}",
|
||||
csv_import_path, policy_data, policy_numbers, lang);
|
||||
let count = import(&mut csv_import_path, &mut policy_data,
|
||||
&mut policy_numbers, &lang);
|
||||
println!(
|
||||
"import with Path: {:?} PolicyData: {:?} PolicyNumbers: {:?}, Lang: {:?}",
|
||||
csv_import_path, policy_data, policy_numbers, lang
|
||||
);
|
||||
let count = import(
|
||||
&mut csv_import_path,
|
||||
&mut policy_data,
|
||||
&mut policy_numbers,
|
||||
&lang,
|
||||
);
|
||||
|
||||
assert_eq!(count.unwrap(), 15498);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user