use std::env; const ENV_VAR_PREFIX: &str = "LOCALITY_"; #[derive(Debug)] pub enum Error { MissingEnvironmentVariable(String), } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::MissingEnvironmentVariable(v) => { write!(f, "The environment variable \"{}\" must be set.", v) } } } } impl std::error::Error for Error {} pub struct Config { pub database_url: String, pub static_file_path: String, pub hmac_secret: Vec, } fn get_config_string(variable: &str) -> Result { let full_var = format!("{ENV_VAR_PREFIX}{variable}"); env::var(&full_var).map_err(|_| Error::MissingEnvironmentVariable(full_var)) } pub fn get_config() -> Result { Ok(Config { database_url: get_config_string("DATABASE_URL")?, static_file_path: get_config_string("STATIC_FILE_PATH")?, hmac_secret: get_config_string("HMAC_SECRET")?.into_bytes(), }) }