From 11287fea5224602d98a71cf92710c84a67e061b0 Mon Sep 17 00:00:00 2001 From: maikb Date: Thu, 21 Dec 2023 20:12:21 +0100 Subject: [PATCH] style(backend): Reformat code and adjust import order This commit reforms code formatting for better readability and adjusts the import order for better organization. It changes multiple files, while no actual functionality is changed. The more structured code can improve understanding and ease of maintenance. Refs: #8 --- src-tauri/build.rs | 2 +- src-tauri/src/common.rs | 2 +- src-tauri/src/common/test.rs | 5 +- src-tauri/src/main.rs | 6 +- src-tauri/src/profile.rs | 2 +- src-tauri/src/profile/application.rs | 2 +- src-tauri/src/profile/application/tauri.rs | 2 +- .../application/tauri/profile_handler.rs | 8 +- src-tauri/src/profile/core.rs | 2 +- src-tauri/src/profile/core/api.rs | 2 +- src-tauri/src/profile/core/domain.rs | 39 +++++++--- src-tauri/src/profile/core/error.rs | 11 ++- src-tauri/src/profile/core/spi.rs | 2 +- src-tauri/src/profile/infrastructure.rs | 2 +- src-tauri/src/profile/infrastructure/aws.rs | 2 +- .../profile/infrastructure/aws/sdk_config.rs | 2 +- .../aws/sdk_config/sdk_config_adapter.rs | 27 +++---- src-tauri/tests/profile_file_adapter_test.rs | 74 +++++++++++++------ 18 files changed, 125 insertions(+), 67 deletions(-) diff --git a/src-tauri/build.rs b/src-tauri/build.rs index 795b9b7..d860e1e 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -1,3 +1,3 @@ fn main() { - tauri_build::build() + tauri_build::build() } diff --git a/src-tauri/src/common.rs b/src-tauri/src/common.rs index 073f984..bcc92d7 100644 --- a/src-tauri/src/common.rs +++ b/src-tauri/src/common.rs @@ -1 +1 @@ -pub(crate) mod test; \ No newline at end of file +pub(crate) mod test; diff --git a/src-tauri/src/common/test.rs b/src-tauri/src/common/test.rs index 47e7710..57e567c 100644 --- a/src-tauri/src/common/test.rs +++ b/src-tauri/src/common/test.rs @@ -3,7 +3,8 @@ pub mod report_utils { use error_stack::{AttachmentKind, FrameKind, Report}; pub fn messages(report: Report) -> Vec { - report.frames() + report + .frames() .map(|frame| { if let FrameKind::Attachment(AttachmentKind::Printable(attachment)) = frame.kind() { Some(attachment.to_string()) @@ -14,4 +15,4 @@ pub mod report_utils { .filter_map(|result| result) .collect() } -} \ No newline at end of file +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f9b1e1d..8a85796 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,12 +1,12 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use std::sync::Arc; -use app::profile::core::domain::ProfileService; -use app::profile::infrastructure::aws::sdk_config::sdk_config_adapter::SdkConfigAdapter; use app::__cmd__get_profiles; use app::profile::application::tauri::profile_handler::get_profiles; use app::profile::core::api::ProfileAPI; +use app::profile::core::domain::ProfileService; +use app::profile::infrastructure::aws::sdk_config::sdk_config_adapter::SdkConfigAdapter; +use std::sync::Arc; fn main() { let profile_data_spi = SdkConfigAdapter; diff --git a/src-tauri/src/profile.rs b/src-tauri/src/profile.rs index e44212b..bfcfc29 100644 --- a/src-tauri/src/profile.rs +++ b/src-tauri/src/profile.rs @@ -1,3 +1,3 @@ pub mod application; pub mod core; -pub mod infrastructure; \ No newline at end of file +pub mod infrastructure; diff --git a/src-tauri/src/profile/application.rs b/src-tauri/src/profile/application.rs index 01e9a2f..62411f7 100644 --- a/src-tauri/src/profile/application.rs +++ b/src-tauri/src/profile/application.rs @@ -1 +1 @@ -pub mod tauri; \ No newline at end of file +pub mod tauri; diff --git a/src-tauri/src/profile/application/tauri.rs b/src-tauri/src/profile/application/tauri.rs index 6ef7d94..acae475 100644 --- a/src-tauri/src/profile/application/tauri.rs +++ b/src-tauri/src/profile/application/tauri.rs @@ -1 +1 @@ -pub mod profile_handler; \ No newline at end of file +pub mod profile_handler; diff --git a/src-tauri/src/profile/application/tauri/profile_handler.rs b/src-tauri/src/profile/application/tauri/profile_handler.rs index 3206b98..86291ae 100644 --- a/src-tauri/src/profile/application/tauri/profile_handler.rs +++ b/src-tauri/src/profile/application/tauri/profile_handler.rs @@ -1,5 +1,5 @@ -use std::sync::Arc; use crate::profile::core::api::ProfileAPI; +use std::sync::Arc; #[derive(serde::Serialize)] pub struct GetConfigProfilesResponse {} @@ -8,6 +8,8 @@ pub struct GetConfigProfilesResponse {} pub struct ErrorResponse {} #[tauri::command] -pub async fn get_profiles(_api: tauri::State<'_, Arc>) -> Result { +pub async fn get_profiles( + _api: tauri::State<'_, Arc>, +) -> Result { todo!() -} \ No newline at end of file +} diff --git a/src-tauri/src/profile/core.rs b/src-tauri/src/profile/core.rs index 76228e7..d6cd593 100644 --- a/src-tauri/src/profile/core.rs +++ b/src-tauri/src/profile/core.rs @@ -1,4 +1,4 @@ pub mod api; -pub mod spi; pub mod domain; pub mod error; +pub mod spi; diff --git a/src-tauri/src/profile/core/api.rs b/src-tauri/src/profile/core/api.rs index 1022cb7..664c049 100644 --- a/src-tauri/src/profile/core/api.rs +++ b/src-tauri/src/profile/core/api.rs @@ -6,4 +6,4 @@ use crate::profile::core::error::ProfileError; #[async_trait] pub trait ProfileAPI: Send + Sync { async fn get_profiles<'a>(&'a self) -> error_stack::Result; -} \ No newline at end of file +} diff --git a/src-tauri/src/profile/core/domain.rs b/src-tauri/src/profile/core/domain.rs index 0676eb0..f99fb01 100644 --- a/src-tauri/src/profile/core/domain.rs +++ b/src-tauri/src/profile/core/domain.rs @@ -18,7 +18,10 @@ pub struct Credentials { impl Credentials { pub fn new(access_key_id: Option, secret_access_key: Option) -> Self { - Self { access_key_id, secret_access_key } + Self { + access_key_id, + secret_access_key, + } } } @@ -30,7 +33,10 @@ pub struct Config { impl Config { pub fn new(region: Option, output_format: Option) -> Self { - Self { region, output_format } + Self { + region, + output_format, + } } } @@ -42,7 +48,10 @@ pub struct Settings { impl Settings { pub fn new(credentials: Credentials, config: Config) -> Self { - Self { credentials: Some(credentials), config: Some(config) } + Self { + credentials: Some(credentials), + config: Some(config), + } } } @@ -57,7 +66,10 @@ pub struct ProfileSet { impl ProfileSet { pub fn new() -> Self { - Self { profiles: HashMap::new(), errors: Vec::new() } + Self { + profiles: HashMap::new(), + errors: Vec::new(), + } } pub fn add_profile(&mut self, name: &str, settings: Settings) -> Result<(), ProfileError> { @@ -99,8 +111,8 @@ impl ProfileAPI for ProfileService { #[cfg(test)] mod tests { - use fake::Fake; use fake::faker::lorem::en::Word; + use fake::Fake; use rstest::rstest; use spectral::prelude::*; @@ -113,7 +125,9 @@ mod tests { #[test] fn should_add_profile() { let mut cut: ProfileSet = ProfileSet::new(); - let input_settings: Settings = Settings { ..Default::default() }; + let input_settings: Settings = Settings { + ..Default::default() + }; let input_profile: String = Word().fake(); cut.add_profile(&input_profile, input_settings.clone()) @@ -128,7 +142,9 @@ mod tests { #[case(" ")] fn should_return_error_when_key_is_blank(#[case] input_profile: &str) { let mut cut: ProfileSet = ProfileSet::new(); - let input_settings: Settings = Settings { ..Default::default() }; + let input_settings: Settings = Settings { + ..Default::default() + }; let actual = cut.add_profile(input_profile, input_settings); @@ -143,7 +159,9 @@ mod tests { #[test] fn should_return_profiles() { let mut cut: ProfileSet = ProfileSet::new(); - let input_settings: Settings = Settings { ..Default::default() }; + let input_settings: Settings = Settings { + ..Default::default() + }; let input_profile = Word().fake(); cut.add_profile(input_profile, input_settings) @@ -156,7 +174,8 @@ mod tests { #[tokio::test] async fn should_load_profile_data_and_return_the_result() { let mut mock_profile_data_spi = MockProfileDataSPI::new(); - mock_profile_data_spi.expect_load_profile_data() + mock_profile_data_spi + .expect_load_profile_data() .return_once(|| Ok(ProfileSet::new())); let cut = ProfileService::new(Arc::new(mock_profile_data_spi)); @@ -164,4 +183,4 @@ mod tests { assert_that!(actual).is_ok(); } -} \ No newline at end of file +} diff --git a/src-tauri/src/profile/core/error.rs b/src-tauri/src/profile/core/error.rs index f432e86..3bc1278 100644 --- a/src-tauri/src/profile/core/error.rs +++ b/src-tauri/src/profile/core/error.rs @@ -1,5 +1,5 @@ +use error_stack::Context; use std::fmt::{Debug, Display, Formatter}; -use error_stack::{Context}; #[derive(Debug, Eq, PartialEq)] pub enum ProfileError { @@ -11,7 +11,7 @@ impl Display for ProfileError { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { ProfileError::InvalidProfileNameError => write!(f, "invalid profile name"), - ProfileError::ProfileDataLoadError => write!(f, "failed to load profiles") + ProfileError::ProfileDataLoadError => write!(f, "failed to load profiles"), } } } @@ -26,7 +26,10 @@ mod tests { fn given_invalid_profile_error_should_output_expected_error_message_when_printed() { let expected = "invalid profile name"; - assert_eq!(format!("{}", ProfileError::InvalidProfileNameError), expected) + assert_eq!( + format!("{}", ProfileError::InvalidProfileNameError), + expected + ) } #[test] @@ -35,4 +38,4 @@ mod tests { assert_eq!(format!("{}", ProfileError::ProfileDataLoadError), expected) } -} \ No newline at end of file +} diff --git a/src-tauri/src/profile/core/spi.rs b/src-tauri/src/profile/core/spi.rs index 4913ec3..35a2ae8 100644 --- a/src-tauri/src/profile/core/spi.rs +++ b/src-tauri/src/profile/core/spi.rs @@ -10,4 +10,4 @@ use crate::profile::core::error::ProfileError; #[async_trait] pub trait ProfileDataSPI: Send + Sync { async fn load_profile_data(&self) -> Result; -} \ No newline at end of file +} diff --git a/src-tauri/src/profile/infrastructure.rs b/src-tauri/src/profile/infrastructure.rs index 6d6fbcb..5e5eeaa 100644 --- a/src-tauri/src/profile/infrastructure.rs +++ b/src-tauri/src/profile/infrastructure.rs @@ -1 +1 @@ -pub mod aws; \ No newline at end of file +pub mod aws; diff --git a/src-tauri/src/profile/infrastructure/aws.rs b/src-tauri/src/profile/infrastructure/aws.rs index 3e13791..17ad3e9 100644 --- a/src-tauri/src/profile/infrastructure/aws.rs +++ b/src-tauri/src/profile/infrastructure/aws.rs @@ -1 +1 @@ -pub mod sdk_config; \ No newline at end of file +pub mod sdk_config; diff --git a/src-tauri/src/profile/infrastructure/aws/sdk_config.rs b/src-tauri/src/profile/infrastructure/aws/sdk_config.rs index 6d1dcf4..4fc28b7 100644 --- a/src-tauri/src/profile/infrastructure/aws/sdk_config.rs +++ b/src-tauri/src/profile/infrastructure/aws/sdk_config.rs @@ -1 +1 @@ -pub mod sdk_config_adapter; \ No newline at end of file +pub mod sdk_config_adapter; diff --git a/src-tauri/src/profile/infrastructure/aws/sdk_config/sdk_config_adapter.rs b/src-tauri/src/profile/infrastructure/aws/sdk_config/sdk_config_adapter.rs index 31e983f..2a220dd 100644 --- a/src-tauri/src/profile/infrastructure/aws/sdk_config/sdk_config_adapter.rs +++ b/src-tauri/src/profile/infrastructure/aws/sdk_config/sdk_config_adapter.rs @@ -18,7 +18,8 @@ impl ProfileDataSPI for SdkConfigAdapter { &Default::default(), &Default::default(), None, - ).await; + ) + .await; match result { Ok(profile_set) => { @@ -36,35 +37,35 @@ impl ProfileDataSPI for SdkConfigAdapter { configuration.errors.push(e); } } else { - panic!("profile set should contain profile name: `{}`", profile_name) + panic!( + "profile set should contain profile name: `{}`", + profile_name + ) } } Ok(configuration) } - Err(e) => { - Err(Report::from(e) - .change_context(ProfileError::ProfileDataLoadError)) - } + Err(e) => Err(Report::from(e).change_context(ProfileError::ProfileDataLoadError)), } } } fn extract_config(profile: &Profile) -> Config { - let output_format = profile.get("output") - .map(|value| value.to_string()); - let region = profile.get("region") - .map(|value| value.to_string()); + let output_format = profile.get("output").map(|value| value.to_string()); + let region = profile.get("region").map(|value| value.to_string()); let config = Config::new(region, output_format); config } fn extract_credentials(profile: &Profile) -> Credentials { - let access_key_id = profile.get("aws_access_key_id") + let access_key_id = profile + .get("aws_access_key_id") .map(|value| value.to_string()); - let secret_access_key = profile.get("aws_secret_access_key") + let secret_access_key = profile + .get("aws_secret_access_key") .map(|value| SecStr::from(value)); let credentials = Credentials::new(access_key_id, secret_access_key); credentials -} \ No newline at end of file +} diff --git a/src-tauri/tests/profile_file_adapter_test.rs b/src-tauri/tests/profile_file_adapter_test.rs index 18f77fc..24049d7 100644 --- a/src-tauri/tests/profile_file_adapter_test.rs +++ b/src-tauri/tests/profile_file_adapter_test.rs @@ -6,8 +6,8 @@ mod tests { use secstr::SecStr; use serial_test::serial; use spectral::prelude::*; - use tempfile::{TempDir, tempdir}; - use test_context::{AsyncTestContext, test_context}; + use tempfile::{tempdir, TempDir}; + use test_context::{test_context, AsyncTestContext}; use app::profile::core::domain::{Config, Credentials, Settings}; use app::profile::core::error::ProfileError; @@ -31,7 +31,8 @@ mod tests { fs::create_dir_all(&test_aws_dir_path).unwrap(); let mut test_config = Ini::new(); - test_config.with_section(Some("profile dev")) + test_config + .with_section(Some("profile dev")) .set("region", "eu-west-1") .set("output", "json"); let test_config_file_path = test_aws_dir_path.join("config"); @@ -39,11 +40,14 @@ mod tests { env::set_var("AWS_CONFIG_FILE", test_config_file_path); let mut test_credentials = Ini::new(); - test_credentials.with_section(Some("dev")) + test_credentials + .with_section(Some("dev")) .set("aws_access_key_id", "devAccessKeyID") .set("aws_secret_access_key", "devSecretAccessKey"); let test_credentials_file_path = test_aws_dir_path.join("credentials"); - test_credentials.write_to_file(&test_credentials_file_path).unwrap(); + test_credentials + .write_to_file(&test_credentials_file_path) + .unwrap(); env::set_var("AWS_SHARED_CREDENTIALS_FILE", test_credentials_file_path); ValidContext { @@ -54,8 +58,16 @@ mod tests { } async fn teardown(self) { - env::set_var("AWS_CONFIG_FILE", self.original_config_file_location.as_deref().unwrap_or("")); - env::set_var("AWS_SHARED_CREDENTIALS_FILE", self.original_credentials_file_location.as_deref().unwrap_or("")); + env::set_var( + "AWS_CONFIG_FILE", + self.original_config_file_location.as_deref().unwrap_or(""), + ); + env::set_var( + "AWS_SHARED_CREDENTIALS_FILE", + self.original_credentials_file_location + .as_deref() + .unwrap_or(""), + ); } } @@ -76,7 +88,8 @@ mod tests { fs::create_dir_all(&test_aws_dir_path).unwrap(); let mut test_config = Ini::new(); - test_config.with_section(Some("")) + test_config + .with_section(Some("")) .set("region", "eu-west-1") .set("output", "json"); let test_config_file_path = test_aws_dir_path.join("config"); @@ -84,11 +97,14 @@ mod tests { env::set_var("AWS_CONFIG_FILE", test_config_file_path); let mut test_credentials = Ini::new(); - test_credentials.with_section(Some("")) + test_credentials + .with_section(Some("")) .set("aws_access_key_id", "devAccessKeyID") .set("aws_secret_access_key", "devSecretAccessKey"); let test_credentials_file_path = test_aws_dir_path.join("credentials"); - test_credentials.write_to_file(&test_credentials_file_path).unwrap(); + test_credentials + .write_to_file(&test_credentials_file_path) + .unwrap(); env::set_var("AWS_SHARED_CREDENTIALS_FILE", test_credentials_file_path); InvalidContext { @@ -99,8 +115,16 @@ mod tests { } async fn teardown(self) { - env::set_var("AWS_CONFIG_FILE", self.original_config_file_location.as_deref().unwrap_or("")); - env::set_var("AWS_SHARED_CREDENTIALS_FILE", self.original_credentials_file_location.as_deref().unwrap_or("")); + env::set_var( + "AWS_CONFIG_FILE", + self.original_config_file_location.as_deref().unwrap_or(""), + ); + env::set_var( + "AWS_SHARED_CREDENTIALS_FILE", + self.original_credentials_file_location + .as_deref() + .unwrap_or(""), + ); } } @@ -110,7 +134,10 @@ mod tests { async fn should_load_config_from_environment(_: &mut ValidContext) { let cut: Box = Box::new(SdkConfigAdapter); let dev_settings = Settings::new( - Credentials::new(Some("devAccessKeyID".to_string()), Some(SecStr::from("devSecretAccessKey"))), + Credentials::new( + Some("devAccessKeyID".to_string()), + Some(SecStr::from("devSecretAccessKey")), + ), Config::new(Some("eu-west-1".to_string()), Some("json".to_string())), ); @@ -119,14 +146,15 @@ mod tests { assert_that(&result).is_ok(); let actual = result.unwrap(); let actual_profiles = actual.profiles(); - assert_that(actual_profiles) - .contains_entry(&"dev".to_string(), &dev_settings); + assert_that(actual_profiles).contains_entry(&"dev".to_string(), &dev_settings); } #[test_context(InvalidContext)] #[tokio::test] #[serial] - async fn should_have_errors_when_loading_config_with_invalid_profile_name(_: &mut InvalidContext) { + async fn should_have_errors_when_loading_config_with_invalid_profile_name( + _: &mut InvalidContext, + ) { let cut: Box = Box::new(SdkConfigAdapter); let result = cut.load_profile_data().await; @@ -134,11 +162,15 @@ mod tests { assert_that(&result).is_ok(); let config = result.unwrap(); assert_that(&config.errors).has_length(1); - let is_invalid_profile_name_error = *&config.errors.get(0).unwrap() - .contains::(); + let is_invalid_profile_name_error = + *&config.errors.get(0).unwrap().contains::(); assert_that(&is_invalid_profile_name_error).is_true(); - let actual = *&config.errors.get(0).unwrap() - .downcast_ref::().unwrap(); + let actual = *&config + .errors + .get(0) + .unwrap() + .downcast_ref::() + .unwrap(); assert_that(&actual).is_equal_to(&ProfileError::InvalidProfileNameError); } -} \ No newline at end of file +}