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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! # Configuration Module
//!
//! This module provides panic-free utilities for managing environment variables
//! using the [dotenvy](https://docs.rs) crate.
//!
//! Functionality includes loading `.env` files into the system environment and
//! retrieving values with optional fallbacks.
use dotenv;
use env;
use Error;
/// Initializes the environment by loading the `.env` file into `std::env`.
///
/// This function attempts to load variables from a `.env` file in the current or parent
/// directories into the system environment. The absence of a `.env` file is ignored
/// to support production environments where variables are set via the operating system.
///
/// # Returns
/// * `Ok(())` - Successfully processed the environment (even if no file was found).
/// * `Err(Box<dyn Error>)` - If a `.env` file exists but is corrupted or unreadable.
///
/// # Example
/// ```
/// use crator::init_env;
///
/// if let Err(e) = init_env() {
/// eprintln!("Initialization failed: {}", e);
/// }
/// ```
/// Retrieves an environment variable by key.
///
/// This provides a safe interface for variables that must exist for the
/// application to function, such as database credentials or API keys.
///
/// # Errors
/// Returns [std::env::VarError] if:
/// * The variable is not present in the environment.
/// * The variable contains invalid Unicode data.
///
/// # Example
/// ```
/// use crator::get_env;
///
/// match get_env("DATABASE_URL") {
/// Ok(val) => println!("Database URL: {}", val),
/// Err(e) => eprintln!("Missing required variable: {}", e),
/// }
/// ```
/// Retrieves an environment variable or returns a provided default value.
///
/// This function is intended for non-critical configuration settings. It attempts
/// to load the `.env` file internally to ensure local variables are available if
/// [init_env] was not previously called.
///
/// # Note
/// While this function handles internal initialization, calling [init_env] once
/// at application startup is more efficient for multiple lookups to avoid
/// repeated file system operations.
///
/// # Example
/// ```
/// use crator::get_env_or;
///
/// // Returns the String "8080" if the "PORT" environment variable is missing
/// let port = get_env_or("PORT", "8080");
///
/// // Returns the String "info" if the "LOG_LEVEL" environment variable is missing
/// let level = get_env_or("LOG_LEVEL", "info");
/// ```
/// Retrieves an environment variable and attempts to parse it into the requested type.
///
/// This function attempts to load the `.env` file internally if it has not been
/// loaded yet.
///
/// # Errors
/// Returns an error if:
/// * The variable is not present.
/// * The variable contains invalid Unicode.
/// * The value cannot be parsed into type `T`.
///
/// # Example
/// ```
/// use crator::get_env_parse;
///
/// let port: u16 = get_env_parse("PORT").unwrap_or(8080);
/// let debug_mode: bool = get_env_parse("DEBUG").unwrap_or(false);
/// ```
/// Retrieves an environment variable and parses it, returning a default value on failure.
///
/// This function attempts to load the `.env` file internally if it has not been
/// loaded yet.
///
/// # Example
/// ```
/// use crator::get_env_parse_or;
/// let port = get_env_parse_or("PORT", 8080);
/// ```