From 88abff575f4226545c8fada5d846e39a7284d05d Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 30 Jul 2023 00:55:27 +0100 Subject: [PATCH] fix: moved the read csv into teh lib as its shared between two methods --- src/bin/new_users.rs | 37 +------------------------------------ src/bin/update_groups.rs | 29 +---------------------------- src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 64 deletions(-) diff --git a/src/bin/new_users.rs b/src/bin/new_users.rs index 6cac88c..b723b43 100644 --- a/src/bin/new_users.rs +++ b/src/bin/new_users.rs @@ -7,9 +7,8 @@ use lettre::{ use maud::html; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; -use skynet_ldap_backend::{db_init, get_config, Accounts, AccountsNew, Config}; +use skynet_ldap_backend::{db_init, get_config, Accounts, AccountsNew, Config, read_csv, Record}; use sqlx::{Pool, Sqlite}; -use std::error::Error; #[async_std::main] async fn main() { @@ -42,40 +41,6 @@ async fn main() { } } -#[derive(Debug, serde::Deserialize)] -struct Record { - #[serde(rename = "MemID")] - mem_id: String, - #[serde(rename = "Student Num")] - id_student: String, - #[serde(rename = "Contact Email")] - email: String, - #[serde(rename = "Expiry")] - expiry: String, - #[serde(rename = "First Name")] - name_first: String, - #[serde(rename = "Last Name")] - name_second: String, -} - -fn read_csv(config: &Config) -> Result, Box> { - let mut records: Vec = 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) -} - async fn check(db: &Pool, mail: &str) -> bool { check_pending(db, mail).await && check_users(db, mail).await } diff --git a/src/bin/update_groups.rs b/src/bin/update_groups.rs index c671bbd..c97a951 100644 --- a/src/bin/update_groups.rs +++ b/src/bin/update_groups.rs @@ -1,7 +1,7 @@ use chrono::{Datelike, Utc}; use dotenvy::dotenv; use ldap3::{LdapConn, Mod, Scope, SearchEntry}; -use skynet_ldap_backend::{get_config, Config}; +use skynet_ldap_backend::{get_config, Config, read_csv}; use std::collections::{HashMap, HashSet}; use std::env; use std::error::Error; @@ -202,30 +202,3 @@ async fn from_csv(config: &Config) -> Result, Box> { Ok(uids) } - -#[derive(Debug, serde::Deserialize)] -struct Record { - // #[serde(rename = "MemID")] - // id_wolves: String, - #[serde(rename = "Student Num")] - id_student: String, - #[serde(rename = "Contact Email")] - email: String, - #[serde(rename = "Expiry")] - expiry: String, -} - -fn read_csv(config: &Config) -> Result, Box> { - let mut records: Vec = 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?; - records.push(record); - } - } - - Ok(records) -} diff --git a/src/lib.rs b/src/lib.rs index f205c38..9756089 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -251,3 +251,38 @@ async fn update_accounts(pool: &Pool, 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, Box> { + let mut records: Vec = 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) +} \ No newline at end of file