Skip to main content

systemprompt_models/config/
environment.rs

1//! Environment detection and configuration.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum Environment {
8    Development,
9    Production,
10    Test,
11}
12
13impl Environment {
14    pub fn detect() -> Self {
15        if let Ok(env) = std::env::var("SYSTEMPROMPT_ENV") {
16            return Self::from_string(&env);
17        }
18
19        if let Ok(env) = std::env::var("RAILWAY_ENVIRONMENT")
20            && env == "production"
21        {
22            return Self::Production;
23        }
24
25        if let Ok(env) = std::env::var("NODE_ENV") {
26            return Self::from_string(&env);
27        }
28
29        if std::env::var("DOCKER_CONTAINER").is_ok() {
30            return Self::Production;
31        }
32
33        if cfg!(debug_assertions) {
34            return Self::Development;
35        }
36
37        Self::Production
38    }
39
40    fn from_string(s: &str) -> Self {
41        match s.to_lowercase().as_str() {
42            "development" | "dev" => Self::Development,
43            "test" | "testing" => Self::Test,
44            _ => Self::Production,
45        }
46    }
47
48    pub const fn is_development(&self) -> bool {
49        matches!(self, Self::Development)
50    }
51
52    pub const fn is_production(&self) -> bool {
53        matches!(self, Self::Production)
54    }
55
56    pub const fn is_test(&self) -> bool {
57        matches!(self, Self::Test)
58    }
59}
60
61impl Default for Environment {
62    fn default() -> Self {
63        Self::detect()
64    }
65}