Skip to content

Commit

Permalink
refactor: Rename files & structs to reflect the domain is the profile
Browse files Browse the repository at this point in the history
Refs: #1
  • Loading branch information
maikbasel committed Dec 12, 2023
1 parent 7a32075 commit a269624
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 49 deletions.
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 @@
mod api;
pub mod api;
pub mod spi;
pub mod domain;
pub mod error;
8 changes: 4 additions & 4 deletions src-tauri/src/profile/core/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::profile::core::domain::{ConfigProfiles};
use crate::profile::core::error::ConfigProfilesError;
use crate::profile::core::domain::{ProfileSet};
use crate::profile::core::error::ProfileError;

pub trait ConfigProfilesAPI {
fn get_config_profiles() -> Result<ConfigProfiles, ConfigProfilesError>;
pub trait ProfileAPI {
fn get_profiles(&self) -> Result<ProfileSet, ProfileError>;
}
39 changes: 26 additions & 13 deletions src-tauri/src/profile/core/domain.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::collections::HashMap;
use std::sync::Arc;

use derivative::Derivative;
use error_stack::{Report, Result};
use secstr::SecStr;
use crate::profile::core::api::ProfileAPI;

use crate::profile::core::error::ConfigProfilesError;
use crate::profile::core::error::ProfileError;
use crate::profile::core::spi::ProfileDataSPI;

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Credentials {
Expand Down Expand Up @@ -44,22 +47,22 @@ impl Settings {

#[derive(Derivative)]
#[derivative(Debug, Eq, PartialEq)]
pub struct ConfigProfiles {
pub struct ProfileSet {
profiles: HashMap<String, Settings>,
#[derivative(Debug = "ignore")]
#[derivative(PartialEq = "ignore")]
pub errors: Vec<Report<ConfigProfilesError>>,
pub errors: Vec<Report<ProfileError>>,
}

impl ConfigProfiles {
impl ProfileSet {
pub fn new() -> Self {
Self { profiles: HashMap::new(), errors: Vec::new() }
}

// changed from String to &str
pub fn add_profile(&mut self, name: &str, settings: Settings) -> Result<(), ConfigProfilesError> {
pub fn add_profile(&mut self, name: &str, settings: Settings) -> Result<(), ProfileError> {
if name.trim().is_empty() {
return Err(Report::new(ConfigProfilesError::InvalidProfileNameError)
return Err(Report::new(ProfileError::InvalidProfileNameError)
.attach_printable("profile name can not be empty or blank"));
}
self.profiles.insert(name.to_string(), settings); // convert to String here as hashmap key is String
Expand All @@ -71,9 +74,19 @@ impl ConfigProfiles {
}
}

impl Default for ConfigProfiles {
impl Default for ProfileSet {
fn default() -> Self {
ConfigProfiles::new()
ProfileSet::new()
}
}

struct ProfileService {
profile_data_spi: Arc<dyn ProfileDataSPI>
}

impl ProfileAPI for ProfileService {
fn get_profiles(&self) -> std::result::Result<ProfileSet, ProfileError> {
todo!()
}
}

Expand All @@ -85,13 +98,13 @@ mod tests {
use spectral::prelude::*;

use crate::common::test::report_utils::messages;
use crate::profile::core::error::ConfigProfilesError;
use crate::profile::core::error::ProfileError;

use super::*;

#[test]
fn should_add_profile() {
let mut cut: ConfigProfiles = ConfigProfiles::new();
let mut cut: ProfileSet = ProfileSet::new();
let input_settings: Settings = Settings { ..Default::default() };
let input_profile: String = Word().fake();

Expand All @@ -106,22 +119,22 @@ mod tests {
#[case("")]
#[case(" ")]
fn should_return_error_when_key_is_blank(#[case] input_profile: &str) {
let mut cut: ConfigProfiles = ConfigProfiles::new();
let mut cut: ProfileSet = ProfileSet::new();
let input_settings: Settings = Settings { ..Default::default() };

let actual = cut.add_profile(input_profile, input_settings);

assert_that(&actual).is_err();
let report = actual.unwrap_err();
assert!(report.contains::<ConfigProfilesError>());
assert!(report.contains::<ProfileError>());
let messages = messages(report);
assert_that(&messages).has_length(1);
assert_that(&messages).contains(String::from("profile name can not be empty or blank"));
}

#[test]
fn should_return_profiles() {
let mut cut: ConfigProfiles = ConfigProfiles::new();
let mut cut: ProfileSet = ProfileSet::new();
let input_settings: Settings = Settings { ..Default::default() };
let input_profile = Word().fake();

Expand Down
18 changes: 9 additions & 9 deletions src-tauri/src/profile/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ use std::fmt::{Debug, Display, Formatter};
use error_stack::{Context};

#[derive(Debug, Eq, PartialEq)]
pub enum ConfigProfilesError {
pub enum ProfileError {
InvalidProfileNameError,
ConfigLoadError,
ProfileDataLoadError,
}

impl Display for ConfigProfilesError {
impl Display for ProfileError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ConfigProfilesError::InvalidProfileNameError => write!(f, "invalid profile name"),
ConfigProfilesError::ConfigLoadError => write!(f, "failed to load config")
ProfileError::InvalidProfileNameError => write!(f, "invalid profile name"),
ProfileError::ProfileDataLoadError => write!(f, "failed to load profiles")
}
}
}

impl Context for ConfigProfilesError {}
impl Context for ProfileError {}

#[cfg(test)]
mod tests {
Expand All @@ -26,13 +26,13 @@ mod tests {
fn given_invalid_profile_error_should_output_expected_error_message_when_printed() {
let expected = "invalid profile name";

assert_eq!(format!("{}", ConfigProfilesError::InvalidProfileNameError), expected)
assert_eq!(format!("{}", ProfileError::InvalidProfileNameError), expected)
}

#[test]
fn given_profile_load_error_should_output_expected_error_message_when_printed() {
let expected = "failed to load config";
let expected = "failed to load profiles";

assert_eq!(format!("{}", ConfigProfilesError::ConfigLoadError), expected)
assert_eq!(format!("{}", ProfileError::ProfileDataLoadError), expected)
}
}
8 changes: 4 additions & 4 deletions src-tauri/src/profile/core/spi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use async_trait::async_trait;
use crate::profile::core::domain::{ConfigProfiles};
use crate::profile::core::error::ConfigProfilesError;
use crate::profile::core::domain::{ProfileSet};
use crate::profile::core::error::ProfileError;
use error_stack::{Result};

#[async_trait]
pub trait ConfigProfilesSPI {
async fn load_config_profiles(&self) -> Result<ConfigProfiles, ConfigProfilesError>;
pub trait ProfileDataSPI {
async fn load_profile_data(&self) -> Result<ProfileSet, ProfileError>;
}
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 configuration_adapter;
pub mod profile_file_adapter;
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use aws_config::profile::Profile;
use error_stack::Report;
use secstr::SecStr;

use crate::profile::core::domain::{Config, ConfigProfiles, Credentials, Settings};
use crate::profile::core::error::ConfigProfilesError;
use crate::profile::core::spi::ConfigProfilesSPI;
use crate::profile::core::domain::{Config, ProfileSet, Credentials, Settings};
use crate::profile::core::error::ProfileError;
use crate::profile::core::spi::ProfileDataSPI;

pub struct ConfigProfilesAdapter;

#[async_trait]
impl ConfigProfilesSPI for ConfigProfilesAdapter {
async fn load_config_profiles(&self) -> error_stack::Result<ConfigProfiles, ConfigProfilesError> {
impl ProfileDataSPI for ConfigProfilesAdapter {
async fn load_profile_data(&self) -> error_stack::Result<ProfileSet, ProfileError> {
// See https://docs.rs/aws-config/latest/aws_config/profile/index.html
let result = aws_config::profile::load(
&Default::default(),
Expand All @@ -23,7 +23,7 @@ impl ConfigProfilesSPI for ConfigProfilesAdapter {
match result {
Ok(profile_set) => {
let profile_names = profile_set.profiles();
let mut configuration = ConfigProfiles::new();
let mut configuration = ProfileSet::new();

for profile_name in profile_names {
if let Some(profile) = profile_set.get_profile(profile_name) {
Expand All @@ -44,7 +44,7 @@ impl ConfigProfilesSPI for ConfigProfilesAdapter {
}
Err(e) => {
Err(Report::from(e)
.change_context(ConfigProfilesError::ConfigLoadError))
.change_context(ProfileError::ProfileDataLoadError))
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src-tauri/tests/configuration_adapter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ mod tests {
use test_context::{AsyncTestContext, test_context};

use app::profile::core::domain::{Config, Credentials, Settings};
use app::profile::core::error::ConfigProfilesError;
use app::profile::core::spi::ConfigProfilesSPI;
use app::profile::infrastructure::aws::sdk_config::configuration_adapter::ConfigProfilesAdapter;
use app::profile::core::error::ProfileError;
use app::profile::core::spi::ProfileDataSPI;
use app::profile::infrastructure::aws::sdk_config::profile_file_adapter::ConfigProfilesAdapter;

struct ValidContext {
_test_dir: TempDir,
Expand Down Expand Up @@ -108,13 +108,13 @@ mod tests {
#[tokio::test]
#[serial]
async fn should_load_config_from_environment(_: &mut ValidContext) {
let cut: Box<dyn ConfigProfilesSPI> = Box::new(ConfigProfilesAdapter);
let cut: Box<dyn ProfileDataSPI> = Box::new(ConfigProfilesAdapter);
let dev_settings = Settings::new(
Credentials::new(Some("devAccessKeyID".to_string()), Some(SecStr::from("devSecretAccessKey"))),
Config::new(Some("eu-west-1".to_string()), Some("json".to_string())),
);

let result = cut.load_config_profiles().await;
let result = cut.load_profile_data().await;

assert_that(&result).is_ok();
let actual = result.unwrap();
Expand All @@ -127,18 +127,18 @@ mod tests {
#[tokio::test]
#[serial]
async fn should_have_errors_when_loading_config_with_invalid_profile_name(_: &mut InvalidContext) {
let cut: Box<dyn ConfigProfilesSPI> = Box::new(ConfigProfilesAdapter);
let cut: Box<dyn ProfileDataSPI> = Box::new(ConfigProfilesAdapter);

let result = cut.load_config_profiles().await;
let result = cut.load_profile_data().await;

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::<ConfigProfilesError>();
.contains::<ProfileError>();
assert_that(&is_invalid_profile_name_error).is_true();
let actual = *&config.errors.get(0).unwrap()
.downcast_ref::<ConfigProfilesError>().unwrap();
assert_that(&actual).is_equal_to(&ConfigProfilesError::InvalidProfileNameError);
.downcast_ref::<ProfileError>().unwrap();
assert_that(&actual).is_equal_to(&ProfileError::InvalidProfileNameError);
}
}

0 comments on commit a269624

Please sign in to comment.