tsk-22: Add function to parse env variable to a vector of strings (#25)
All checks were successful
Release Tagging / release (push) Successful in 33s
Rust Build / Check (push) Successful in 33s
Rust Build / Test Suite (push) Successful in 31s
Rust Build / Rustfmt (push) Successful in 31s
Rust Build / Clippy (push) Successful in 32s
Rust Build / build (push) Successful in 30s
All checks were successful
Release Tagging / release (push) Successful in 33s
Rust Build / Check (push) Successful in 33s
Rust Build / Test Suite (push) Successful in 31s
Rust Build / Rustfmt (push) Successful in 31s
Rust Build / Clippy (push) Successful in 32s
Rust Build / build (push) Successful in 30s
Closes #22 Reviewed-on: #25 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -297,7 +297,7 @@ checksum = "f154ce46856750ed433c8649605bf7ed2de3bc35fd9d2a9f30cddd873c80cb08"
|
||||
|
||||
[[package]]
|
||||
name = "icarus_envy"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
dependencies = [
|
||||
"async-std",
|
||||
"const_format",
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "icarus_envy"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
edition = "2024"
|
||||
rust-version = "1.88"
|
||||
|
||||
|
@@ -90,13 +90,15 @@ pub async fn get_allowed_origins() -> crate::EnvVar {
|
||||
let key = crate::keys::ALLOWED_ORIGINS;
|
||||
let value = std::env::var(key).expect(key);
|
||||
|
||||
crate::init_envvar(key, &value)
|
||||
let mut envvar = crate::init_envvar(key, &value);
|
||||
crate::init_delimiter(&mut envvar, ',');
|
||||
|
||||
envvar
|
||||
}
|
||||
|
||||
/// Get environment not specified in the code
|
||||
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);
|
||||
let value = std::env::var(environment).expect(&my_error);
|
||||
|
||||
|
24
src/lib.rs
24
src/lib.rs
@@ -1,15 +1,39 @@
|
||||
pub mod environment;
|
||||
pub mod keys;
|
||||
pub mod utility;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct EnvVar {
|
||||
pub key: String,
|
||||
pub value: String,
|
||||
pub has_delimiter: bool,
|
||||
pub delimiter: char,
|
||||
}
|
||||
|
||||
pub fn init_envvar(key: &str, value: &str) -> EnvVar {
|
||||
EnvVar {
|
||||
key: key.to_string(),
|
||||
value: value.to_string(),
|
||||
has_delimiter: false,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_delimiter(envvar: &mut EnvVar, delimiter: char) {
|
||||
let mut amount_of_delimiters_found: i32 = 0;
|
||||
|
||||
for v in envvar.value.chars() {
|
||||
if v == delimiter {
|
||||
amount_of_delimiters_found += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let has_delimiter = amount_of_delimiters_found >= 1;
|
||||
|
||||
if has_delimiter {
|
||||
envvar.has_delimiter = has_delimiter;
|
||||
envvar.delimiter = delimiter;
|
||||
} else {
|
||||
envvar.has_delimiter = has_delimiter;
|
||||
}
|
||||
}
|
||||
|
15
src/utility.rs
Normal file
15
src/utility.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
/// Take the Environment variable and delimitize it. If the value has a delimiter,
|
||||
/// extract it into some strings
|
||||
pub fn delimitize(var: &crate::EnvVar) -> Result<Vec<String>, std::io::Error> {
|
||||
if var.has_delimiter {
|
||||
Ok(var
|
||||
.value
|
||||
.split(var.delimiter)
|
||||
.map(|c| c.parse::<String>().unwrap())
|
||||
.collect())
|
||||
} else {
|
||||
Err(std::io::Error::other(
|
||||
"Environment variable does not have a delimiter",
|
||||
))
|
||||
}
|
||||
}
|
@@ -114,7 +114,28 @@ mod tests {
|
||||
"{} does not match {:?}",
|
||||
icarus_envy::keys::ALLOWED_ORIGINS,
|
||||
result
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
result.has_delimiter, true,
|
||||
"The {} variable has an issue finding the delimiter",
|
||||
result.key
|
||||
);
|
||||
|
||||
match icarus_envy::utility::delimitize(&result) {
|
||||
Ok(allowed_origins) => {
|
||||
assert_eq!(
|
||||
allowed_origins.len(),
|
||||
2,
|
||||
"The amount of allowed origins does not match. {} {}",
|
||||
allowed_origins.len(),
|
||||
2
|
||||
)
|
||||
}
|
||||
Err(err) => {
|
||||
assert!(false, "Error: {:?}", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user