diff options
author | Joris | 2023-04-17 21:10:48 +0200 |
---|---|---|
committer | Joris | 2023-04-17 21:10:48 +0200 |
commit | 459016e70dd4933a8082d27748097de81a3e53ff (patch) | |
tree | 0ed14ecc8762d62a55bf8c05356bcc984b1a0cea | |
parent | 3932daa26360d6e03807381d0b8ffa2d0e704847 (diff) |
Follow clippy indications
-rw-r--r-- | src/assets.rs | 2 | ||||
-rw-r--r-- | src/controller/balance.rs | 11 | ||||
-rw-r--r-- | src/controller/login.rs | 2 | ||||
-rw-r--r-- | src/controller/utils.rs | 2 | ||||
-rw-r--r-- | src/db/incomes.rs | 4 | ||||
-rw-r--r-- | src/db/jobs.rs | 7 | ||||
-rw-r--r-- | src/db/payments.rs | 11 | ||||
-rw-r--r-- | src/jobs/jobs.rs | 28 | ||||
-rw-r--r-- | src/jobs/mod.rs | 29 | ||||
-rw-r--r-- | src/mail.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 8 | ||||
-rw-r--r-- | src/payer.rs | 4 | ||||
-rw-r--r-- | src/queries.rs | 58 | ||||
-rw-r--r-- | src/routes.rs | 3 | ||||
-rw-r--r-- | src/templates.rs | 6 | ||||
-rw-r--r-- | src/utils/text.rs | 4 |
16 files changed, 75 insertions, 106 deletions
diff --git a/src/assets.rs b/src/assets.rs index dc46c78..80f9630 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -7,7 +7,7 @@ pub fn get() -> HashMap<String, String> { let paths = fs::read_dir("assets").unwrap().map(|e| { let path = format!("{}", e.unwrap().path().display()); let file = fs::read(&path).unwrap(); - let mut path_iter = path.split("/"); + let mut path_iter = path.split('/'); path_iter.next(); let name = path_iter.collect::<Vec<&str>>().join("/"); let hashed = format!("/assets/{}/{}", sha256(file), name); diff --git a/src/controller/balance.rs b/src/controller/balance.rs index 228ff04..adde8a9 100644 --- a/src/controller/balance.rs +++ b/src/controller/balance.rs @@ -24,8 +24,7 @@ pub async fn get(wallet: &Wallet) -> Response<Body> { let user_payments = db::payments::repartition(&wallet.pool).await; let template_user_payments = get_template_user_payments(&users, &user_payments); - let total_payments: i64 = - user_payments.clone().into_iter().map(|p| p.1).sum(); + let total_payments: i64 = user_payments.iter().map(|p| p.1).sum(); let exceeding_payers = payer::exceeding(&users, &user_incomes, &user_payments); @@ -47,11 +46,11 @@ pub async fn get(wallet: &Wallet) -> Response<Body> { } fn get_template_user_payments( - users: &Vec<User>, + users: &[User], user_payments: &HashMap<i64, i64>, ) -> Vec<(String, i64)> { let mut user_payments: Vec<(String, i64)> = users - .into_iter() + .iter() .map(|u| (u.name.clone(), *user_payments.get(&u.id).unwrap_or(&0))) .collect(); user_payments.sort_by_key(|i| i.1); @@ -59,11 +58,11 @@ fn get_template_user_payments( } fn get_template_user_incomes( - users: &Vec<User>, + users: &[User], user_incomes: &HashMap<i64, i64>, ) -> Vec<(String, i64)> { let mut user_incomes: Vec<(String, i64)> = users - .into_iter() + .iter() .map(|u| (u.name.clone(), *user_incomes.get(&u.id).unwrap_or(&0))) .collect(); user_incomes.sort_by_key(|i| i.1); diff --git a/src/controller/login.rs b/src/controller/login.rs index ea9db57..09a2786 100644 --- a/src/controller/login.rs +++ b/src/controller/login.rs @@ -81,6 +81,6 @@ pub async fn logout(config: Config, wallet: &Wallet) -> Response<Body> { if db::users::remove_login_token(&wallet.pool, wallet.user.id).await { utils::with_logout_cookie(config, utils::redirect("/")) } else { - error::error(&wallet, "Erreur serveur", "Erreur serveur") + error::error(wallet, "Erreur serveur", "Erreur serveur") } } diff --git a/src/controller/utils.rs b/src/controller/utils.rs index bd3007e..26db765 100644 --- a/src/controller/utils.rs +++ b/src/controller/utils.rs @@ -71,7 +71,7 @@ pub fn template( path: &str, context: Context, ) -> Response<Body> { - let mut context = context.clone(); + let mut context = context; context.insert("assets", assets); let response = match templates.render(path, &context) { diff --git a/src/db/incomes.rs b/src/db/incomes.rs index 2952d62..e5c3164 100644 --- a/src/db/incomes.rs +++ b/src/db/incomes.rs @@ -18,7 +18,7 @@ WHERE incomes.deleted_at IS NULL "#; - let res = sqlx::query(&query) + let res = sqlx::query(query) .map(|row: SqliteRow| row.get("count")) .fetch_one(pool) .await; @@ -169,7 +169,7 @@ WHERE AND deleted_at IS NULL "#; - let res = sqlx::query(&query) + let res = sqlx::query(query) .bind(user_id) .bind(date) .map(|row: SqliteRow| row.get("id")) diff --git a/src/db/jobs.rs b/src/db/jobs.rs index 88c2005..3f84fc8 100644 --- a/src/db/jobs.rs +++ b/src/db/jobs.rs @@ -34,7 +34,7 @@ WHERE } } -pub async fn actualize_last_execution(pool: &SqlitePool, job: Job) -> () { +pub async fn actualize_last_execution(pool: &SqlitePool, job: Job) { let query = r#" UPDATE jobs @@ -48,9 +48,6 @@ WHERE match res { Ok(_) => (), - Err(err) => { - error!("Error actualizing job last execution: {:?}", err); - () - } + Err(err) => error!("Error actualizing job last execution: {:?}", err), } } diff --git a/src/db/payments.rs b/src/db/payments.rs index 0082736..bebc69d 100644 --- a/src/db/payments.rs +++ b/src/db/payments.rs @@ -515,7 +515,7 @@ LIMIT 1 "#; - let res = sqlx::query(&query).bind(category_id).fetch_one(pool).await; + let res = sqlx::query(query).bind(category_id).fetch_one(pool).await; match res { Ok(_) => true, @@ -552,7 +552,7 @@ LEFT OUTER JOIN ( ON users.id = payments.user_id"#; - let res = sqlx::query(&query) + let res = sqlx::query(query) .map(|row: SqliteRow| (row.get("user_id"), row.get("sum"))) .fetch_all(pool) .await; @@ -566,7 +566,7 @@ ON } } -pub async fn create_monthly_payments(pool: &SqlitePool) -> () { +pub async fn create_monthly_payments(pool: &SqlitePool) { let query = r#" INSERT INTO payments(name, cost, user_id, category_id, date, frequency) @@ -588,10 +588,7 @@ WHERE match res { Ok(_) => (), - Err(err) => { - error!("Error creating monthly payments: {:?}", err); - () - } + Err(err) => error!("Error creating monthly payments: {:?}", err), } } diff --git a/src/jobs/jobs.rs b/src/jobs/jobs.rs deleted file mode 100644 index 536ff97..0000000 --- a/src/jobs/jobs.rs +++ /dev/null @@ -1,28 +0,0 @@ -use sqlx::sqlite::SqlitePool; -use tera::Tera; -use tokio::time::{sleep, Duration}; - -use crate::db; -use crate::jobs::weekly_report; -use crate::model::config::Config; -use crate::model::job::Job; - -pub async fn start(config: Config, pool: SqlitePool, templates: Tera) -> () { - loop { - if db::jobs::should_run(&pool, Job::WeeklyReport).await { - info!("Starting weekly report job"); - if weekly_report::send(&config, &pool, &templates).await { - db::jobs::actualize_last_execution(&pool, Job::WeeklyReport) - .await; - } - } - if db::jobs::should_run(&pool, Job::MonthlyPayment).await { - info!("Starting monthly payment job"); - db::payments::create_monthly_payments(&pool).await; - db::jobs::actualize_last_execution(&pool, Job::MonthlyPayment) - .await; - } - // Sleeping 8 hours - sleep(Duration::from_secs(8 * 60 * 60)).await; - } -} diff --git a/src/jobs/mod.rs b/src/jobs/mod.rs index be2ddac..5f95597 100644 --- a/src/jobs/mod.rs +++ b/src/jobs/mod.rs @@ -1,2 +1,29 @@ -pub mod jobs; mod weekly_report; + +use sqlx::sqlite::SqlitePool; +use tera::Tera; +use tokio::time::{sleep, Duration}; + +use crate::db; +use crate::model::config::Config; +use crate::model::job::Job; + +pub async fn start(config: Config, pool: SqlitePool, templates: Tera) { + loop { + if db::jobs::should_run(&pool, Job::WeeklyReport).await { + info!("Starting weekly report job"); + if weekly_report::send(&config, &pool, &templates).await { + db::jobs::actualize_last_execution(&pool, Job::WeeklyReport) + .await; + } + } + if db::jobs::should_run(&pool, Job::MonthlyPayment).await { + info!("Starting monthly payment job"); + db::payments::create_monthly_payments(&pool).await; + db::jobs::actualize_last_execution(&pool, Job::MonthlyPayment) + .await; + } + // Sleeping 8 hours + sleep(Duration::from_secs(8 * 60 * 60)).await; + } +} diff --git a/src/mail.rs b/src/mail.rs index 149a5ef..dec8691 100644 --- a/src/mail.rs +++ b/src/mail.rs @@ -89,7 +89,7 @@ fn format_address(name: String, address: String) -> String { format!("{} <{}>", name, address) } -async fn spawn(mut command: Command, stdin: &Vec<u8>) -> Result<Output, Error> { +async fn spawn(mut command: Command, stdin: &[u8]) -> Result<Output, Error> { let mut process = command.spawn()?; process .stdin diff --git a/src/main.rs b/src/main.rs index 99c20a3..ed54a7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,7 +46,7 @@ async fn main() { let args = Args::parse(); let config_str = std::fs::read_to_string(&args.config) - .expect(&format!("Missing {}", args.config)); + .unwrap_or_else(|_| panic!("{}", format!("Missing {}", args.config))); let config: Config = serde_json::from_str(&config_str).unwrap(); let pool = SqlitePool::connect(&format!("sqlite:{}", args.database)) @@ -57,11 +57,7 @@ async fn main() { let templates = templates::get(); - tokio::spawn(jobs::jobs::start( - config.clone(), - pool.clone(), - templates.clone(), - )); + tokio::spawn(jobs::start(config.clone(), pool.clone(), templates.clone())); let make_svc = make_service_fn(|_conn| { let config = config.clone(); diff --git a/src/payer.rs b/src/payer.rs index 48cee52..0cc8016 100644 --- a/src/payer.rs +++ b/src/payer.rs @@ -3,11 +3,11 @@ use std::collections::HashMap; use crate::model::user::User; pub fn exceeding( - users: &Vec<User>, + users: &[User], user_incomes: &HashMap<i64, i64>, user_payments: &HashMap<i64, i64>, ) -> Vec<(String, i64)> { - let ratios = users.into_iter().map(|u| { + let ratios = users.iter().map(|u| { let income = *user_incomes.get(&u.id).unwrap_or(&0); if income == 0 { (u.name.clone(), 0, 0.0) diff --git a/src/queries.rs b/src/queries.rs index db098e7..8ecc4fe 100644 --- a/src/queries.rs +++ b/src/queries.rs @@ -22,62 +22,44 @@ pub fn payments_url(q: Payments) -> String { Some(p) => params.push(format!("page={}", p)), }; - match q.frequency { - Some(Frequency::Monthly) => { - params.push("frequency=Monthly".to_string()) - } - _ => (), + if let Some(Frequency::Monthly) = q.frequency { + params.push("frequency=Monthly".to_string()) }; - match q.highlight { - Some(id) => params.push(format!("highlight={}", id)), - _ => (), + if let Some(id) = q.highlight { + params.push(format!("highlight={}", id)) }; - match q.name { - Some(str) => { - if !str.is_empty() { - params.push(format!("name={}", str)) - } + if let Some(str) = q.name { + if !str.is_empty() { + params.push(format!("name={}", str)) } - _ => (), }; - match q.cost { - Some(str) => { - if !str.is_empty() { - params.push(format!("cost={}", str)) - } + if let Some(str) = q.cost { + if !str.is_empty() { + params.push(format!("cost={}", str)) } - _ => (), }; - match q.user { - Some(id) => params.push(format!("user={}", id)), - _ => (), + if let Some(id) = q.user { + params.push(format!("user={}", id)) }; - match q.category { - Some(id) => params.push(format!("category={}", id)), - _ => (), + if let Some(id) = q.category { + params.push(format!("category={}", id)) }; - match q.start_date { - Some(str) => { - if !str.is_empty() { - params.push(format!("start_date={}", str)) - } + if let Some(str) = q.start_date { + if !str.is_empty() { + params.push(format!("start_date={}", str)) } - _ => (), }; - match q.end_date { - Some(str) => { - if !str.is_empty() { - params.push(format!("end_date={}", str)) - } + if let Some(str) = q.end_date { + if !str.is_empty() { + params.push(format!("end_date={}", str)) } - _ => (), }; if params.is_empty() { diff --git a/src/routes.rs b/src/routes.rs index 7369f98..b9e137e 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,6 +1,5 @@ use hyper::{Body, Method, Request, Response}; use serde::Deserialize; -use serde_urlencoded; use sqlx::sqlite::SqlitePool; use std::collections::HashMap; use std::convert::Infallible; @@ -73,7 +72,7 @@ async fn connected_user( let mut xs = cookie.split('='); xs.next(); let login_token = xs.next()?; - db::users::get_by_login_token(&pool, login_token.to_string()).await + db::users::get_by_login_token(pool, login_token.to_string()).await } async fn authenticated_routes( diff --git a/src/templates.rs b/src/templates.rs index 390a3aa..c537ac7 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -57,7 +57,7 @@ fn euros(value: &Value, _: &HashMap<String, Value>) -> Result<Value> { let sign = if n < 0 { "-" } else { "" }; Ok(json!(format!("{}{} €", sign, str))) } else if let Some(n) = n.as_f64() { - Ok(json!(format!("{} €", n.to_string()))) + Ok(json!(format!("{} €", n))) } else { Err(Error::msg("Error parsing number")) } @@ -74,7 +74,7 @@ fn numeric(value: &Value, _: &HashMap<String, Value>) -> Result<Value> { let sign = if n < 0 { "-" } else { "" }; Ok(json!(format!("{}{}", sign, str))) } else if let Some(n) = n.as_f64() { - Ok(json!(format!("{}", n.to_string()))) + Ok(json!(format!("{}", n))) } else { Err(Error::msg("Error parsing number")) } @@ -84,7 +84,7 @@ fn numeric(value: &Value, _: &HashMap<String, Value>) -> Result<Value> { } fn rgrouped(str: String, n: usize) -> Vec<String> { - let mut str = str.clone(); + let mut str = str; let mut l = str.len(); let mut res = vec![]; while l > n { diff --git a/src/utils/text.rs b/src/utils/text.rs index 3a6f495..8985210 100644 --- a/src/utils/text.rs +++ b/src/utils/text.rs @@ -1,8 +1,8 @@ -pub fn format_search(str: &String) -> String { +pub fn format_search(str: &str) -> String { format!("%{}%", unaccent(&str.to_lowercase())) } -pub fn unaccent(str: &String) -> String { +pub fn unaccent(str: &str) -> String { str.chars().map(unaccent_char).collect() } |