Skip to main content

haste_config/
lib.rs

1use crate::environment::EnvironmentConfig;
2use haste_fhir_operation_error::OperationOutcomeError;
3use std::sync::Arc;
4
5mod environment;
6
7pub trait Config<Key: Into<String>>: Send + Sync {
8    /// Gets the value of an environment configuration variable.
9    ///
10    /// # Errors
11    ///
12    /// Returns [`OperationOutcomeError`] if the environment variable is not set
13    /// or cannot be read.
14    fn get(&self, name: Key) -> Result<String, OperationOutcomeError>;
15    /// Sets the value of an environment configuration variable.
16    ///
17    /// # Errors
18    ///
19    /// Returns [`OperationOutcomeError`] if setting the variable fails.
20    fn set(&self, name: Key, value: String) -> Result<(), OperationOutcomeError>;
21}
22
23#[derive(Clone, Copy)]
24pub enum ConfigType {
25    Environment,
26}
27
28impl From<&str> for ConfigType {
29    fn from(value: &str) -> Self {
30        match value {
31            "environment" => ConfigType::Environment,
32            _ => panic!("Unknown config type"),
33        }
34    }
35}
36
37#[must_use]
38pub fn get_config<Key: Into<String>>(config_type: ConfigType) -> Arc<dyn Config<Key>> {
39    match config_type {
40        ConfigType::Environment => Arc::new(EnvironmentConfig::new(&[".env", ".env.development"])),
41    }
42}