kit_rs/config/providers/
app.rs

1use crate::config::env::{env, Environment};
2
3/// Application configuration
4#[derive(Debug, Clone)]
5pub struct AppConfig {
6    /// Application name
7    pub name: String,
8    /// Current environment
9    pub environment: Environment,
10    /// Debug mode enabled
11    pub debug: bool,
12    /// Application URL
13    pub url: String,
14}
15
16impl AppConfig {
17    /// Build config from environment variables
18    pub fn from_env() -> Self {
19        Self {
20            name: env("APP_NAME", "Kit Application".to_string()),
21            environment: Environment::detect(),
22            debug: env("APP_DEBUG", true),
23            url: env("APP_URL", "http://localhost:8080".to_string()),
24        }
25    }
26
27    /// Create a builder for customizing config
28    pub fn builder() -> AppConfigBuilder {
29        AppConfigBuilder::default()
30    }
31
32    /// Check if debug mode is enabled
33    pub fn is_debug(&self) -> bool {
34        self.debug
35    }
36
37    /// Check if running in production
38    pub fn is_production(&self) -> bool {
39        self.environment.is_production()
40    }
41
42    /// Check if running in development
43    pub fn is_development(&self) -> bool {
44        self.environment.is_development()
45    }
46}
47
48impl Default for AppConfig {
49    fn default() -> Self {
50        Self::from_env()
51    }
52}
53
54/// Builder for AppConfig
55#[derive(Default)]
56pub struct AppConfigBuilder {
57    name: Option<String>,
58    environment: Option<Environment>,
59    debug: Option<bool>,
60    url: Option<String>,
61}
62
63impl AppConfigBuilder {
64    /// Set the application name
65    pub fn name(mut self, name: impl Into<String>) -> Self {
66        self.name = Some(name.into());
67        self
68    }
69
70    /// Set the environment
71    pub fn environment(mut self, env: Environment) -> Self {
72        self.environment = Some(env);
73        self
74    }
75
76    /// Set debug mode
77    pub fn debug(mut self, debug: bool) -> Self {
78        self.debug = Some(debug);
79        self
80    }
81
82    /// Set the application URL
83    pub fn url(mut self, url: impl Into<String>) -> Self {
84        self.url = Some(url.into());
85        self
86    }
87
88    /// Build the AppConfig
89    pub fn build(self) -> AppConfig {
90        let default = AppConfig::from_env();
91        AppConfig {
92            name: self.name.unwrap_or(default.name),
93            environment: self.environment.unwrap_or(default.environment),
94            debug: self.debug.unwrap_or(default.debug),
95            url: self.url.unwrap_or(default.url),
96        }
97    }
98}