Skip to content

Commit

Permalink
refactor: clippy enhancements (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
kentSarmiento committed Dec 27, 2023
1 parent b4c1429 commit 0a167e9
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 43 deletions.
1 change: 1 addition & 0 deletions link-for-later/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn new(db: Database) -> Router {
.with_state(state)
}

#[allow(missing_debug_implementations)]
#[derive(Clone)]
pub struct State {
links_service: DynLinksService,
Expand Down
4 changes: 2 additions & 2 deletions link-for-later/src/dto/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Claims {
impl Claims {
pub fn new(sub: &str, iat: usize, exp: usize) -> Self {
Self {
sub: sub.to_string(),
sub: sub.to_owned(),
iat,
exp,
}
Expand All @@ -29,7 +29,7 @@ pub struct Token {
impl Token {
pub fn new(jwt: &str) -> Self {
Self {
jwt: jwt.to_string(),
jwt: jwt.to_owned(),
}
}

Expand Down
8 changes: 4 additions & 4 deletions link-for-later/src/dto/request/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Item {
}
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Query {
#[serde(skip_serializing_if = "String::is_empty")]
id: String,
Expand All @@ -60,13 +60,13 @@ pub struct QueryBuilder {
impl QueryBuilder {
pub fn new(id: &str, owner: &str) -> Self {
Self {
id: id.to_string(),
owner: owner.to_string(),
id: id.to_owned(),
owner: owner.to_owned(),
}
}

pub fn owner(mut self, owner: &str) -> Self {
self.owner = owner.to_string();
self.owner = owner.to_owned();
self
}

Expand Down
4 changes: 2 additions & 2 deletions link-for-later/src/dto/request/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Info {
}
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Query {
email: String,
}
Expand All @@ -45,7 +45,7 @@ pub struct QueryBuilder {
impl QueryBuilder {
pub fn new(email: &str) -> Self {
Self {
email: email.to_string(),
email: email.to_owned(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion link-for-later/src/dto/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct Login {
impl Login {
pub fn new(token: &str) -> Self {
Self {
token: token.to_string(),
token: token.to_owned(),
}
}
}
16 changes: 8 additions & 8 deletions link-for-later/src/entity/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,43 @@ pub struct ItemBuilder {
impl ItemBuilder {
pub fn new(url: &str) -> Self {
Self {
url: url.to_string(),
url: url.to_owned(),
..Default::default()
}
}

pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self.id = id.to_owned();
self
}

pub fn owner(mut self, owner: &str) -> Self {
self.owner = owner.to_string();
self.owner = owner.to_owned();
self
}

pub fn url(mut self, url: &str) -> Self {
self.url = url.to_string();
self.url = url.to_owned();
self
}

pub fn title(mut self, title: &str) -> Self {
self.title = title.to_string();
self.title = title.to_owned();
self
}

pub fn description(mut self, description: &str) -> Self {
self.description = description.to_string();
self.description = description.to_owned();
self
}

pub fn created_at(mut self, created_at: &str) -> Self {
self.created_at = created_at.to_string();
self.created_at = created_at.to_owned();
self
}

pub fn updated_at(mut self, updated_at: &str) -> Self {
self.updated_at = updated_at.to_string();
self.updated_at = updated_at.to_owned();
self
}

Expand Down
10 changes: 5 additions & 5 deletions link-for-later/src/entity/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ pub struct InfoBuilder {
impl InfoBuilder {
pub fn new(email: &str, password: &str) -> Self {
Self {
email: email.to_string(),
password: password.to_string(),
email: email.to_owned(),
password: password.to_owned(),
..Default::default()
}
}

pub fn id(mut self, id: &str) -> Self {
self.id = id.to_string();
self.id = id.to_owned();
self
}

Expand All @@ -51,12 +51,12 @@ impl InfoBuilder {
}

pub fn created_at(mut self, created_at: &str) -> Self {
self.created_at = created_at.to_string();
self.created_at = created_at.to_owned();
self
}

pub fn updated_at(mut self, updated_at: &str) -> Self {
self.updated_at = updated_at.to_string();
self.updated_at = updated_at.to_owned();
self
}

Expand Down
35 changes: 34 additions & 1 deletion link-for-later/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,37 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![warn(
clippy::all,
clippy::pedantic,
clippy::nursery,
clippy::cargo,
clippy::style,
clippy::suspicious,
clippy::complexity,
clippy::perf,
//clippy::restriction,
clippy::todo,
clippy::mem_forget,
clippy::lossy_float_literal,
clippy::rest_pat_in_fully_bound_structs,
clippy::exit,
clippy::verbose_file_reads,
clippy::str_to_string,
clippy::unwrap_used,
clippy::expect_used,
rust_2018_idioms,
future_incompatible,
nonstandard_style,
missing_debug_implementations,
unused
)]
#![forbid(unsafe_code)]
#![allow(elided_lifetimes_in_paths)]
#![cfg_attr(
test,
allow(clippy::str_to_string, clippy::unwrap_used, clippy::expect_used,)
)]

pub use types::Database as DatabaseType;
pub mod app;
Expand Down
49 changes: 37 additions & 12 deletions link-for-later/src/repository/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl LinksRepository for LinksRepositoryProvider {
let filtered_links: Vec<LinkItem> = self
.links_data
.lock()
.unwrap()
.map_err(|e| AppError::Database(format!("find() {e:?}")))?
.iter()
.filter(|link| {
(link.id() == query.id() || query.id().is_empty())
Expand All @@ -58,33 +58,47 @@ impl LinksRepository for LinksRepositoryProvider {
async fn get(&self, query: &LinkQuery) -> Result<LinkItem> {
self.links_data
.lock()
.unwrap()
.map_err(|e| AppError::Database(format!("get() {e:?}")))?
.iter()
.find(|link| link.id() == query.id() && link.owner() == query.owner())
.cloned()
.ok_or_else(|| AppError::LinkNotFound(query.id().to_owned()))
}

async fn create(&self, item: &LinkItem) -> Result<LinkItem> {
let id = self.links_data_counter.lock().unwrap().len() + 1;
let id = self
.links_data_counter
.lock()
.map_err(|e| AppError::Database(format!("create() {e:?}")))?
.len()
+ 1;
let link = LinkItemBuilder::from(item.clone())
.id(&id.to_string())
.build();
self.links_data.lock().unwrap().push(link.clone());
self.links_data_counter.lock().unwrap().push(id);
self.links_data
.lock()
.map_err(|e| AppError::Database(format!("create() {e:?}")))?
.push(link.clone());
self.links_data_counter
.lock()
.map_err(|e| AppError::Database(format!("create() {e:?}")))?
.push(id);
Ok(link)
}

async fn update(&self, id: &str, item: &LinkItem) -> Result<LinkItem> {
self.links_data
.lock()
.unwrap()
.map_err(|e| AppError::Database(format!("update() {e:?}")))?
.iter()
.find(|link| link.id() == id && link.owner() == item.owner())
.cloned()
.ok_or_else(|| AppError::LinkNotFound(id.to_owned()))?;
self.delete(item).await?;
self.links_data.lock().unwrap().push(item.clone());
self.links_data
.lock()
.map_err(|e| AppError::Database(format!("update() {e:?}")))?
.push(item.clone());
Ok(item.clone())
}

Expand All @@ -93,7 +107,7 @@ impl LinksRepository for LinksRepositoryProvider {
self.get(&query).await?;
self.links_data
.lock()
.unwrap()
.map_err(|e| AppError::Database(format!("delete() {e:?}")))?
.retain(|link| link.id() != query.id());
Ok(())
}
Expand All @@ -104,20 +118,31 @@ impl UsersRepository for UsersRepositoryProvider {
async fn get(&self, query: &UserQuery) -> Result<UserInfo> {
self.users_data
.lock()
.unwrap()
.map_err(|e| AppError::Database(format!("get() {e:?}")))?
.iter()
.find(|user| user.email() == query.email())
.cloned()
.ok_or_else(|| AppError::UserNotFound(query.email().to_owned()))
}

async fn create(&self, info: &UserInfo) -> Result<UserInfo> {
let id = self.users_data_counter.lock().unwrap().len() + 1;
let id = self
.users_data_counter
.lock()
.map_err(|e| AppError::Database(format!("create() {e:?}")))?
.len()
+ 1;
let user = UserInfoBuilder::from(info.clone())
.id(&id.to_string())
.build();
self.users_data.lock().unwrap().push(user.clone());
self.users_data_counter.lock().unwrap().push(id);
self.users_data
.lock()
.map_err(|e| AppError::Database(format!("create() {e:?}")))?
.push(user.clone());
self.users_data_counter
.lock()
.map_err(|e| AppError::Database(format!("create() {e:?}")))?
.push(id);
Ok(user)
}
}
Expand Down
4 changes: 2 additions & 2 deletions link-for-later/src/repository/mongodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct UsersRepositoryProvider {
impl LinksRepositoryProvider {
pub fn new(db: &Database) -> Self {
let collection_name = std::env::var(LINKS_COLLECTION_NAME_KEY)
.unwrap_or_else(|_| LINKS_COLLECTION_NAME_DEFAULT.to_string());
.unwrap_or_else(|_| LINKS_COLLECTION_NAME_DEFAULT.to_owned());
let links_collection = db.collection::<LinkItem>(&collection_name);
Self { links_collection }
}
Expand All @@ -37,7 +37,7 @@ impl LinksRepositoryProvider {
impl UsersRepositoryProvider {
pub fn new(db: &Database) -> Self {
let collection_name = std::env::var(USERS_COLLECTION_NAME_KEY)
.unwrap_or_else(|_| USERS_COLLECTION_NAME_DEFAULT.to_string());
.unwrap_or_else(|_| USERS_COLLECTION_NAME_DEFAULT.to_owned());
let users_collection = db.collection::<UserInfo>(&collection_name);
Self { users_collection }
}
Expand Down
1 change: 1 addition & 0 deletions link-for-later/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub type Result<T> = std::result::Result<T, AppError>;
pub type AppState = crate::app::State;
pub type AppError = crate::app::Error;

#[derive(Debug)]
pub enum Database {
MongoDb(mongodb::Database),
InMemory,
Expand Down
1 change: 0 additions & 1 deletion link-for-later/tests/app/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
use axum::Router;

use crate::repository::{mongodb, DatabaseType};
Expand Down
1 change: 0 additions & 1 deletion link-for-later/tests/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
use chrono::Utc;
use jsonwebtoken::{encode, EncodingKey, Header};
use serde::{Deserialize, Serialize};
Expand Down
2 changes: 2 additions & 0 deletions link-for-later/tests/links.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use axum::{
body::Body,
http::{Request, StatusCode},
Expand Down
1 change: 0 additions & 1 deletion link-for-later/tests/repository/inmemory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
use axum::async_trait;

use crate::entity::{LinkItem, UserInfo};
Expand Down
2 changes: 0 additions & 2 deletions link-for-later/tests/repository/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

use axum::async_trait;

use crate::entity::{LinkItem, UserInfo};
Expand Down
1 change: 0 additions & 1 deletion link-for-later/tests/repository/mongodb.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(dead_code)]
use std::str::FromStr;

use axum::async_trait;
Expand Down
2 changes: 2 additions & 0 deletions link-for-later/tests/users.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(dead_code)]

use argon2::{
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
Argon2,
Expand Down

0 comments on commit 0a167e9

Please sign in to comment.