Repository navigation
Expand file tree
/
Copy pathenvironment.rs
More file actions
116 lines (106 loc) · 3.26 KB
/
Copy pathenvironment.rs
File metadata and controls
116 lines (106 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2025, The Ruskit Authors
// MIT License
// All rights reserved.
//! Provides environment types and utilities for environment-specific configuration.
//!
//! This module defines the `Environment` enum and related functionality for
//! determining and working with different deployment environments.
use std::{
env,
fmt::{Display, Formatter, Result},
};
/// # Environment
///
/// Represents the deployment environment for an application.
///
/// This enum provides variants for common deployment stages (Local, Dev, Staging, Prod)
/// and methods to determine the current environment based on environment variables.
///
/// ## Examples
///
/// ```
/// use configs::Environment;
///
/// // Get environment from RUST_ENV environment variable
/// let current_env = Environment::from_rust_env();
///
/// // Check if we're in production
/// if current_env.is_prod() {
/// println!("Running in production mode");
/// }
/// ```
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Environment {
/// Local development environment (default)
#[default]
Local,
/// Development environment
Dev,
/// Staging/testing environment
Staging,
/// Production environment
Prod,
}
impl Display for Environment {
fn fmt(&self, f: &mut Formatter) -> Result {
let printable = match *self {
Environment::Local => "local",
Environment::Dev => "dev",
Environment::Staging => "stg",
Environment::Prod => "prd",
};
write!(f, "{}", printable)
}
}
impl Environment {
/// Creates an `Environment` based on the RUST_ENV environment variable.
///
/// This method checks the value of the `RUST_ENV` variable and returns
/// the corresponding `Environment` variant. If the variable is unset or
/// doesn't match any known environment, it defaults to `Environment::Local`.
///
/// ## Returns
///
/// An `Environment` value derived from the environment variable.
pub fn from_rust_env() -> Environment {
let env = env::var("RUST_ENV").unwrap_or_default();
match env.as_str() {
"production" | "PRODUCTION" | "prod" | "PROD" | "prd" | "PRD" => Environment::Prod,
"staging" | "STAGING" | "stg" | "STG" => Environment::Staging,
"develop" | "DEVELOP" | "dev" | "DEV" => Environment::Dev,
_ => Environment::Local,
}
}
/// Checks if the environment is Local.
///
/// ## Returns
///
/// `true` if the environment is Local, `false` otherwise.
pub fn is_local(&self) -> bool {
self == &Environment::Local
}
/// Checks if the environment is Dev.
///
/// ## Returns
///
/// `true` if the environment is Dev, `false` otherwise.
pub fn is_dev(&self) -> bool {
self == &Environment::Dev
}
/// Checks if the environment is Staging.
///
/// ## Returns
///
/// `true` if the environment is Staging, `false` otherwise.
pub fn is_stg(&self) -> bool {
self == &Environment::Staging
}
/// Checks if the environment is Production.
///
/// ## Returns
///
/// `true` if the environment is Production, `false` otherwise.
pub fn is_prod(&self) -> bool {
self == &Environment::Prod
}
}