From 31fd0f0c5101f7eb4259ba822686d85bf67cd823 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 15 Jul 2022 18:59:57 +0530 Subject: [PATCH] chore: clippy lints --- db/db-core/src/tests.rs | 9 ++--- db/db-sqlx-sqlite/src/lib.rs | 3 +- federate/federate-core/src/tests.rs | 4 +-- federate/publiccodeyml/src/lib.rs | 2 +- src/errors.rs | 9 ++--- src/pages/auth/add.rs | 8 +---- src/pages/auth/verify.rs | 11 ++---- src/settings.rs | 36 +++----------------- src/spider.rs | 53 +++++++++++++---------------- src/static_assets/mod.rs | 6 ++-- src/static_assets/static_files.rs | 2 -- src/tests.rs | 18 +++++----- src/verify.rs | 1 - 13 files changed, 60 insertions(+), 102 deletions(-) diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index 5f8cf7a..7eee390 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -102,8 +102,9 @@ pub async fn adding_forge_works<'a, T: SCDatabase>( /// test if all forge type implementations are loaded into DB pub async fn forge_type_exists_helper(db: &T) { - for f in [ForgeImplementation::Gitea].iter() { - println!("Testing forge implementation exists for: {}", f.to_str()); - assert!(db.forge_type_exists(f).await.unwrap()); - } + //for f in [ForgeImplementation::Gitea].iter() { + //let f = For + let f = ForgeImplementation::Gitea; + println!("Testing forge implementation exists for: {}", f.to_str()); + assert!(db.forge_type_exists(&f).await.unwrap()); } diff --git a/db/db-sqlx-sqlite/src/lib.rs b/db/db-sqlx-sqlite/src/lib.rs index f49c981..ea35239 100644 --- a/db/db-sqlx-sqlite/src/lib.rs +++ b/db/db-sqlx-sqlite/src/lib.rs @@ -274,7 +274,7 @@ impl SCDatabase for Database { .map_err(|e| DBError::DBError(Box::new(e)))?; Ok(User { username: username.into(), - url: url.into(), + url, profile_photo: res.profile_photo_html_url, html_link: res.html_url, }) @@ -496,6 +496,7 @@ impl SCDatabase for Database { /// Get all repositories async fn get_all_repositories(&self, offset: u32, limit: u32) -> DBResult> { + #[allow(non_snake_case)] struct InnerRepository { /// html link to the repository pub html_url: String, diff --git a/federate/federate-core/src/tests.rs b/federate/federate-core/src/tests.rs index 2f94733..5a2fc80 100644 --- a/federate/federate-core/src/tests.rs +++ b/federate/federate-core/src/tests.rs @@ -32,12 +32,12 @@ pub async fn adding_forge_works<'a, T: Federate>( // add user assert!(!ff - .user_exists(&create_user_msg.username, &create_user_msg.url) + .user_exists(create_user_msg.username, &create_user_msg.url) .await .unwrap()); ff.create_user(&create_user_msg).await.unwrap(); assert!(ff - .user_exists(&create_user_msg.username, &create_user_msg.url) + .user_exists(create_user_msg.username, &create_user_msg.url) .await .unwrap()); diff --git a/federate/publiccodeyml/src/lib.rs b/federate/publiccodeyml/src/lib.rs index 573d3c6..c145898 100644 --- a/federate/publiccodeyml/src/lib.rs +++ b/federate/publiccodeyml/src/lib.rs @@ -136,7 +136,7 @@ impl Federate for PccFederate { /// delete forge instance async fn delete_forge_instance(&self, url: &Url) -> FResult<()> { - let path = self.get_instance_path(&url, false).await?; + let path = self.get_instance_path(url, false).await?; self.rm_util(&path).await } diff --git a/src/errors.rs b/src/errors.rs index 4f8f351..fd0ada7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -131,10 +131,11 @@ impl From for ServiceError { #[cfg(not(tarpaulin_include))] fn from(e: DBError) -> ServiceError { println!("from conversin: {}", e); - match e { - // TODO: resolve all errors to ServiceError::* - _ => ServiceError::DBError(DBErrorWrapper(e)), - } + ServiceError::DBError(DBErrorWrapper(e)) + // match e { + // // TODO: resolve all errors to ServiceError::* + // _ => ServiceError::DBError(DBErrorWrapper(e)), + // } } } diff --git a/src/pages/auth/add.rs b/src/pages/auth/add.rs index fed8116..c164fbf 100644 --- a/src/pages/auth/add.rs +++ b/src/pages/auth/add.rs @@ -18,7 +18,6 @@ use actix_web::http::{self, header::ContentType}; use actix_web::{HttpResponse, Responder}; use actix_web_codegen_const_routes::{get, post}; -use log::info; use serde::{Deserialize, Serialize}; use std::cell::RefCell; use tera::Context; @@ -97,7 +96,7 @@ pub async fn add_submit( db: &BoxDB, ) -> ServiceResult { let url_hostname = Url::parse(&payload.hostname).unwrap(); - let key = TXTChallenge::get_challenge_txt_key(&ctx, &url_hostname); + let key = TXTChallenge::get_challenge_txt_key(ctx, &url_hostname); if db.dns_challenge_exists(&key).await? { let value = db.get_dns_challenge(&key).await?.value; Ok(TXTChallenge { key, value }) @@ -136,14 +135,9 @@ mod tests { use actix_web::test; use url::Url; - use super::AddChallenge; use super::AddChallengePayload; use super::TXTChallenge; use crate::errors::*; - use crate::pages::errors::*; - use crate::settings::Settings; - - use db_core::prelude::*; #[cfg(test)] mod isolated { diff --git a/src/pages/auth/verify.rs b/src/pages/auth/verify.rs index 56458ba..9f5061f 100644 --- a/src/pages/auth/verify.rs +++ b/src/pages/auth/verify.rs @@ -18,7 +18,6 @@ use actix_web::http::{self, header::ContentType}; use actix_web::{HttpResponse, Responder}; use actix_web_codegen_const_routes::{get, post}; -use log::info; use serde::{Deserialize, Serialize}; use std::cell::RefCell; use tera::Context; @@ -83,7 +82,7 @@ pub async fn get_verify( query: web::Query, ) -> PageResult { let payload = query.into_inner(); - let value = _get_challenge(&payload, &ctx, &db).await.map_err(|e| { + let value = _get_challenge(&payload, &db).await.map_err(|e| { let challenge = Challenge { key: payload.hostname, value: "".into(), @@ -103,11 +102,7 @@ pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(submit_verify); } -async fn _get_challenge( - payload: &VerifyChallengePayload, - ctx: &ArcCtx, - db: &BoxDB, -) -> ServiceResult { +async fn _get_challenge(payload: &VerifyChallengePayload, db: &BoxDB) -> ServiceResult { let value = db.get_dns_challenge(&payload.hostname).await?; Ok(value) } @@ -120,7 +115,7 @@ pub async fn submit_verify( federate: WebFederate, ) -> PageResult { let payload = payload.into_inner(); - let value = _get_challenge(&payload, &ctx, &db).await.map_err(|e| { + let value = _get_challenge(&payload, &db).await.map_err(|e| { let challenge = Challenge { key: payload.hostname.clone(), value: "".into(), diff --git a/src/settings.rs b/src/settings.rs index 09675af..068b0e8 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -90,13 +90,11 @@ impl Repository { if license_path.exists() { if license_path.is_dir() { panic!("Can't create license file at {:?}", license_path); - } else { - if !fs::read_to_string(&license_path) - .unwrap() - .contains(CC0_LICENSE_TXT) - { - panic!("Can't create license file at {:?}", &license_path); - } + } else if !fs::read_to_string(&license_path) + .unwrap() + .contains(CC0_LICENSE_TXT) + { + panic!("Can't create license file at {:?}", &license_path); } } else { fs::write(license_path, CC0_LICENSE_TXT).unwrap(); @@ -113,16 +111,6 @@ pub enum DBType { Sqlite, } -impl DBType { - fn from_url(url: &Url) -> Result { - match url.scheme() { - "sqlite" => Ok(Self::Sqlite), - "postgres" => Ok(Self::Postgres), - _ => Err(ConfigError::Message("Unknown database type".into())), - } - } -} - #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Database { pub url: String, @@ -237,20 +225,6 @@ mod tests { use super::*; use crate::utils::get_random; - #[test] - fn database_type_test() { - for i in ["sqlite://foo", "postgres://bar", "unknown://"].iter() { - let url = Url::parse(i).unwrap(); - if i.contains("sqlite") { - assert_eq!(DBType::from_url(&url).unwrap(), DBType::Sqlite); - } else if i.contains("unknown") { - assert!(DBType::from_url(&url).is_err()); - } else { - assert_eq!(DBType::from_url(&url).unwrap(), DBType::Postgres); - } - } - } - #[test] fn root_dir_is_created_test() { let dir; diff --git a/src/spider.rs b/src/spider.rs index 2ae9cd5..e75b104 100644 --- a/src/spider.rs +++ b/src/spider.rs @@ -43,21 +43,19 @@ impl Ctx { let url = forge.get_url(); if !db.forge_exists(url).await.unwrap() { info!("[crawl][{url}] Creating forge"); - let mut msg = CreateForge { + let msg = CreateForge { url: url.clone(), forge_type: forge.forge_type(), }; db.create_forge_instance(&msg).await.unwrap(); - } else { - if !federate.forge_exists(&url).await.unwrap() { - let forge = db.get_forge(&url).await.unwrap(); - let msg = CreateForge { - url: url.clone(), - forge_type: forge.forge_type, - }; - federate.create_forge_instance(&msg).await.unwrap(); - } + } else if !federate.forge_exists(url).await.unwrap() { + let forge = db.get_forge(url).await.unwrap(); + let msg = CreateForge { + url: url.clone(), + forge_type: forge.forge_type, + }; + federate.create_forge_instance(&msg).await.unwrap(); } loop { @@ -84,15 +82,14 @@ impl Ctx { let msg = u.as_ref().into(); db.add_user(&msg).await.unwrap(); federate.create_user(&msg).await.unwrap(); - } else { - if !federate - .user_exists(username, forge.get_url()) - .await - .unwrap() - { - let msg = u.as_ref().into(); - federate.create_user(&msg).await.unwrap(); - } + } + if !federate + .user_exists(username, forge.get_url()) + .await + .unwrap() + { + let msg = u.as_ref().into(); + federate.create_user(&msg).await.unwrap(); } } @@ -106,15 +103,14 @@ impl Ctx { let msg = r.into(); db.create_repository(&msg).await.unwrap(); federate.create_repository(&msg).await.unwrap(); - } else { - if !federate - .repository_exists(&r.name, &r.owner.username, &r.url) - .await - .unwrap() - { - let msg = r.into(); - federate.create_repository(&msg).await.unwrap(); - } + } + if !federate + .repository_exists(&r.name, &r.owner.username, &r.url) + .await + .unwrap() + { + let msg = r.into(); + federate.create_repository(&msg).await.unwrap(); } } @@ -126,7 +122,6 @@ impl Ctx { #[cfg(test)] mod tests { use crate::tests::sqlx_sqlite; - use db_core::prelude::*; use url::Url; diff --git a/src/static_assets/mod.rs b/src/static_assets/mod.rs index 90f6350..30cd95c 100644 --- a/src/static_assets/mod.rs +++ b/src/static_assets/mod.rs @@ -33,7 +33,7 @@ pub mod routes { use super::*; lazy_static! { - pub static ref ASSETS: Assets = Assets::new(); + pub static ref ASSETS: Assets = Assets::default(); } #[derive(Serialize)] @@ -43,9 +43,9 @@ pub mod routes { pub css: &'static str, } - impl Assets { + impl Default for Assets { /// create new instance of Routes - pub fn new() -> Assets { + fn default() -> Assets { Assets { css: &static_files::assets::CSS, } diff --git a/src/static_assets/static_files.rs b/src/static_assets/static_files.rs index c0ab0cd..6d3cee9 100644 --- a/src/static_assets/static_files.rs +++ b/src/static_assets/static_files.rs @@ -67,9 +67,7 @@ mod tests { use actix_web::http::StatusCode; use actix_web::test; - use crate::db::BoxDB; use crate::tests::*; - use crate::ArcCtx; use crate::*; use super::assets::CSS; diff --git a/src/tests.rs b/src/tests.rs index e2a3337..79a9719 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -25,16 +25,16 @@ pub use crate::federate::{get_federate, ArcFederate}; use crate::settings::{DBType, Settings}; //use actix_web::cookie::Cookie; -use crate::errors::*; +//use crate::errors::*; use crate::*; -use actix_web::test; -use actix_web::{ - body::{BoxBody, EitherBody}, - dev::ServiceResponse, - error::ResponseError, - http::StatusCode, -}; -use serde::Serialize; +//use actix_web::test; +//use actix_web::{ +// body::{BoxBody, EitherBody}, +// dev::ServiceResponse, +// error::ResponseError, +// http::StatusCode, +//}; +//use serde::Serialize; //pub mod sqlx_postgres { // use super::*; diff --git a/src/verify.rs b/src/verify.rs index 8348312..a1c37a4 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -31,7 +31,6 @@ pub struct TXTChallenge { pub value: String, } -const KEY_LEN: usize = 30; const VALUES_LEN: usize = 30; impl TXTChallenge {