2023-09-29 12:15:07 +02:00
|
|
|
/*
|
|
|
|
env.rs - The environment loader. It loads all the .env config to a convenient `Env` struct.
|
|
|
|
This file provides the `loadenv` function that will do just that.
|
|
|
|
*/
|
|
|
|
|
2023-09-29 12:57:53 +02:00
|
|
|
use std::{env::var, net::SocketAddr};
|
2023-09-29 12:15:07 +02:00
|
|
|
|
|
|
|
pub struct Env {
|
2023-09-29 12:57:53 +02:00
|
|
|
pub logging: bool,
|
|
|
|
pub listen: SocketAddr
|
2023-09-29 12:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_var<T: Into<String>, O: From<String>>(name: T) -> Result<O, String> {
|
|
|
|
let name: String = name.into();
|
|
|
|
let v = var(name.clone());
|
|
|
|
if v.is_err() {
|
|
|
|
return Err(format!("Variable {name} does not exist!"));
|
|
|
|
}
|
|
|
|
return Ok(v.unwrap().into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn loadenv() -> Result<Env, Box<dyn std::error::Error>> {
|
|
|
|
Ok(
|
|
|
|
Env {
|
|
|
|
logging: get_var::<&str, String>("APP_LOGGING")?.to_lowercase() == "true",
|
2023-09-29 12:57:53 +02:00
|
|
|
listen: get_var::<&str, String>("APP_HOST")?.parse::<SocketAddr>().unwrap()
|
2023-09-29 12:15:07 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|