windjammer_runtime/
env.rs

1//! Environment variable operations
2//!
3//! Windjammer's `std::env` module maps to these functions.
4
5/// Get an environment variable
6pub fn var(key: &str) -> Result<String, String> {
7    std::env::var(key).map_err(|e| e.to_string())
8}
9
10/// Set an environment variable
11pub fn set_var(key: &str, value: &str) {
12    std::env::set_var(key, value);
13}
14
15/// Remove an environment variable
16pub fn remove_var(key: &str) {
17    std::env::remove_var(key);
18}
19
20/// Get all environment variables
21pub fn vars() -> Vec<(String, String)> {
22    std::env::vars().collect()
23}
24
25/// Get current working directory
26pub fn current_dir() -> Result<String, String> {
27    std::env::current_dir()
28        .map(|p| p.to_string_lossy().to_string())
29        .map_err(|e| e.to_string())
30}
31
32/// Get current executable path
33pub fn current_exe() -> Result<String, String> {
34    std::env::current_exe()
35        .map(|p| p.to_string_lossy().to_string())
36        .map_err(|e| e.to_string())
37}
38
39/// Get command line arguments
40pub fn args() -> Vec<String> {
41    std::env::args().collect()
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_set_get_var() {
50        set_var("WINDJAMMER_TEST", "hello");
51        assert_eq!(var("WINDJAMMER_TEST"), Ok("hello".to_string()));
52        remove_var("WINDJAMMER_TEST");
53        assert!(var("WINDJAMMER_TEST").is_err());
54    }
55
56    #[test]
57    fn test_current_dir() {
58        let dir = current_dir();
59        assert!(dir.is_ok());
60    }
61
62    #[test]
63    fn test_args() {
64        let args = args();
65        assert!(!args.is_empty());
66    }
67}