Skip to content

Config Feature #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
version = "0.0.0"
name = "code0-flow"
edition = "2021"
edition = "2024"
description = "Crate for managing the code0-flows inside of the Flow Queue & FlowStore"
repository = "https://github.com/code0-tech/code0-flow"
homepage = "https://code0.tech"
Expand All @@ -23,6 +23,8 @@ serde = "1.0.138"
lapin = "2.5.0"
futures-lite = "2.6.0"
tonic = "0.13.0"
dotenv = "0.15.0"


[dev-dependencies]
testcontainers = "0.24.0"
Expand All @@ -36,4 +38,5 @@ default = ["all"]
flow_queue = []
flow_store = []
flow_definition = []
all = ["flow_queue", "flow_store", "flow_definition"]
flow_config = []
all = ["flow_queue", "flow_store", "flow_definition", "flow_config"]
21 changes: 21 additions & 0 deletions src/flow_config/environment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::str::FromStr;

#[derive(Debug, PartialEq, Eq)]
pub enum Environment {
Development,
Staging,
Production,
}

impl FromStr for Environment {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"staging" => Ok(Environment::Staging),
"production" => Ok(Environment::Production),
"development" => Ok(Environment::Development),
_ => Ok(Environment::Development),
}
}
}
256 changes: 256 additions & 0 deletions src/flow_config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
use std::{fmt::Debug, str::FromStr};

pub mod environment;
pub mod mode;

pub fn env_with_default<T: FromStr + Debug>(key: &str, default: T) -> T {
match std::env::var(key) {
Ok(string) => match T::from_str(&string) {
Ok(value) => {
log::info!("Found env: {} with value: {:?}", key, &value);
value
}
Err(_) => {
log::warn!("Failed to parse env: {} with value: {:?}", key, &string);
default
}
},
Err(_) => {
log::warn!("Failed to find env: {}", key);
default
}
}
}

pub fn load_env_file() {
match dotenv::dotenv() {
Ok(path) => log::info!("Found Env. file at {:?} ", path),
Err(e) => log::error!("Failed to load .env file. Reason: {:?}", e),
}
}

#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::env;

#[test]
#[serial]
fn test_env_with_default_string_exists() {
let key = "TEST_STRING_VAR";
let expected = "test_value";

unsafe {
env::set_var(key, expected);
}

let result = env_with_default(key, "default".to_string());
assert_eq!(result, expected);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_string_missing() {
let key = "TEST_MISSING_STRING_VAR";
unsafe {
env::remove_var(key);
}

let default = "default_value".to_string();
let result = env_with_default(key, default.clone());
assert_eq!(result, default);
}

#[test]
#[serial]
fn test_env_with_default_integer_exists() {
let key = "TEST_INT_VAR";
let expected = 42;
unsafe {
env::set_var(key, expected.to_string());
}

let result = env_with_default(key, 0i32);
assert_eq!(result, expected);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_integer_missing() {
let key = "TEST_MISSING_INT_VAR";
unsafe {
env::remove_var(key);
}

let default = 123i32;
let result = env_with_default(key, default);
assert_eq!(result, default);
}

#[test]
#[serial]
fn test_env_with_default_boolean_exists_true() {
let key = "TEST_BOOL_TRUE_VAR";
unsafe {
env::set_var(key, "true");
}

let result = env_with_default(key, false);
assert_eq!(result, true);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_boolean_exists_false() {
let key = "TEST_BOOL_FALSE_VAR";
unsafe {
env::set_var(key, "false");
}

let result = env_with_default(key, true);
assert_eq!(result, false);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_boolean_missing() {
let key = "TEST_MISSING_BOOL_VAR";
unsafe {
env::remove_var(key);
}

let default = true;
let result = env_with_default(key, default);
assert_eq!(result, default);
}

#[test]
#[serial]
fn test_env_with_default_boolean_invalid() {
let key = "TEST_INVALID_BOOL_VAR";
unsafe {
env::set_var(key, "maybe");
}

let result = env_with_default(key, false);
assert_eq!(result, false);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_u32_exists() {
let key = "TEST_U32_VAR";
let expected = 42u32;
unsafe {
env::set_var(key, expected.to_string());
}

let result = env_with_default(key, 0u32);
assert_eq!(result, expected);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_u32_negative_invalid() {
let key = "TEST_U32_NEGATIVE_VAR";
unsafe {
env::set_var(key, "-42");
}

let result = env_with_default(key, 0u32);
assert_eq!(result, 0u32);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_empty_string() {
let key = "TEST_EMPTY_STRING_VAR";
unsafe {
env::set_var(key, "");
}

let result = env_with_default(key, "default".to_string());
assert_eq!(result, "");

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_default_whitespace_string() {
let key = "TEST_WHITESPACE_VAR";
unsafe {
env::set_var(key, " whitespace ");
}

let result = env_with_default(key, "default".to_string());
assert_eq!(result, " whitespace ");

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_environment() {
let key = "TEST_ENVIRONMENT";
unsafe {
env::set_var(key, "DEVELOPMENT");
}

let result = env_with_default(key, environment::Environment::Development);
assert_eq!(result, environment::Environment::Development);

unsafe {
env::remove_var(key);
}
}

#[test]
#[serial]
fn test_env_with_mode() {
let key = "TEST_MODE";
unsafe {
env::set_var(key, "STATIC");
}

let result = env_with_default(key, mode::Mode::STATIC);
assert_eq!(result, mode::Mode::STATIC);

unsafe {
env::remove_var(key);
}
}
}
24 changes: 24 additions & 0 deletions src/flow_config/mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::str::FromStr;

/// STATIC:
/// The service will start with no Sagittarius in mind
///
/// DYNAMIC:
/// The service will start with Sagittarius in mind
#[derive(PartialEq, Eq, Debug)]
pub enum Mode {
STATIC,
DYNAMIC,
}

impl FromStr for Mode {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"static" => Ok(Mode::STATIC),
"dynamic" => Ok(Mode::DYNAMIC),
_ => Ok(Mode::STATIC),
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ pub mod flow_queue;

#[cfg(feature = "flow_definition")]
pub mod flow_definition;

#[cfg(feature = "flow_config")]
pub mod flow_config;