fix: moved the read csv into teh lib as its shared between two methods

This commit is contained in:
silver 2023-07-30 00:55:27 +01:00
parent e21af089d1
commit 88abff575f
3 changed files with 37 additions and 64 deletions

View file

@ -251,3 +251,38 @@ async fn update_accounts(pool: &Pool<Sqlite>, config: &Config) {
// done with ldap
ldap.unbind().unwrap();
}
#[derive(Debug, serde::Deserialize)]
pub struct Record {
#[serde(rename = "MemID")]
pub mem_id: String,
#[serde(rename = "Student Num")]
pub id_student: String,
#[serde(rename = "Contact Email")]
pub email: String,
#[serde(rename = "Expiry")]
pub expiry: String,
#[serde(rename = "First Name")]
pub name_first: String,
#[serde(rename = "Last Name")]
pub name_second: String,
}
pub fn read_csv(config: &Config) -> Result<Vec<Record>, Box<dyn std::error::Error>> {
let mut records: Vec<Record> = vec![];
if let Ok(mut rdr) = csv::Reader::from_path(format!("{}/{}", &config.home, &config.csv)) {
for result in rdr.deserialize() {
// Notice that we need to provide a type hint for automatic
// deserialization.
let record: Record = result?;
if record.mem_id.is_empty() {
continue;
}
records.push(record);
}
}
Ok(records)
}