Skip to main content

logicaffeine_system/
env.rs

1//! Environment Variable and Argument Access
2//!
3//! Provides access to environment variables and command-line arguments.
4//!
5//! # Platform Support
6//!
7//! - **Native**: Full access to system environment
8//! - **WASM**: Not available (module not compiled for wasm32)
9//!
10//! # Example
11//!
12//! ```
13//! use logicaffeine_system::env;
14//!
15//! // Read environment variable
16//! if let Some(home) = env::get("HOME".to_string()) {
17//!     assert!(!home.is_empty());
18//! }
19//!
20//! // Command-line arguments are always available
21//! let args = env::args();
22//! assert!(!args.is_empty());
23//! ```
24
25use logicaffeine_data::LogosSeq;
26use std::env as std_env;
27
28/// Returns the value of an environment variable.
29///
30/// # Arguments
31///
32/// * `key` - The environment variable name
33///
34/// # Returns
35///
36/// `Some(value)` if the variable exists and is valid UTF-8, `None` otherwise.
37///
38/// # Example
39///
40/// ```
41/// use logicaffeine_system::env;
42///
43/// let path = env::get("PATH".to_string());
44/// ```
45pub fn get(key: String) -> Option<String> {
46    if let Some(sealed) = crate::testworld::intercept_env_get(&key) {
47        return sealed;
48    }
49    std_env::var(&key).ok()
50}
51
52/// Returns command-line arguments as a vector.
53///
54/// The first element is the program name (or path), followed by any
55/// arguments passed to the program.
56///
57/// # Returns
58///
59/// A vector of all command-line arguments.
60///
61/// # Example
62///
63/// ```
64/// use logicaffeine_system::env;
65///
66/// let args = env::args();
67/// assert!(!args.is_empty());
68/// ```
69pub fn args() -> LogosSeq<String> {
70    LogosSeq::from_vec(std_env::args().collect())
71}