Skip to main content

aster_server/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum ConfigError {
5    #[error("Missing required environment variable: {env_var}")]
6    MissingEnvVar { env_var: String },
7    #[error("Configuration error: {0}")]
8    Other(#[from] config::ConfigError),
9}
10
11// Helper function to format environment variable names
12pub(crate) fn to_env_var(field_path: &str) -> String {
13    // Handle nested fields by converting dots to double underscores
14    // If the field is in the provider object, we need to prefix it appropriately
15    let normalized_path = if field_path == "type" {
16        "provider.type".to_string()
17    } else if field_path.starts_with("provider.") {
18        field_path.to_string()
19    } else {
20        format!("provider.{}", field_path)
21    };
22
23    format!(
24        "ASTER_{}",
25        normalized_path.replace('.', "__").to_uppercase()
26    )
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_env_var_conversion() {
35        assert_eq!(to_env_var("type"), "ASTER_PROVIDER__TYPE");
36        assert_eq!(to_env_var("api_key"), "ASTER_PROVIDER__API_KEY");
37        assert_eq!(to_env_var("provider.host"), "ASTER_PROVIDER__HOST");
38        assert_eq!(to_env_var("provider.api_key"), "ASTER_PROVIDER__API_KEY");
39    }
40}