Skip to content

Commit

Permalink
refactor: remove useless from_str for db enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ParzivalEugene committed Aug 7, 2024
1 parent ab1f934 commit 780d290
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 51 deletions.
15 changes: 2 additions & 13 deletions src/data/enums/country.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::enums::errors::internal::{CountryError, InternalError};
use diesel::{
deserialize::{self, FromSql, FromSqlRow},
expression::AsExpression,
pg::{Pg, PgValue},
serialize::{self, IsNull, Output, ToSql},
};
use serde::{Deserialize, Serialize};
use std::io::Write;

#[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[diesel(sql_type = crate::data::schema::sql_types::Country)]
#[allow(clippy::upper_case_acronyms)]
pub enum Country {
Expand All @@ -16,17 +16,6 @@ pub enum Country {
Germany,
}

impl Country {
pub fn from_str(s: &str) -> Result<Country, InternalError> {
match s {
"UK" => Ok(Country::UK),
"USA" => Ok(Country::USA),
"Germany" => Ok(Country::Germany),
_ => Err(InternalError::CountryError(CountryError::CountryNotFound)),
}
}
}

impl ToSql<crate::data::schema::sql_types::Country, Pg> for Country {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
match *self {
Expand Down
13 changes: 0 additions & 13 deletions src/data/enums/oauth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use diesel::{
use serde::{Deserialize, Serialize};
use std::io::Write;

use crate::enums::errors::internal::{AuthError, InternalError};

#[derive(Debug, AsExpression, FromSqlRow, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
#[diesel(sql_type = crate::data::schema::sql_types::OAuthProvider)]
pub enum OAuthProvider {
Expand All @@ -17,17 +15,6 @@ pub enum OAuthProvider {
Discord,
}

impl OAuthProvider {
pub fn from_str(s: &str) -> Result<Self, InternalError> {
match s {
"Github" => Ok(OAuthProvider::Github),
"Google" => Ok(OAuthProvider::Google),
"Discord" => Ok(OAuthProvider::Discord),
_ => Err(InternalError::AuthError(AuthError::UnknownOAuthProvider)),
}
}
}

impl ToSql<crate::data::schema::sql_types::OAuthProvider, Pg> for OAuthProvider {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
match *self {
Expand Down
13 changes: 0 additions & 13 deletions src/data/enums/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ pub enum OS {
Unknown,
}

impl OS {
pub fn from_str(s: &str) -> Self {
match s {
"Windows" => OS::Windows,
"Linux" => OS::Linux,
"MacOS" => OS::MacOS,
"Android" => OS::Android,
"IOS" => OS::IOS,
_ => OS::Unknown,
}
}
}

impl ToSql<crate::data::schema::sql_types::Os, Pg> for OS {
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
match *self {
Expand Down
4 changes: 2 additions & 2 deletions src/dto/auth/request/authorize.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::dto::device::internal::DeviceInfo;
use crate::{data::enums::OAuthProvider, dto::device::internal::DeviceInfo};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
pub struct AuthorizeRequest {
pub code: String,
pub provider: String,
pub provider: OAuthProvider,
pub device: DeviceInfo,
}
3 changes: 2 additions & 1 deletion src/dto/device/internal/device_info.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serde::{Deserialize, Serialize};

Check warning on line 1 in src/dto/device/internal/device_info.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/master-backend-rust/master-backend-rust/src/dto/device/internal/device_info.rs
use utoipa::ToSchema;
use crate::data::enums::OS;

#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub struct DeviceInfo {
#[schema(example = "Android")]
pub os: String,
pub os: OS,
#[schema(example = "Vitya Phone")]
pub name: String,
}
3 changes: 2 additions & 1 deletion src/dto/session/request/create_session.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::data::enums::Country;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)]
pub struct CreateSession {
pub country: String,
pub country: Country,
}
3 changes: 1 addition & 2 deletions src/handlers/auth/authorize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{
data::enums::OAuthProvider,
dto::{
auth::{internal::OAuthCode, request::AuthorizeRequest, response::Tokens},
device::internal::DeviceInfo,
Expand All @@ -21,7 +20,7 @@ pub async fn authorize(
&state,
&OAuthCode {
code: payload.code.clone(),
provider: OAuthProvider::from_str(&payload.provider)?,
provider: payload.provider,
},
&DeviceInfo {
os: payload.device.os,
Expand Down
4 changes: 1 addition & 3 deletions src/handlers/session/create_session.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{
data::enums::Country,
dto::{
auth::internal::AccessToken,
response::success::Response,
Expand Down Expand Up @@ -61,8 +60,7 @@ pub async fn create_session(
State(pool): State<Pool>,

Check warning on line 60 in src/handlers/session/create_session.rs

View workflow job for this annotation

GitHub Actions / cargo fmt

Diff in /home/runner/work/master-backend-rust/master-backend-rust/src/handlers/session/create_session.rs
Json(payload): Json<CreateSession>,
) -> Result<Response<Session>, ExternalError> {
let country = Country::from_str(&payload.country)?;
let session = session_service::create_session(&pool, &claims.device_id, &country).await?;
let session = session_service::create_session(&pool, &claims.device_id, &payload.country).await?;

info!("Session created successfully: {}", session.session_id);

Expand Down
6 changes: 3 additions & 3 deletions src/services/user_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub async fn sign_in(

let device = NewDevice {
name: device.name.clone(),
os: OS::from_str(&device.os),
os: device.os,
user_id: user_db.user.id,
};
let device = device_service::add_device(pool, &device).await?;
Expand Down Expand Up @@ -297,7 +297,7 @@ pub async fn sign_up(

let device = NewDevice {
name: device.name.clone(),
os: OS::from_str(&device.os),
os: device.os,
user_id: current_user.user.id,
};

Expand Down Expand Up @@ -335,7 +335,7 @@ pub async fn authorize(

let device = NewDevice {
name: device.name.clone(),
os: OS::from_str(&device.os),
os: device.os,
user_id: user.user.id,
};

Expand Down

0 comments on commit 780d290

Please sign in to comment.