kit_rs/config/providers/
app.rs1use crate::config::env::{env, Environment};
2
3#[derive(Debug, Clone)]
5pub struct AppConfig {
6 pub name: String,
8 pub environment: Environment,
10 pub debug: bool,
12 pub url: String,
14}
15
16impl AppConfig {
17 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 pub fn builder() -> AppConfigBuilder {
29 AppConfigBuilder::default()
30 }
31
32 pub fn is_debug(&self) -> bool {
34 self.debug
35 }
36
37 pub fn is_production(&self) -> bool {
39 self.environment.is_production()
40 }
41
42 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#[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 pub fn name(mut self, name: impl Into<String>) -> Self {
66 self.name = Some(name.into());
67 self
68 }
69
70 pub fn environment(mut self, env: Environment) -> Self {
72 self.environment = Some(env);
73 self
74 }
75
76 pub fn debug(mut self, debug: bool) -> Self {
78 self.debug = Some(debug);
79 self
80 }
81
82 pub fn url(mut self, url: impl Into<String>) -> Self {
84 self.url = Some(url.into());
85 self
86 }
87
88 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}