diff --git a/Cargo.lock b/Cargo.lock index 3e4dcd1..a0ae075 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,7 +297,7 @@ checksum = "f154ce46856750ed433c8649605bf7ed2de3bc35fd9d2a9f30cddd873c80cb08" [[package]] name = "icarus_envy" -version = "0.3.5" +version = "0.4.0" dependencies = [ "async-std", "const_format", diff --git a/Cargo.toml b/Cargo.toml index b0ad5a3..72a3b8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus_envy" -version = "0.3.5" +version = "0.4.0" edition = "2024" rust-version = "1.88" diff --git a/src/environment.rs b/src/environment.rs index 3c51a96..b0e9972 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -1,67 +1,104 @@ -pub async fn get_db_url() -> String { +pub async fn get_db_url() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::DB_URL).expect(crate::keys::error::DB_URL) + let key = crate::keys::DB_URL; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_secret_main_key() -> String { +pub async fn get_secret_main_key() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::SECRET_MAIN_KEY).expect(crate::keys::error::SECRET_MAIN_KEY) + let key = crate::keys::SECRET_MAIN_KEY; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_service_passphrase() -> String { +pub async fn get_service_passphrase() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::SERVICE_PASSPHRASE).expect(crate::keys::error::SERVICE_LOGIN) + let key = crate::keys::SERVICE_PASSPHRASE; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_secret_key() -> String { +pub async fn get_secret_key() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::SECRET_KEY).expect(crate::keys::error::SECRET_KEY) + let key = crate::keys::SECRET_KEY; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_root_directory() -> String { +pub async fn get_root_directory() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::ROOT_DIRECTORY).expect(crate::keys::error::ROOT_DIRECTORY) + let key = crate::keys::ROOT_DIRECTORY; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_icarus_base_api_url() -> String { +pub async fn get_icarus_base_api_url() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::ICARUS_BASE_API_URL).expect(crate::keys::error::ICARUS_BASE_API_URL) + let key = crate::keys::ICARUS_BASE_API_URL; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_icarus_auth_base_api_url() -> String { +pub async fn get_icarus_auth_base_api_url() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::ICARUS_AUTH_BASE_API_URL) - .expect(crate::keys::error::ICARUS_AUTH_BASE_API_URL) + let key = crate::keys::ICARUS_AUTH_BASE_API_URL; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_app_env() -> String { +pub async fn get_app_env() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::APP_ENV).expect(crate::keys::error::APP_ENV) + let key = crate::keys::APP_ENV; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_backend_port() -> String { +pub async fn get_backend_port() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::BACKEND_PORT).expect(crate::keys::error::BACKEND_PORT) + let key = crate::keys::BACKEND_PORT; + let value = std::env::var(key).expect(key); + crate::init_envvar(key, &value) } -pub async fn get_frontend_url() -> String { +pub async fn get_frontend_url() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::FRONTEND_URL).expect(crate::keys::error::FRONTEND_URL) + let key = crate::keys::FRONTEND_URL; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_rust_log() -> String { +pub async fn get_rust_log() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::RUST_LOG).expect(crate::keys::error::RUST_LOG) + let key = crate::keys::RUST_LOG; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } -pub async fn get_allowed_origins() -> String { +pub async fn get_allowed_origins() -> crate::EnvVar { dotenvy::dotenv().ok(); - std::env::var(crate::keys::ALLOWED_ORIGINS).expect(crate::keys::error::ALLOWED_ORIGINS) + let key = crate::keys::ALLOWED_ORIGINS; + let value = std::env::var(key).expect(key); + + crate::init_envvar(key, &value) } /// Get environment not specified in the code -pub async fn get_env(environment: &str) -> String { +pub async fn get_env(environment: &str) -> crate::EnvVar { dotenvy::dotenv().ok(); + // let key = crate::keys:: let my_error = format!("{environment} {}", crate::keys::error::GENERAL_ERROR); - std::env::var(environment).expect(&my_error) + let value = std::env::var(environment).expect(&my_error); + + crate::init_envvar(environment, &value) } diff --git a/src/lib.rs b/src/lib.rs index 5019657..5e88d16 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,15 @@ pub mod environment; pub mod keys; + +#[derive(Debug, Default, Clone)] +pub struct EnvVar { + pub key: String, + pub value: String, +} + +pub fn init_envvar(key: &str, value: &str) -> EnvVar { + EnvVar { + key: key.to_string(), + value: value.to_string(), + } +} diff --git a/tests/test.rs b/tests/test.rs index e657583..d92de61 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -4,7 +4,7 @@ mod tests { fn test_dburl() { let result = async_std::task::block_on(icarus_envy::environment::get_db_url()); assert_eq!( - result, "postgres://myuser:password@localhost:5432/my_db", + result.value, "postgres://myuser:password@localhost:5432/my_db", "DATABASE_URL does not match {:?}", result ); @@ -14,7 +14,7 @@ mod tests { fn test_get_secret_main_key() { let result = async_std::task::block_on(icarus_envy::environment::get_secret_main_key()); assert_eq!( - result, "Somesupersecretpassword!!!45345435", + result.value, "Somesupersecretpassword!!!45345435", "SECRET_MAIN_KEY does not match {:?}", result ) @@ -24,7 +24,7 @@ mod tests { fn test_get_service_passphrase() { let result = async_std::task::block_on(icarus_envy::environment::get_service_passphrase()); assert_eq!( - result, "T5OCHDHadAtuOWIqRAS7u8XHDDkzKT1Uvvw7mGMkNzKjVdlHA8xGdILf2adDHspO", + result.value, "T5OCHDHadAtuOWIqRAS7u8XHDDkzKT1Uvvw7mGMkNzKjVdlHA8xGdILf2adDHspO", "SERVICE_PASSPHRASE does not match {:?}", result ) @@ -34,7 +34,7 @@ mod tests { fn test_get_secret_key() { let result = async_std::task::block_on(icarus_envy::environment::get_secret_key()); assert_eq!( - result, "AmIGoodEnoughForYou?", + result.value, "AmIGoodEnoughForYou?", "SECRET_KEY does not match {:?}", result ) @@ -44,7 +44,7 @@ mod tests { fn test_get_root_directory() { let result = async_std::task::block_on(icarus_envy::environment::get_root_directory()); assert_eq!( - result, "/path/to/root", + result.value, "/path/to/root", "ROOT_DIRECTORY does not match {:?}", result ) @@ -54,7 +54,7 @@ mod tests { fn test_get_icarus_base_api_url() { let result = async_std::task::block_on(icarus_envy::environment::get_icarus_base_api_url()); assert_eq!( - result, "https://icarus.com", + result.value, "https://icarus.com", "ICARUS_BASE_API_URL does not match {:?}", result ) @@ -64,7 +64,7 @@ mod tests { fn test_get_app_env() { let result = async_std::task::block_on(icarus_envy::environment::get_app_env()); assert_eq!( - result, + result.value, "development", "{} does not match {:?}", icarus_envy::keys::APP_ENV, @@ -76,7 +76,7 @@ mod tests { fn test_get_backend_port() { let result = async_std::task::block_on(icarus_envy::environment::get_backend_port()); assert_eq!( - result, + result.value, "8001", "{} does not match {:?}", icarus_envy::keys::BACKEND_PORT, @@ -87,7 +87,7 @@ mod tests { fn test_get_frontend_url() { let result = async_std::task::block_on(icarus_envy::environment::get_frontend_url()); assert_eq!( - result, + result.value, "http://localhost:4200", "{} does not match {:?}", icarus_envy::keys::FRONTEND_URL, @@ -98,7 +98,7 @@ mod tests { fn test_get_rust_log() { let result = async_std::task::block_on(icarus_envy::environment::get_rust_log()); assert_eq!( - result, + result.value, "debug", "{} does not match {:?}", icarus_envy::keys::RUST_LOG, @@ -109,7 +109,7 @@ mod tests { fn test_get_allowed_origins() { let result = async_std::task::block_on(icarus_envy::environment::get_allowed_origins()); assert_eq!( - result, + result.value, "https://soaricarus.com,https://www.soaricarus.com", "{} does not match {:?}", icarus_envy::keys::ALLOWED_ORIGINS, @@ -132,7 +132,11 @@ mod tests { for (key, value) in keys.iter() { let result = async_std::task::block_on(icarus_envy::environment::get_env(key)); - assert_eq!(result, *value, "{:?} does not match {:?}", key, result) + assert_eq!( + result.value, *value, + "{:?} does not match {:?}", + key, result + ) } } }