From e21af089d1b0d8c211c54ce7399f19ef73857fe3 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Sun, 30 Jul 2023 00:48:15 +0100 Subject: [PATCH] fmt: clippy and fmt --- src/bin/new_users.rs | 135 +++++++++++++++++-------------------------- src/lib.rs | 26 +++------ 2 files changed, 62 insertions(+), 99 deletions(-) diff --git a/src/bin/new_users.rs b/src/bin/new_users.rs index c490c3d..6cac88c 100644 --- a/src/bin/new_users.rs +++ b/src/bin/new_users.rs @@ -1,18 +1,15 @@ use chrono::{DateTime, SecondsFormat, Utc}; -use skynet_ldap_backend::{get_config, Config, db_init, Accounts, AccountsNew}; -use std::error::Error; -use sqlx::{Pool, Sqlite}; -use rand::{thread_rng, Rng}; -use rand::distributions::Alphanumeric; use lettre::{ message::{header, MultiPart, SinglePart}, - SmtpTransport, Message, Transport, - transport::smtp::{ - authentication::Credentials, - response::Response - }, + transport::smtp::{authentication::Credentials, response::Response}, + Message, SmtpTransport, Transport, }; use maud::html; +use rand::distributions::Alphanumeric; +use rand::{thread_rng, Rng}; +use skynet_ldap_backend::{db_init, get_config, Accounts, AccountsNew, Config}; +use sqlx::{Pool, Sqlite}; +use std::error::Error; #[async_std::main] async fn main() { @@ -20,34 +17,31 @@ async fn main() { let db = db_init(&config).await.unwrap(); let now = Utc::now(); - if let Ok(records) = read_csv(&config){ + if let Ok(records) = read_csv(&config) { for record in records { // check if the email is already in the db if !check(&db, &record.email).await { continue; } - + // generate a auth key let auth = generate_auth(); - + match send_mail(&config, &record, &auth) { - Ok(_) => { - match save_to_db(&db, now, &record, &auth).await { - Ok(_) => {} - Err(e) => { - println!("Unable to save to db {} {e:?}", &record.email); - } + Ok(_) => match save_to_db(&db, now, &record, &auth).await { + Ok(_) => {} + Err(e) => { + println!("Unable to save to db {} {e:?}", &record.email); + } + }, + Err(e) => { + println!("Unable to send mail to {} {e:?}", &record.email); } - } - Err(e) => { - println!("Unable to send mail to {} {e:?}", &record.email); - } } } } } - #[derive(Debug, serde::Deserialize)] struct Record { #[serde(rename = "MemID")] @@ -72,7 +66,7 @@ fn read_csv(config: &Config) -> Result, Box> { // Notice that we need to provide a type hint for automatic // deserialization. let record: Record = result?; - if record.mem_id == "" { + if record.mem_id.is_empty() { continue; } records.push(record); @@ -93,11 +87,11 @@ async fn check_users(db: &Pool, mail: &str) -> bool { WHERE mail == ? "#, ) - .bind(mail) - .fetch_all(db) - .await - .unwrap_or(vec![]) - .is_empty() + .bind(mail) + .fetch_all(db) + .await + .unwrap_or(vec![]) + .is_empty() } async fn check_pending(db: &Pool, mail: &str) -> bool { sqlx::query_as::<_, Accounts>( @@ -107,20 +101,16 @@ async fn check_pending(db: &Pool, mail: &str) -> bool { WHERE mail == ? "#, ) - .bind(mail) - .fetch_all(db) - .await - .unwrap_or(vec![]) - .is_empty() + .bind(mail) + .fetch_all(db) + .await + .unwrap_or(vec![]) + .is_empty() } // from https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html#create-random-passwords-from-a-set-of-alphanumeric-characters fn generate_auth() -> String { - thread_rng() - .sample_iter(&Alphanumeric) - .take(30) - .map(char::from) - .collect() + thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect() } // using https://github.com/lettre/lettre/blob/57886c367d69b4d66300b322c94bd910b1eca364/examples/maud_html.rs @@ -145,7 +135,7 @@ fn send_mail(config: &Config, record: &Record, auth: &str) -> Result Result Result Result, now: DateTime, record: &Record, auth: &str) -> Result, sqlx::Error> { +async fn save_to_db(db: &Pool, now: DateTime, record: &Record, auth: &str) -> Result, sqlx::Error> { sqlx::query_as::<_, AccountsNew>( " INSERT OR REPLACE INTO accounts_new (mail, auth_code, date_iso, date_expiry, name_first, name_surname) VALUES (?1, ?2, ?3, ?4, ?5, ?6) ", ) - .bind(record.email.to_owned()) - .bind(auth.to_owned()) - .bind(now.to_rfc3339_opts(SecondsFormat::Millis, true)) - .bind(record.expiry.to_owned()) - .bind(record.name_first.to_owned()) - .bind(record.name_second.to_owned()) - .fetch_optional(db) - .await + .bind(record.email.to_owned()) + .bind(auth.to_owned()) + .bind(now.to_rfc3339_opts(SecondsFormat::Millis, true)) + .bind(record.expiry.to_owned()) + .bind(record.name_first.to_owned()) + .bind(record.name_second.to_owned()) + .fetch_optional(db) + .await } - - - - - diff --git a/src/lib.rs b/src/lib.rs index 7bd606c..f205c38 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ pub struct Accounts { mail: String, student_id: String, enabled: bool, - secure: bool, + secure: bool, } pub async fn db_init(config: &Config) -> Result, Error> { @@ -74,12 +74,12 @@ pub async fn db_init(config: &Config) -> Result, Error> { name_surname integer not null )", ) - .execute(&pool) - .await?; + .execute(&pool) + .await?; sqlx::query("CREATE INDEX IF NOT EXISTS index_auth_code ON accounts_new (auth_code)") - .execute(&pool) - .await?; + .execute(&pool) + .await?; // this is for active use sqlx::query( @@ -96,9 +96,7 @@ pub async fn db_init(config: &Config) -> Result, Error> { .execute(&pool) .await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)") - .execute(&pool) - .await?; + sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)").execute(&pool).await?; update_accounts(&pool, config).await; @@ -190,12 +188,7 @@ async fn update_accounts(pool: &Pool, config: &Config) { ldap.simple_bind(&config.ldap_admin, &config.ldap_admin_pw).unwrap().success().unwrap(); // use this to pre load a large chunk of data - if let Ok(x) = ldap.search( - "ou=users,dc=skynet,dc=ie", - Scope::OneLevel, - "(objectClass=*)", - vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "skSecure"] - ) { + if let Ok(x) = ldap.search("ou=users,dc=skynet,dc=ie", Scope::OneLevel, "(objectClass=*)", vec!["uid", "uidNumber", "skDiscord", "skMemberOf", "mail", "skID", "skSecure"]) { if let Ok((rs, _res)) = x.success() { for entry in rs { let tmp = SearchEntry::construct(entry); @@ -226,10 +219,7 @@ async fn update_accounts(pool: &Pool, config: &Config) { if tmp.attrs.contains_key("skID") && !tmp.attrs["skID"].is_empty() { tmp_account.student_id = tmp.attrs["skID"][0].clone(); } - if tmp.attrs.contains_key("skMemberOf") - && !tmp.attrs["skMemberOf"].is_empty() - && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie")) - { + if tmp.attrs.contains_key("skMemberOf") && !tmp.attrs["skMemberOf"].is_empty() && tmp.attrs["skMemberOf"].contains(&String::from("cn=skynet-users-linux,ou=groups,dc=skynet,dc=ie")) { tmp_account.enabled = true; } if tmp.attrs.contains_key("skSecure") && !tmp.attrs["skSecure"].is_empty() {