From f8d676e5dfdbd57d404f980e654831d6cbe62c9b Mon Sep 17 00:00:00 2001 From: didier amyot Date: Thu, 9 Mar 2023 09:37:21 -0500 Subject: [PATCH] lint code. --- src/db/_ledger_internal.rs | 8 +++---- src/db/mod.rs | 4 ++-- src/services/_authentication.rs | 38 +++++++++++++++++-------------- src/services/_securities.rs | 6 ++--- src/services/mod.rs | 8 +++---- src/webui/index_views.rs | 40 ++++++++++++++++++--------------- src/webui/mod.rs | 16 ++++++++----- src/webui/services.rs | 12 +++++----- src/webui/worker.rs | 1 - 9 files changed, 71 insertions(+), 62 deletions(-) diff --git a/src/db/_ledger_internal.rs b/src/db/_ledger_internal.rs index 3957f70..26f9004 100644 --- a/src/db/_ledger_internal.rs +++ b/src/db/_ledger_internal.rs @@ -1,6 +1,6 @@ -use rusqlite::{Connection, params, Row, Transaction, Result}; use crate::util::{CustomResult, Error}; -use base64::{Engine as _, engine::general_purpose}; +use base64::{engine::general_purpose, Engine as _}; +use rusqlite::{params, Connection, Result, Row, Transaction}; pub struct LedgerInternal {} @@ -47,11 +47,11 @@ impl LedgerInternal { #[cfg(test)] mod test_ledger_internal { - use crate::migration; use super::*; + use crate::migration; #[test] - fn test_initial_cookie_secret() -> CustomResult<()>{ + fn test_initial_cookie_secret() -> CustomResult<()> { let mut conn = Connection::open_in_memory().unwrap(); migration::apply_migrations(&mut conn).unwrap(); let tx = conn.transaction().unwrap(); diff --git a/src/db/mod.rs b/src/db/mod.rs index 31672e0..aac6a64 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,12 +2,12 @@ mod _accounts; mod _cheques; mod _external_id; mod _interests; +mod _ledger_internal; mod _securities; mod _test_utils; mod _users; -mod _ledger_internal; -pub mod ledger_internal{ +pub mod ledger_internal { pub use super::_ledger_internal::*; } diff --git a/src/services/_authentication.rs b/src/services/_authentication.rs index b95785e..f2a3244 100644 --- a/src/services/_authentication.rs +++ b/src/services/_authentication.rs @@ -1,33 +1,36 @@ -use actix_web::HttpRequest; -use cookie::{CookieJar, Cookie, Key}; -use cookie::time::OffsetDateTime; -use rusqlite::Connection; use crate::db::ledger_internal::LedgerInternal; use crate::{CustomResult, Error}; +use actix_web::HttpRequest; +use cookie::time::OffsetDateTime; +use cookie::{Cookie, CookieJar, Key}; +use rusqlite::Connection; pub struct SessionService { key: Key, } - impl SessionService { - - pub fn from_conn(conn: &mut Connection) -> CustomResult{ + pub fn from_conn(conn: &mut Connection) -> CustomResult { let tx = conn.transaction().map_err(Error::SqlError)?; - let secret = LedgerInternal::new().get_cookie_secret(& tx)?; - let ret = SessionService{key: Key::derive_from(&secret)}; + let secret = LedgerInternal::new().get_cookie_secret(&tx)?; + let ret = SessionService { + key: Key::derive_from(&secret), + }; Ok(ret) } - pub fn create_login_cookie(&self, user_id: &String ) -> Cookie{ + pub fn create_login_cookie(&self, user_id: &String) -> Cookie { let mut jar = CookieJar::new(); - jar.private_mut(& self.key).add(Cookie::new("ledger-user-id", user_id.clone())); - jar.get("ledger-user-id").unwrap_or(& Cookie::new("ledger-user-id", "")).clone() + jar.private_mut(&self.key) + .add(Cookie::new("ledger-user-id", user_id.clone())); + jar.get("ledger-user-id") + .unwrap_or(&Cookie::new("ledger-user-id", "")) + .clone() } - fn _decrypt_cookie(&self, cookie: Cookie<'static>) -> Option{ + fn _decrypt_cookie(&self, cookie: Cookie<'static>) -> Option { let mut jar = CookieJar::new(); - jar.private(& self.key).decrypt(cookie) + jar.private(&self.key).decrypt(cookie) } // todo: this service should not be aware of http requests. @@ -35,13 +38,14 @@ impl SessionService { // web framework pub fn get_login(&self, req: &HttpRequest) -> Option { req.cookie("ledger-user-id") - .and_then(|c|self._decrypt_cookie(c)).map(|c| c.value().to_string()) + .and_then(|c| self._decrypt_cookie(c)) + .map(|c| c.value().to_string()) } - pub fn logout(&self) -> Cookie{ + pub fn logout(&self) -> Cookie { let mut cookie = Cookie::new("ledger-user-id", ""); let epoch = OffsetDateTime::from_unix_timestamp_nanos(0).unwrap(); cookie.set_expires(epoch); cookie } -} \ No newline at end of file +} diff --git a/src/services/_securities.rs b/src/services/_securities.rs index c7b3a4e..9583574 100644 --- a/src/services/_securities.rs +++ b/src/services/_securities.rs @@ -1,8 +1,6 @@ -use rusqlite::{Connection}; +use rusqlite::Connection; -pub struct SecurityDTO{ - -} +pub struct SecurityDTO {} pub struct SecurityService<'a> { conn: &'a Connection, diff --git a/src/services/mod.rs b/src/services/mod.rs index 56c78dd..9404c68 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,13 +1,13 @@ -use rusqlite::Connection; use crate::{CustomResult, Error}; +use rusqlite::Connection; -pub use crate::services::_users::UserService; pub use crate::services::_authentication::SessionService; +pub use crate::services::_users::UserService; -mod _transactions; -mod _users; mod _authentication; mod _securities; +mod _transactions; +mod _users; pub use crate::db::ledger_internal::LedgerInternal; diff --git a/src/webui/index_views.rs b/src/webui/index_views.rs index b612559..2bafbe0 100644 --- a/src/webui/index_views.rs +++ b/src/webui/index_views.rs @@ -1,15 +1,15 @@ -use std::time::Instant; -use actix_web::{HttpRequest, Responder, web}; -use cookie::{CookieJar, Cookie, Key}; +use actix_web::{web, HttpRequest, Responder}; use cookie::time::macros::datetime; use cookie::time::OffsetDateTime; +use cookie::{Cookie, CookieJar, Key}; +use std::time::Instant; -use minijinja::context; -use crate::webui::MiniJinjaRenderer; -use serde::Deserialize; use crate::db::external_id::UserId; use crate::services::ReadOnlyServiceFactory; +use crate::webui::MiniJinjaRenderer; use crate::SQLiteConnectionFactory; +use minijinja::context; +use serde::Deserialize; #[derive(Deserialize, Debug)] struct LoginPayload { @@ -26,18 +26,21 @@ pub async fn login( let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500 let mut service_factory = ReadOnlyServiceFactory::new(&mut conn); let authentication_service = service_factory.get_authentication_service().unwrap(); - let cookie = authentication_service.create_login_cookie(& form.username); + let cookie = authentication_service.create_login_cookie(&form.username); let ctx = context! { name => form.username, }; - let mut ret = tmpl_env.render("partials/index/success.html", ctx)?.customize(); + let mut ret = tmpl_env + .render("partials/index/success.html", ctx)? + .customize(); Ok(ret.insert_header(("Set-Cookie", cookie.to_string()))) } #[actix_web::post("/logout")] -pub async fn logout(conn_factory: web::Data, - tmpl_env: MiniJinjaRenderer) -> actix_web::Result { - +pub async fn logout( + conn_factory: web::Data, + tmpl_env: MiniJinjaRenderer, +) -> actix_web::Result { let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500 let mut service_factory = ReadOnlyServiceFactory::new(&mut conn); let authentication_service = service_factory.get_authentication_service().unwrap(); @@ -49,19 +52,20 @@ pub async fn logout(conn_factory: web::Data, } #[actix_web::get("/")] -pub async fn index(req: HttpRequest, - conn_factory: web::Data, - tmpl_env: MiniJinjaRenderer) -> actix_web::Result { - +pub async fn index( + req: HttpRequest, + conn_factory: web::Data, + tmpl_env: MiniJinjaRenderer, +) -> actix_web::Result { let mut conn = conn_factory.readonly().unwrap(); // todo; to proper 500 let mut service_factory = ReadOnlyServiceFactory::new(&mut conn); let authentication_service = service_factory.get_authentication_service().unwrap(); - let userid = authentication_service.get_login(& req); + let userid = authentication_service.get_login(&req); let username = userid.clone().unwrap_or("".to_string()); - let ctx = context!{ + let ctx = context! { is_logged_in => userid.is_some(), name => username, }; tmpl_env.render("index.html", ctx) -} \ No newline at end of file +} diff --git a/src/webui/mod.rs b/src/webui/mod.rs index fcecec6..347d0c0 100644 --- a/src/webui/mod.rs +++ b/src/webui/mod.rs @@ -3,7 +3,13 @@ use std::{env, path::PathBuf}; use actix_files; -use actix_web::{dev::ServiceResponse, error, http::{header::ContentType, StatusCode}, middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result, Scope}; +use actix_web::{ + dev::ServiceResponse, + error, + http::{header::ContentType, StatusCode}, + middleware::{ErrorHandlerResponse, ErrorHandlers, Logger}, + web, App, FromRequest, HttpRequest, HttpResponse, HttpServer, Responder, Result, Scope, +}; use actix_web_lab::respond::Html; use async_channel; use futures::{pin_mut, select, FutureExt}; @@ -13,7 +19,7 @@ use rusqlite::Connection; use crate::events::Event; use crate::util::{CustomResult, Error, SQLiteConnectionFactory}; -use crate::webui::worker::{event_processor_loop}; +use crate::webui::worker::event_processor_loop; mod index_views; mod services; @@ -71,13 +77,11 @@ fn create_auto_reloader(enable_template_autoreload: bool) -> AutoReloader { } fn create_static_scope() -> Scope { - web::scope("/static").service( - actix_files::Files::new("/", "./resources/static/")) + web::scope("/static").service(actix_files::Files::new("/", "./resources/static/")) } fn create_api_scope() -> Scope { - web::scope("/v1") - .service(views::push_event) + web::scope("/v1").service(views::push_event) } fn create_views_scope() -> Scope { diff --git a/src/webui/services.rs b/src/webui/services.rs index 2e05d37..95c5805 100644 --- a/src/webui/services.rs +++ b/src/webui/services.rs @@ -1,5 +1,5 @@ +use crate::services::{ReadOnlyServiceFactory, SessionService, UserService}; use crate::util::{CustomResult, Error, SQLiteConnectionFactory}; -use crate::services::{SessionService, ReadOnlyServiceFactory, UserService}; struct ServicesCache { session_service: SessionService, @@ -13,13 +13,13 @@ struct Services { impl Services { pub fn new(conn_fact: SQLiteConnectionFactory) -> Services { let mut tmp_conn = conn_fact.connection().unwrap(); - let session_service = SessionService::from_conn(&mut tmp_conn).unwrap(); - let cache = ServicesCache{session_service}; + let session_service = SessionService::from_conn(&mut tmp_conn).unwrap(); + let cache = ServicesCache { session_service }; Services { conn_fact, cache } } - pub fn get_authentication_service(& self) -> &SessionService { - & self.cache.session_service + pub fn get_authentication_service(&self) -> &SessionService { + &self.cache.session_service } pub fn get_user_service(&self) -> i32 { @@ -28,4 +28,4 @@ impl Services { // service_factory.get_user_service() 88 } -} \ No newline at end of file +} diff --git a/src/webui/worker.rs b/src/webui/worker.rs index 73c8565..8fee6ce 100644 --- a/src/webui/worker.rs +++ b/src/webui/worker.rs @@ -25,4 +25,3 @@ pub async fn event_processor_loop( } } } -