env_type/is_debug.rs
1/// This is used to determine if the current environment is a debug environment.
2/// The IsDebug trait is used to check if the environment type is debug.
3///
4/// # Example
5///
6/// ```
7/// use env_type::types::EnvType;
8/// use env_type::is_debug::{IsDebug, IsDebugContext, debug_context};
9/// use env_type::environment::EnvironmentBuilder;
10///
11/// let is_debug_context = debug_context().build();
12///
13/// let env = EnvironmentBuilder::default()
14/// .current_env(EnvType::Dev)
15/// .with_context(is_debug_context)
16/// .build();
17///
18/// assert!(env.is_ok());
19/// let env = env.unwrap();
20/// assert_eq!(EnvType::Dev, *env.current_env());
21/// assert_eq!(true, env.is_debug());
22/// ```
23use crate::context::{ContextBuilder, ContextMarker};
24use crate::environment::Environment;
25use crate::types::EnvType;
26
27pub struct IsDebugContext;
28
29impl ContextMarker for IsDebugContext {
30 type Value = bool;
31}
32
33pub fn debug_context() -> ContextBuilder<IsDebugContext> {
34 ContextBuilder::<IsDebugContext>::default()
35 .with_value(EnvType::Dev, true)
36 .with_default(false)
37}
38
39pub trait IsDebug {
40 fn is_debug(&self) -> bool;
41}
42
43impl IsDebug for Environment {
44 fn is_debug(&self) -> bool {
45 self.current_value::<IsDebugContext>().unwrap_or(false)
46 }
47}