csv-test: integrate HashMap functionality
Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
@@ -11,9 +11,10 @@ use locales::t;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::{error::Error, process};
|
use std::{error::Error, process};
|
||||||
|
use std::collections::HashMap;
|
||||||
use tracing::{debug, trace, Level};
|
use tracing::{debug, trace, Level};
|
||||||
|
|
||||||
use advotracker::data::{PolicyList, PolicyDataList, PolicyData};
|
use advotracker::data::{PolicyCode, PolicyList, PolicyDataList, PolicyData};
|
||||||
|
|
||||||
// include modules
|
// include modules
|
||||||
mod parse_args;
|
mod parse_args;
|
||||||
@@ -81,7 +82,8 @@ fn export(p: &mut String, lang: &String) -> Result<usize, Box<dyn Error>> {
|
|||||||
/// import from csv format
|
/// import from csv format
|
||||||
/// https://docs.rs/csv/1.1.3/csv/cookbook/index.html
|
/// https://docs.rs/csv/1.1.3/csv/cookbook/index.html
|
||||||
/// https://blog.burntsushi.net/csv/
|
/// https://blog.burntsushi.net/csv/
|
||||||
fn import(p: &mut String, data_list: &mut PolicyDataList, lang: &String) -> Result<usize, Box<dyn Error>> {
|
fn import(p: &mut String, data_list: &mut PolicyDataList,
|
||||||
|
policy_numbers: &mut HashMap<usize, PolicyCode>, lang: &String) -> Result<usize, Box<dyn Error>> {
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
@@ -139,9 +141,12 @@ fn import(p: &mut String, data_list: &mut PolicyDataList, lang: &String) -> Resu
|
|||||||
// The iterator yields Result<StringRecord, Error>, so we check the
|
// The iterator yields Result<StringRecord, Error>, so we check the
|
||||||
// error here.
|
// error here.
|
||||||
let record: PolicyData = result?;
|
let record: PolicyData = result?;
|
||||||
println!("{:?}", record);
|
//println!("{:?}", record);
|
||||||
|
|
||||||
// WIP: write to redis backend
|
// WIP: write to redis backend
|
||||||
|
// append the policy_number to the HashMap
|
||||||
|
policy_numbers.insert(record.policy_number, record.policy_code);
|
||||||
|
|
||||||
// push record as new vector elements
|
// push record as new vector elements
|
||||||
data_list.push(record);
|
data_list.push(record);
|
||||||
|
|
||||||
@@ -168,7 +173,9 @@ fn import(p: &mut String, data_list: &mut PolicyDataList, lang: &String) -> Resu
|
|||||||
|
|
||||||
/// validate a given policy number
|
/// validate a given policy number
|
||||||
/// result will return true or false
|
/// result will return true or false
|
||||||
fn is_valid(policy_number: &usize, policy_list: &PolicyDataList, lang: &String) -> Result<(), Box<dyn std::error::Error>> {
|
fn is_valid(policy_number: &usize, policy_list: &PolicyDataList,
|
||||||
|
policy_numbers: &mut HashMap<usize, PolicyCode>, lang: &String)
|
||||||
|
-> Result<bool, Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
let mut res = t!("policy.validation.started", lang);
|
let mut res = t!("policy.validation.started", lang);
|
||||||
let mut state = t!("state.started", lang);
|
let mut state = t!("state.started", lang);
|
||||||
@@ -181,17 +188,73 @@ fn is_valid(policy_number: &usize, policy_list: &PolicyDataList, lang: &String)
|
|||||||
policy_list = ?policy_list.name,
|
policy_list = ?policy_list.name,
|
||||||
elements = ?policy_list.policy_data.len(),
|
elements = ?policy_list.policy_data.len(),
|
||||||
date_start = ?dt_start.to_string());
|
date_start = ?dt_start.to_string());
|
||||||
|
println!("Policy_number list: {:?}", policy_list.policy_data);
|
||||||
|
|
||||||
// println!("policy_number: {:?}", policy_number);
|
// WIP: testcode to collect policy_number via iterator
|
||||||
|
//println!("policy_number: {:?}", policy_list.policy_data[1].policy_number);
|
||||||
|
//println!("Policy_number list: {:?}", policy_list.policy_data);
|
||||||
// println!("policy_list: {:?} (with {:?} elements)",
|
// println!("policy_list: {:?} (with {:?} elements)",
|
||||||
// policy_list.name, policy_list.policy_data.len());
|
// policy_list.name, policy_list.policy_data.len());
|
||||||
|
|
||||||
let test: Vec<_> = vec!["one", "two", "three"];
|
|
||||||
let index: usize = test.iter().enumerate().find(|&r| r.1.to_string() == "two".to_string()).unwrap().0;
|
// policy_list.into_iter()
|
||||||
println!("index: {:?} -> {:?}", index, test[index]);
|
// .filter(|num| matches(w, w1))
|
||||||
|
// .clone
|
||||||
|
// .collect::<Vec<policy_number>>()
|
||||||
|
// let my_num = policy_list.policy_data.iter()
|
||||||
|
// .map(|policy_number| {
|
||||||
|
// policy_number
|
||||||
|
// })
|
||||||
|
// .collect::<Vec<_>>();
|
||||||
|
|
||||||
|
//println!("My policy_numbers: {:?}", my_num);
|
||||||
|
|
||||||
|
// let mut my_policy_list = [
|
||||||
|
// [ ("Jack", 20), ("Jane", 23), ("Jill", 18), ("John", 19), ],
|
||||||
|
// [ ("Bill", 17), ("Brenda", 16), ("Brad", 18), ("Barbara", 17), ]
|
||||||
|
// ];
|
||||||
|
|
||||||
|
// let teams_in_score_order = teams
|
||||||
|
// .iter_mut()
|
||||||
|
// .map(|team| {
|
||||||
|
// team.sort_by(|&a, &b| a.1.cmp(&b.1).reverse());
|
||||||
|
// team
|
||||||
|
// })
|
||||||
|
// .collect::<Vec<_>>();
|
||||||
|
|
||||||
|
//println!("Teams: {:?}", teams_in_score_order);
|
||||||
|
|
||||||
|
// if policy_list.policy_data.iter().any(|v| v == policy_number) {
|
||||||
|
// println!("{:?} contains {}", policy_list.name, policy_number);
|
||||||
|
// } else {
|
||||||
|
// println!("{:?} doesn't contain {}", policy_list, policy_number);
|
||||||
|
//}
|
||||||
|
|
||||||
|
// let test: Vec<_> = vec!["one", "two", "three"];
|
||||||
|
// let index: usize = test.iter().enumerate().find(|&r| r.1.to_string() == "two".to_string()).unwrap().0;
|
||||||
|
// println!("index: {:?} -> {:?}", index, test[index]);
|
||||||
|
|
||||||
//let index: usize = test.iter().enumerate().find(|&r| r.policy_number == "two".to_string()).unwrap().0;
|
//let index: usize = test.iter().enumerate().find(|&r| r.policy_number == "two".to_string()).unwrap().0;
|
||||||
|
|
||||||
|
let result = false;
|
||||||
|
match policy_numbers.get(&policy_number) {
|
||||||
|
Some(&policy_code) => {
|
||||||
|
let mut res = t!("policy.validation.success", lang);
|
||||||
|
println!("policy_number: {} ({:?})",
|
||||||
|
policy_number, policy_code);
|
||||||
|
result = true;
|
||||||
|
},
|
||||||
|
_ => {
|
||||||
|
let mut res = t!("policy.validation.failed", lang);
|
||||||
|
println!("Nuup! Number isn't valid!");
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
trace!(target: "csv-test",
|
||||||
|
validation = ?res,
|
||||||
|
policy_number = ?policy_number,
|
||||||
|
policy_code = ?policy_code);
|
||||||
|
|
||||||
let dt_end: DateTime<Local> = Local::now();
|
let dt_end: DateTime<Local> = Local::now();
|
||||||
let duration = dt_end.signed_duration_since(dt_start);
|
let duration = dt_end.signed_duration_since(dt_start);
|
||||||
|
|
||||||
@@ -200,11 +263,10 @@ fn is_valid(policy_number: &usize, policy_list: &PolicyDataList, lang: &String)
|
|||||||
trace!(target: "csv-test",
|
trace!(target: "csv-test",
|
||||||
process = ?res,
|
process = ?res,
|
||||||
state = ?state,
|
state = ?state,
|
||||||
date_stop = ?dt_end.to_string());
|
date_stop = ?dt_end.to_string(),
|
||||||
|
duration = ?duration);
|
||||||
|
|
||||||
trace!(target: "csv-test", duration = ?duration);
|
Ok(result)
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
@@ -279,8 +341,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let mut policy_data = PolicyDataList::new("Allianz-Import 20200628");
|
let mut policy_data = PolicyDataList::new("Allianz-Import 20200628");
|
||||||
println!("Policy Data List {:?} ", policy_data.name);
|
println!("Policy Data List {:?} ", policy_data.name);
|
||||||
|
|
||||||
|
let mut policy_numbers : HashMap<usize, PolicyCode> = HashMap::new();
|
||||||
|
|
||||||
let mut csv_import_path = v.get::<String>("import_file").unwrap();
|
let mut csv_import_path = v.get::<String>("import_file").unwrap();
|
||||||
match import(&mut csv_import_path, &mut policy_data, &lang) {
|
match import(&mut csv_import_path, &mut policy_data,
|
||||||
|
&mut policy_numbers, &lang) {
|
||||||
Ok(count) => {
|
Ok(count) => {
|
||||||
println!("Imported {:?} records", count);
|
println!("Imported {:?} records", count);
|
||||||
}
|
}
|
||||||
@@ -291,8 +356,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test if policy_number is_valid
|
// test if policy_number is_valid
|
||||||
let policy_number : usize = 9999999999;
|
let policy_number : usize = 1999999999;
|
||||||
let _res = is_valid(&policy_number, &policy_data, &lang);
|
let _res = is_valid(&policy_number, &policy_data, &mut policy_numbers, &lang);
|
||||||
println!("given policy_number {:?} is valid!", policy_number);
|
println!("given policy_number {:?} is valid!", policy_number);
|
||||||
|
|
||||||
// export policy code elements to csv-file
|
// export policy code elements to csv-file
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ pub fn parse_args(v: &mut Viperus) -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
// preset default key/value pairs (lowest priority)
|
// preset default key/value pairs (lowest priority)
|
||||||
v.add_default("config_file", String::from("csv_import.ron"));
|
v.add_default("config_file", String::from("csv_import.ron"));
|
||||||
//v.add_default("import_file", String::from("POLLFNR_WOECHENTLICH.txt"));
|
//v.add_default("import_file", String::from("POLLFNR_WOECHENTLICH.txt"));
|
||||||
v.add_default("import_file", String::from("POLLFNR_TEST.txt"));
|
v.add_default("import_file", String::from("POLLFNR_MINI.txt"));
|
||||||
v.add_default("export_file", String::from(""));
|
v.add_default("export_file", String::from(""));
|
||||||
v.add_default("to_email_address_file", String::from("Allianz RA-Hotline <smr-rahotline@allianz.de>"));
|
v.add_default("to_email_address_file", String::from("Allianz RA-Hotline <smr-rahotline@allianz.de>"));
|
||||||
v.add_default("from_email_address_file", String::from("Allianz-Hotline RA-Hiedemann <azhotline@hiedemann.de>"));
|
v.add_default("from_email_address_file", String::from("Allianz-Hotline RA-Hiedemann <azhotline@hiedemann.de>"));
|
||||||
@@ -65,7 +65,7 @@ SMR Unerledigt: 089 92529 60222")
|
|||||||
.long("importFile")
|
.long("importFile")
|
||||||
.help("Select source file for the csv-import")
|
.help("Select source file for the csv-import")
|
||||||
//.default_value("POLLFNR_WOECHENTLICH.txt")
|
//.default_value("POLLFNR_WOECHENTLICH.txt")
|
||||||
.default_value("POLLFNR_TEST.txt")
|
.default_value("POLLFNR_MINI.txt")
|
||||||
.takes_value(true),
|
.takes_value(true),
|
||||||
)
|
)
|
||||||
.arg(
|
.arg(
|
||||||
|
|||||||
Reference in New Issue
Block a user