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

@ -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<Vec<Record>, Box<dyn 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)
}
async fn check(db: &Pool<Sqlite>, mail: &str) -> bool {
check_pending(db, mail).await && check_users(db, mail).await
}

View file

@ -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<HashSet<String>, Box<dyn Error>> {
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<Vec<Record>, Box<dyn 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?;
records.push(record);
}
}
Ok(records)
}

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)
}