From af11915a522e5ddce74a94881541f0d5c0de025d Mon Sep 17 00:00:00 2001 From: didier amyot Date: Wed, 15 Feb 2023 16:52:21 -0500 Subject: [PATCH] Lint the shit out of the project. --- src/db/_securities.rs | 4 +- src/db/_test_utils.rs | 2 +- src/db/_users.rs | 12 +++-- src/event_processor.rs | 11 ++-- src/main.rs | 95 +++++++++++++++++++++++------------ src/services/_transactions.rs | 4 +- src/webui/mod.rs | 15 +++--- src/webui/views.rs | 8 +-- src/webui/worker.rs | 2 +- 9 files changed, 92 insertions(+), 61 deletions(-) diff --git a/src/db/_securities.rs b/src/db/_securities.rs index e138e7d..23e7fe1 100644 --- a/src/db/_securities.rs +++ b/src/db/_securities.rs @@ -4,7 +4,6 @@ use serde::Serialize; use crate::db::_external_id::{Id, SecurityId}; use crate::util::{CustomResult, Error}; - #[derive(Debug, Serialize, PartialEq)] pub enum SecurityType { Currency, @@ -77,7 +76,8 @@ pub fn get_security_by_id(tx: &Transaction, id: i32) -> CustomResult { let q = "SELECT id AS sec_id, security_type AS sec_security_type, symbol AS sec_symbol FROM securities WHERE id = ?1"; - tx.query_row(q, params![id], Security::from_row).map_err(Error::SqlError) + tx.query_row(q, params![id], Security::from_row) + .map_err(Error::SqlError) } pub fn list_securities(conn: &Connection) -> CustomResult> { diff --git a/src/db/_test_utils.rs b/src/db/_test_utils.rs index 122f375..8d910e6 100644 --- a/src/db/_test_utils.rs +++ b/src/db/_test_utils.rs @@ -1,4 +1,4 @@ -use rusqlite::{Result, Transaction}; +use rusqlite::{Transaction}; use crate::db::_accounts::create_checking_account; use crate::db::_external_id::UserId; diff --git a/src/db/_users.rs b/src/db/_users.rs index f8eca29..891c3ae 100644 --- a/src/db/_users.rs +++ b/src/db/_users.rs @@ -1,7 +1,7 @@ use crate::db::_external_id::{Id, UserId}; +use crate::util::{CustomResult, Error}; use rusqlite::{params, Connection, Result, Row, Transaction}; use serde::Serialize; -use crate::util::{CustomResult, Error}; #[derive(Debug, Serialize)] pub struct User { @@ -20,7 +20,8 @@ pub fn create_user(tx: &Transaction, name: &String, public_key: &String) -> Cust tx.execute( "INSERT INTO users (name, public_key) VALUES (?1, ?2)", (name, public_key), - ).map_err(Error::SqlError)?; + ) + .map_err(Error::SqlError)?; return get_user_by_id(tx, tx.last_insert_rowid() as i32); } @@ -36,11 +37,14 @@ fn get_user_by_id(tx: &Transaction, id: i32) -> CustomResult { let q = "SELECT id, name, public_key FROM users WHERE id = ?1"; - tx.query_row(q, params![id], _user_from_row).map_err(Error::SqlError) + tx.query_row(q, params![id], _user_from_row) + .map_err(Error::SqlError) } pub fn list_users(conn: &Connection) -> CustomResult> { - let mut stmt = conn.prepare("SELECT id, name, public_key FROM users").map_err(Error::SqlError)?; + let mut stmt = conn + .prepare("SELECT id, name, public_key FROM users") + .map_err(Error::SqlError)?; let ret: Vec = stmt .query_map([], _user_from_row) .map_err(Error::SqlError)? diff --git a/src/event_processor.rs b/src/event_processor.rs index 413651d..600f10f 100644 --- a/src/event_processor.rs +++ b/src/event_processor.rs @@ -1,15 +1,11 @@ use std::borrow::Borrow; - use std::fs::File; use std::path::PathBuf; use crate::util::{CustomResult, Error}; - - - use rusqlite::Connection; use serde::{Deserialize, Serialize}; @@ -40,10 +36,11 @@ where } mod test { - use rusqlite::Connection; use tempfile::tempfile; - use crate::event_processor::process_event_file; - use crate::util::CustomResult; + use super::*; + + + #[test] fn test_basic() -> CustomResult<()> { diff --git a/src/main.rs b/src/main.rs index dc8302e..462b719 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::path::PathBuf; -use clap::{Arg, ArgAction, ArgMatches, Command, value_parser}; use clap::parser::ValuesRef; +use clap::{value_parser, Arg, ArgAction, ArgMatches, Command}; use log::{debug, error, info}; use rusqlite::Connection; use serde::Serialize; @@ -38,15 +38,29 @@ impl Output { } trait CommandLineUserLogic { - fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String) -> CustomResult<()>; + fn create_user( + &mut self, + conn: &mut Connection, + name: &String, + public_key: &String, + ) -> CustomResult<()>; fn list_user(&mut self, conn: &Connection) -> CustomResult<()>; fn list_securities(&mut self, conn: &Connection) -> CustomResult<()>; - fn process_events(&mut self, conn: &mut Connection, files: ValuesRef) -> CustomResult<()>; + fn process_events( + &mut self, + conn: &mut Connection, + files: ValuesRef, + ) -> CustomResult<()>; - fn run_webui(&mut self, conn: &mut Connection, port: u16, enable_template_autoreload: bool) -> CustomResult<()>; + fn run_webui( + &mut self, + conn: &mut Connection, + port: u16, + enable_template_autoreload: bool, + ) -> CustomResult<()>; } fn verbosity_arg() -> Arg { @@ -190,7 +204,7 @@ fn process_list_user_command_line( conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic, -) -> CustomResult<()>{ +) -> CustomResult<()> { debug!("Processing list users command line {:?}.", m); logic.list_user(conn) } @@ -202,9 +216,7 @@ fn process_user_command_line( ) -> CustomResult<()> { debug!("Processing user command line {:?}.", m); match m.subcommand() { - Some(("new", new_user_match)) => { - process_new_user_command_line(conn, new_user_match, logic) - } + Some(("new", new_user_match)) => process_new_user_command_line(conn, new_user_match, logic), Some(("ls", list_user_match)) => { process_list_user_command_line(conn, list_user_match, logic) } @@ -216,7 +228,7 @@ fn process_list_securities_command_line( conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic, -) -> CustomResult<()>{ +) -> CustomResult<()> { debug!("Processing list securities command line {:?}.", m); logic.list_securities(conn) } @@ -230,7 +242,9 @@ fn process_process_events_command_line( let mut files = m.get_many::("PATH").unwrap(); if files.any(|x| !x.exists()) { error!("Some passed files do not exists. Existing right now."); - return Err(Error::UserInputError("Some passed files do not exists. Existing right now.".to_string())); + return Err(Error::UserInputError( + "Some passed files do not exists. Existing right now.".to_string(), + )); } logic.process_events(conn, files) @@ -240,7 +254,7 @@ fn process_run_wui_command_line( conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic, -) -> CustomResult<()> { +) -> CustomResult<()> { debug!("Processing run wui command line {:?}.", m); let port = m.get_one::("port").unwrap_or(&2222); let enable_template_autoreload = m.get_flag("enable-template-autoreload"); @@ -251,7 +265,7 @@ fn process_securities_command_line( conn: &Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic, -) -> CustomResult<()>{ +) -> CustomResult<()> { debug!("Processing securities command line {:?}.", m); match m.subcommand() { Some(("ls", list_securities_match)) => { @@ -265,7 +279,7 @@ fn process_events_command_line( conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic, -) -> CustomResult<()>{ +) -> CustomResult<()> { debug!("Processing events command line {:?}.", m); match m.subcommand() { Some(("process", process_events_match)) => { @@ -279,12 +293,10 @@ fn process_wui_command_line( conn: &mut Connection, m: &ArgMatches, logic: &mut dyn CommandLineUserLogic, -) -> CustomResult<()> { +) -> CustomResult<()> { debug!("Processing wui command line {:?}.", m); match m.subcommand() { - Some(("run", run_wui_match)) => { - process_run_wui_command_line(conn, run_wui_match, logic) - } + Some(("run", run_wui_match)) => process_run_wui_command_line(conn, run_wui_match, logic), _ => unreachable!(), } } @@ -296,18 +308,12 @@ fn process_command_line( ) -> CustomResult<()> { debug!("Processing command line {:?}.", m); match m.subcommand() { - Some(("users", user_match)) => { - process_user_command_line(conn, user_match, logic) - } + Some(("users", user_match)) => process_user_command_line(conn, user_match, logic), Some(("securities", securities_match)) => { process_securities_command_line(conn, securities_match, logic) } - Some(("events", events_match)) => { - process_events_command_line(conn, events_match, logic) - } - Some(("wui", wui_match)) => { - process_wui_command_line(conn, wui_match, logic) - } + Some(("events", events_match)) => process_events_command_line(conn, events_match, logic), + Some(("wui", wui_match)) => process_wui_command_line(conn, wui_match, logic), _ => { println!("Received un parsable command. {:?}", m.subcommand()); unreachable!(); @@ -340,22 +346,31 @@ mod cli_test { } impl CommandLineUserLogic for TestCommandLineLogic { - fn create_user(&mut self, _conn: &mut Connection, _name: &String, _public_key: &String) -> CustomResult<()> { + fn create_user( + &mut self, + _conn: &mut Connection, + _name: &String, + _public_key: &String, + ) -> CustomResult<()> { self.create_user_count += 1; Ok(()) } - fn list_user(&mut self, _conn: &Connection) -> CustomResult<()>{ + fn list_user(&mut self, _conn: &Connection) -> CustomResult<()> { self.list_user_count += 1; Ok(()) } - fn list_securities(&mut self, _conn: &Connection) -> CustomResult<()>{ + fn list_securities(&mut self, _conn: &Connection) -> CustomResult<()> { self.list_securities_count += 1; Ok(()) } - fn process_events(&mut self, _conn: &mut Connection, _files: ValuesRef) -> CustomResult<()> { + fn process_events( + &mut self, + _conn: &mut Connection, + _files: ValuesRef, + ) -> CustomResult<()> { self.process_events_count += 1; Ok(()) } @@ -471,7 +486,12 @@ impl ProCommandLineLogic { } impl CommandLineUserLogic for ProCommandLineLogic { - fn create_user(&mut self, conn: &mut Connection, name: &String, public_key: &String) -> CustomResult<()> { + fn create_user( + &mut self, + conn: &mut Connection, + name: &String, + public_key: &String, + ) -> CustomResult<()> { let tx = conn.transaction().map_err(Error::SqlError)?; create_user(&tx, name, public_key)?; tx.commit().map_err(Error::SqlError) @@ -491,11 +511,20 @@ impl CommandLineUserLogic for ProCommandLineLogic { Ok(()) } - fn process_events(&mut self, conn: &mut Connection, files: ValuesRef) -> CustomResult<()> { + fn process_events( + &mut self, + conn: &mut Connection, + files: ValuesRef, + ) -> CustomResult<()> { process_event_files(conn, files) } - fn run_webui(&mut self, _conn: &mut Connection, port: u16, enable_template_autoreload: bool) -> CustomResult<()> { + fn run_webui( + &mut self, + _conn: &mut Connection, + port: u16, + enable_template_autoreload: bool, + ) -> CustomResult<()> { info!("Serving web-ui on port {:?}", port); web_ui_main(port, enable_template_autoreload) } diff --git a/src/services/_transactions.rs b/src/services/_transactions.rs index 6876d99..e2b97e6 100644 --- a/src/services/_transactions.rs +++ b/src/services/_transactions.rs @@ -124,7 +124,9 @@ fn cash_cheque(tx: &mut Transaction, cheque: &Cheque, account_id: &AccountId) -> mod test_caching_cheques { use rusqlite::Connection; - use crate::db::accounts::{create_checking_account, create_security_emitter_account, get_account_by_id}; + use crate::db::accounts::{ + create_checking_account, create_security_emitter_account, get_account_by_id, + }; use crate::db::cheques::{Drawer, Payee}; use crate::db::external_id::{Id, UserId}; use crate::db::test_utils; diff --git a/src/webui/mod.rs b/src/webui/mod.rs index f1f7645..b1ff9fd 100644 --- a/src/webui/mod.rs +++ b/src/webui/mod.rs @@ -1,19 +1,18 @@ #![feature(future_join)] - use std::{env, path::PathBuf}; use actix_files; use actix_web::{ - App, dev::ServiceResponse, error, - FromRequest, - http::{header::ContentType, StatusCode}, HttpRequest, HttpResponse, HttpServer, middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, Responder, Result, web, + http::{header::ContentType, StatusCode}, + middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, + web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result, }; use actix_web_lab::respond::Html; use async_channel; -use futures::{FutureExt, pin_mut, select}; +use futures::{pin_mut, select, FutureExt}; use minijinja; use minijinja_autoreload::AutoReloader; @@ -94,9 +93,9 @@ pub async fn web_ui_main(port: u16, enable_template_autoreload: bool) -> CustomR .wrap(ErrorHandlers::new().handler(StatusCode::NOT_FOUND, not_found)) .wrap(Logger::default()) }) - .workers(2) - .bind(("0.0.0.0", port)) - .unwrap(); + .workers(2) + .bind(("0.0.0.0", port)) + .unwrap(); let t1 = server.run().fuse(); let t2 = worker_loop(r).fuse(); diff --git a/src/webui/views.rs b/src/webui/views.rs index d484725..0bfa217 100644 --- a/src/webui/views.rs +++ b/src/webui/views.rs @@ -1,9 +1,9 @@ -use actix_web::http::header::q; + use actix_web::{web, HttpRequest, Responder}; use clap::builder::TypedValueParser; use log::info; use minijinja::context; -use resource::resource_str; + use serde::Deserialize; use std::collections::HashMap; @@ -26,7 +26,7 @@ pub async fn overview(tmpl_env: MiniJinjaRenderer) -> actix_web::Result>, ) -> actix_web::Result { @@ -38,7 +38,7 @@ pub async fn overview_submit_button( #[actix_web::get("/partials/actions/action-form")] pub async fn actions_partials( - req: HttpRequest, + _req: HttpRequest, tmpl_env: MiniJinjaRenderer, query: web::Query>, ) -> actix_web::Result { diff --git a/src/webui/worker.rs b/src/webui/worker.rs index 3ffd0f7..9de653d 100644 --- a/src/webui/worker.rs +++ b/src/webui/worker.rs @@ -1,4 +1,4 @@ -use std::{io}; +use std::io; pub async fn worker_loop(r: async_channel::Receiver) -> io::Result<()> { loop {