From 45c0a0030a5d73279272f84f848db81731fb86b9 Mon Sep 17 00:00:00 2001 From: privacyguard Date: Sun, 7 Jul 2024 01:48:10 +0300 Subject: [PATCH] use derive_new::new instead of TypedBuilder --- crates/api_crud/src/oauth_provider/create.rs | 34 +++++++++---------- crates/api_crud/src/oauth_provider/update.rs | 33 +++++++++--------- crates/api_crud/src/user/create.rs | 14 +++----- crates/db_schema/src/source/oauth_account.rs | 5 ++- crates/db_schema/src/source/oauth_provider.rs | 5 ++- 5 files changed, 42 insertions(+), 49 deletions(-) diff --git a/crates/api_crud/src/oauth_provider/create.rs b/crates/api_crud/src/oauth_provider/create.rs index bbf08404b..f0b091384 100644 --- a/crates/api_crud/src/oauth_provider/create.rs +++ b/crates/api_crud/src/oauth_provider/create.rs @@ -36,23 +36,23 @@ pub async fn create_oauth_provider( reader.read(&mut id_bytes); let cloned_data = data.clone(); - let oauth_provider_form = OAuthProviderInsertForm::builder() - .id(OAuthProviderId(i64::from_ne_bytes(id_bytes))) - .display_name(cloned_data.display_name) - .issuer(Url::parse(&cloned_data.issuer)?.into()) - .authorization_endpoint(Url::parse(&cloned_data.authorization_endpoint)?.into()) - .token_endpoint(Url::parse(&cloned_data.token_endpoint)?.into()) - .userinfo_endpoint(Url::parse(&cloned_data.userinfo_endpoint)?.into()) - .id_claim(cloned_data.id_claim) - .name_claim(cloned_data.name_claim) - .client_id(data.client_id.to_string()) - .client_secret(data.client_secret.to_string()) - .scopes(data.scopes.to_string()) - .auto_verify_email(data.auto_verify_email) - .auto_approve_application(data.auto_approve_application) - .account_linking_enabled(data.account_linking_enabled) - .enabled(data.enabled) - .build(); + let oauth_provider_form = OAuthProviderInsertForm { + id: OAuthProviderId(i64::from_ne_bytes(id_bytes)), + display_name: cloned_data.display_name, + issuer: Url::parse(&cloned_data.issuer)?.into(), + authorization_endpoint: Url::parse(&cloned_data.authorization_endpoint)?.into(), + token_endpoint: Url::parse(&cloned_data.token_endpoint)?.into(), + userinfo_endpoint: Url::parse(&cloned_data.userinfo_endpoint)?.into(), + id_claim: cloned_data.id_claim, + name_claim: cloned_data.name_claim, + client_id: data.client_id.to_string(), + client_secret: data.client_secret.to_string(), + scopes: data.scopes.to_string(), + auto_verify_email: data.auto_verify_email, + auto_approve_application: data.auto_approve_application, + account_linking_enabled: data.account_linking_enabled, + enabled: data.enabled, + }; let unsafe_oauth_provider = UnsafeOAuthProvider::create(&mut context.pool(), &oauth_provider_form).await?; Ok(Json(OAuthProvider::from_unsafe(&unsafe_oauth_provider))) diff --git a/crates/api_crud/src/oauth_provider/update.rs b/crates/api_crud/src/oauth_provider/update.rs index 5a21c5f39..f368b4228 100644 --- a/crates/api_crud/src/oauth_provider/update.rs +++ b/crates/api_crud/src/oauth_provider/update.rs @@ -20,27 +20,28 @@ pub async fn update_oauth_provider( is_admin(&local_user_view)?; let cloned_data = data.clone(); - let oauth_provider_form = OAuthProviderUpdateForm::builder() - .display_name(cloned_data.display_name) - .authorization_endpoint(Url::parse(&cloned_data.authorization_endpoint)?.into()) - .token_endpoint(Url::parse(&cloned_data.token_endpoint)?.into()) - .userinfo_endpoint(Url::parse(&cloned_data.userinfo_endpoint)?.into()) - .id_claim(data.id_claim.to_string()) - .name_claim(data.name_claim.to_string()) - .client_secret(if !data.client_secret.is_empty() { + let oauth_provider_form = OAuthProviderUpdateForm { + display_name: cloned_data.display_name, + authorization_endpoint: Url::parse(&cloned_data.authorization_endpoint)?.into(), + token_endpoint: Url::parse(&cloned_data.token_endpoint)?.into(), + userinfo_endpoint: Url::parse(&cloned_data.userinfo_endpoint)?.into(), + id_claim: data.id_claim.to_string(), + name_claim: data.name_claim.to_string(), + client_secret: if !data.client_secret.is_empty() { Some(data.client_secret.to_string()) } else { None - }) - .scopes(data.scopes.to_string()) - .auto_verify_email(data.auto_verify_email) - .auto_approve_application(data.auto_approve_application) - .account_linking_enabled(data.account_linking_enabled) - .enabled(data.enabled) - .updated(naive_now()); + }, + scopes: data.scopes.to_string(), + auto_verify_email: data.auto_verify_email, + auto_approve_application: data.auto_approve_application, + account_linking_enabled: data.account_linking_enabled, + enabled: data.enabled, + updated: naive_now(), + }; let update_result = - UnsafeOAuthProvider::update(&mut context.pool(), data.id, &oauth_provider_form.build()).await?; + UnsafeOAuthProvider::update(&mut context.pool(), data.id, &oauth_provider_form).await?; let unsafe_oauth_provider = UnsafeOAuthProvider::read(&mut context.pool(), update_result.id) .await? .ok_or(LemmyErrorType::CouldntFindOauthProvider)?; diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 074bf6ae8..09499f09b 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -390,11 +390,8 @@ pub async fn authenticate_with_oauth( if oauth_provider.account_linking_enabled { // Link with OAUTH => Login user - let oauth_account_form = OAuthAccountInsertForm::builder() - .local_user_id(user_view.local_user.id) - .oauth_provider_id(oauth_provider.id) - .oauth_user_id(oauth_user_id) - .build(); + let oauth_account_form = + OAuthAccountInsertForm::new(user_view.local_user.id, oauth_provider.id, oauth_user_id); OAuthAccount::create(&mut context.pool(), &oauth_account_form) .await @@ -452,11 +449,8 @@ pub async fn authenticate_with_oauth( .ok_or(LemmyErrorType::OauthLoginFailed)?; // Create the oauth account - let oauth_account_form = OAuthAccountInsertForm::builder() - .local_user_id(local_user.id) - .oauth_provider_id(oauth_provider.id) - .oauth_user_id(oauth_user_id) - .build(); + let oauth_account_form = + OAuthAccountInsertForm::new(local_user.id, oauth_provider.id, oauth_user_id); OAuthAccount::create(&mut context.pool(), &oauth_account_form) .await diff --git a/crates/db_schema/src/source/oauth_account.rs b/crates/db_schema/src/source/oauth_account.rs index 1df3ef1e7..87d0ba6ab 100644 --- a/crates/db_schema/src/source/oauth_account.rs +++ b/crates/db_schema/src/source/oauth_account.rs @@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; -use typed_builder::TypedBuilder; #[skip_serializing_none] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] @@ -24,7 +23,7 @@ pub struct OAuthAccount { pub updated: Option>, } -#[derive(Debug, Clone, TypedBuilder)] +#[derive(Debug, Clone, derive_new::new)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = oauth_account))] pub struct OAuthAccountInsertForm { @@ -33,7 +32,7 @@ pub struct OAuthAccountInsertForm { pub oauth_user_id: String, } -#[derive(Debug, Clone, TypedBuilder)] +#[derive(Debug, Clone, derive_new::new)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = oauth_account))] pub struct OAuthAccountUpdateForm { diff --git a/crates/db_schema/src/source/oauth_provider.rs b/crates/db_schema/src/source/oauth_provider.rs index 4121a5382..a1786baf7 100644 --- a/crates/db_schema/src/source/oauth_provider.rs +++ b/crates/db_schema/src/source/oauth_provider.rs @@ -6,7 +6,6 @@ use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; -use typed_builder::TypedBuilder; #[skip_serializing_none] #[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] @@ -110,7 +109,7 @@ pub struct OAuthProvider { pub updated: Option>, } -#[derive(Debug, Clone, TypedBuilder)] +#[derive(Debug, Clone)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset, TS))] #[cfg_attr(feature = "full", diesel(table_name = oauth_provider))] #[cfg_attr(feature = "full", ts(export))] @@ -136,7 +135,7 @@ pub struct OAuthProviderInsertForm { pub enabled: bool, } -#[derive(Debug, Clone, TypedBuilder)] +#[derive(Debug, Clone)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset, TS))] #[cfg_attr(feature = "full", diesel(table_name = oauth_provider))] #[cfg_attr(feature = "full", ts(export))]