Skip to content

Commit

Permalink
style(backend): Reformat code and adjust import order
Browse files Browse the repository at this point in the history
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
  • Loading branch information
maikbasel committed Dec 21, 2023
1 parent ecae60a commit 11287fe
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src-tauri/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn main() {
tauri_build::build()
tauri_build::build()
}
2 changes: 1 addition & 1 deletion src-tauri/src/common.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub(crate) mod test;
pub(crate) mod test;
5 changes: 3 additions & 2 deletions src-tauri/src/common/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub mod report_utils {
use error_stack::{AttachmentKind, FrameKind, Report};

pub fn messages<E>(report: Report<E>) -> Vec<String> {
report.frames()
report
.frames()
.map(|frame| {
if let FrameKind::Attachment(AttachmentKind::Printable(attachment)) = frame.kind() {
Some(attachment.to_string())
Expand All @@ -14,4 +15,4 @@ pub mod report_utils {
.filter_map(|result| result)
.collect()
}
}
}
6 changes: 3 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/profile.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod application;
pub mod core;
pub mod infrastructure;
pub mod infrastructure;
2 changes: 1 addition & 1 deletion src-tauri/src/profile/application.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod tauri;
pub mod tauri;
2 changes: 1 addition & 1 deletion src-tauri/src/profile/application/tauri.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod profile_handler;
pub mod profile_handler;
8 changes: 5 additions & 3 deletions src-tauri/src/profile/application/tauri/profile_handler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::sync::Arc;
use crate::profile::core::api::ProfileAPI;
use std::sync::Arc;

#[derive(serde::Serialize)]
pub struct GetConfigProfilesResponse {}
Expand All @@ -8,6 +8,8 @@ pub struct GetConfigProfilesResponse {}
pub struct ErrorResponse {}

#[tauri::command]
pub async fn get_profiles(_api: tauri::State<'_, Arc<dyn ProfileAPI>>) -> Result<GetConfigProfilesResponse, ErrorResponse> {
pub async fn get_profiles(
_api: tauri::State<'_, Arc<dyn ProfileAPI>>,
) -> Result<GetConfigProfilesResponse, ErrorResponse> {
todo!()
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/profile/core.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod api;
pub mod spi;
pub mod domain;
pub mod error;
pub mod spi;
2 changes: 1 addition & 1 deletion src-tauri/src/profile/core/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProfileSet, ProfileError>;
}
}
39 changes: 29 additions & 10 deletions src-tauri/src/profile/core/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ pub struct Credentials {

impl Credentials {
pub fn new(access_key_id: Option<String>, secret_access_key: Option<SecStr>) -> Self {
Self { access_key_id, secret_access_key }
Self {
access_key_id,
secret_access_key,
}
}
}

Expand All @@ -30,7 +33,10 @@ pub struct Config {

impl Config {
pub fn new(region: Option<String>, output_format: Option<String>) -> Self {
Self { region, output_format }
Self {
region,
output_format,
}
}
}

Expand All @@ -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),
}
}
}

Expand All @@ -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> {
Expand Down Expand Up @@ -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::*;

Expand All @@ -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())
Expand All @@ -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);

Expand All @@ -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)
Expand All @@ -156,12 +174,13 @@ 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));

let actual = cut.get_profiles().await;

assert_that!(actual).is_ok();
}
}
}
11 changes: 7 additions & 4 deletions src-tauri/src/profile/core/error.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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"),
}
}
}
Expand All @@ -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]
Expand All @@ -35,4 +38,4 @@ mod tests {

assert_eq!(format!("{}", ProfileError::ProfileDataLoadError), expected)
}
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/profile/core/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ use crate::profile::core::error::ProfileError;
#[async_trait]
pub trait ProfileDataSPI: Send + Sync {
async fn load_profile_data(&self) -> Result<ProfileSet, ProfileError>;
}
}
2 changes: 1 addition & 1 deletion src-tauri/src/profile/infrastructure.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod aws;
pub mod aws;
2 changes: 1 addition & 1 deletion src-tauri/src/profile/infrastructure/aws.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod sdk_config;
pub mod sdk_config;
2 changes: 1 addition & 1 deletion src-tauri/src/profile/infrastructure/aws/sdk_config.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod sdk_config_adapter;
pub mod sdk_config_adapter;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl ProfileDataSPI for SdkConfigAdapter {
&Default::default(),
&Default::default(),
None,
).await;
)
.await;

match result {
Ok(profile_set) => {
Expand All @@ -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
}
}
Loading

0 comments on commit 11287fe

Please sign in to comment.