Thank you all for your help 
I also think that regex might be an overkill in this case^^
@jofas The match statement looks good to me!
Regarding short circuiting: You guys are right, that's a bug. I've replaced it with ||
@BurntSushi
Thank you very much for the offering! I do not think that the performance loss will be an issue, but for the sake of my education, I've crafted a minimal example that covers the gist of my code.
Disclaimer: I know that the data structures and function signatures are far from optimal. I've tried to keep this example as minimal as possible. And I am new to rust
Also, error handling is out of scope
A little bit of context: The csv data contains sensor data, and the sensors are identified by their name in the description field. For each sensor (or group of sensors) an appropriate calibration is performed, and then the average value is stored togehter with its date. The real application is a bit more complex, but no cuputation-heavy stuff. Therefore, I think this should suffice as an example.
Thanks again!
Regards,
Ted
use serde::Deserialize;
use chrono::NaiveDate;
use chrono::Datelike;
#[derive(Debug, Deserialize)]
struct Record {
date: String,
description: String,
value_1: f64,
value_2: f64,
value_3: f64,
value_4: f64,
}
struct Data {
date_t: Vec<String>,
date_p: Vec<String>,
temp: Vec<f64>,
pressure: Vec<f64>,
}
fn process_temp(v1: f64, v2: f64, v3: f64, v4: f64) -> f64 {
(v1 * 2. + v2 * 2. + v3 * 2. + v4 * 2.)/4.
}
fn process_pressure_1(v1: f64, v2: f64, v3: f64, v4: f64) -> f64 {
(v1 * 5. + v2 * 5. + v3 * 5. + v4 * 5.)/4.
}
fn process_pressure_2(v1: f64, v2: f64, v3: f64, v4: f64) -> f64 {
(v1 * 3. + v2 * 3. + v3 * 3. + v4 * 3.)/4.
}
fn main() {
let csv_data = "date;description;value_1;value_2;value_3;value_4
13.04.2023;T_1;23.5;24.8;23.7;24.0
13.04.2023;T_2;22.5;22.8;22.7;22.0
13.04.2023;p_1;4.0;4.1;4.1;3.9
13.04.2023;p_2;4.4;4.2;4.3;4.2";
let mut rdr = csv::ReaderBuilder::new()
.has_headers(true)
.delimiter(b';')
.from_reader(csv_data.as_bytes());
let mut data = Data {
date_t: vec![],
date_p: vec![],
temp: vec![],
pressure: vec![],
};
for (result) in rdr.deserialize() {
let record: Record = result.unwrap();
let naive_date = NaiveDate::parse_from_str(&record.date, "%d.%m.%Y").unwrap();
let month = naive_date.month();
let year = naive_date.year();
let string = record.description.to_lowercase();
if string.contains("t_1") || string.contains("t_2") {
data.temp.push(process_temp(record.value_1, record.value_2, record.value_3, record.value_4));
data.date_t.push(format!("{}.{}", month, year));
} else if string.contains("p_1") {
data.pressure.push(process_pressure_1(record.value_1, record.value_2, record.value_3, record.value_4));
data.date_p.push(format!("{}.{}", month, year));
} else if string.contains("p_2") {
data.pressure.push(process_pressure_2(record.value_1, record.value_2, record.value_3, record.value_4));
data.date_p.push(format!("{}.{}", month, year));
} else {
println!("Unexpected value");
}
}
for i in 0..data.date_t.len() {
println!("{} - {}", data.date_t[i], data.temp[i]);
}
for i in 0..data.date_p.len() {
println!("{} - {}", data.date_p[i], data.pressure[i]);
}
}