devela/sys/env/
env.rs

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// devela::sys::env::env
//
//! A namespaced wrapper for std
//
// NOTES:
// - The lists of expected values for configuration flags can be copied from:
//   https://github.com/rust-lang/rust/blob/master/tests/ui/check-cfg/well-known-values.stderr
//   - Afterwards they can be procesed with vim commands similar to:
//     s/`//g
//     s/, /\r/g
//     s/\([a-z0-9_.]\+\)/#[cfg(target_arch = "\1")]\r{ "\1" }/
//     s/\([a-z0-9_.]\+\)/target_arch = "\1",/

#[cfg(feature = "std")]
use crate::{IoResult, OsStr, OsString, Path, PathBuf, _dep::_std::env::*};

/// A namespaced wrapper for `std::env` functions and constants.
pub struct Env;

impl Env {}

/// # Functions related to command line arguments
#[cfg(feature = "std")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
impl Env {
    /// Returns the arguments that this program was started with.
    ///
    /// See [args].
    pub fn args() -> Args {
        args()
    }

    /// See [args_os].
    pub fn args_os() -> ArgsOs {
        args_os()
    }
}

/// # Functions related to environment variables
#[cfg(feature = "std")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
impl Env {
    /// Fetches the environment variable key from the current process.
    ///
    /// See [var].
    pub fn var<K: AsRef<OsStr>>(key: K) -> Result<String, VarError> {
        var(key)
    }

    /// Returns an iterator of (variable, value) pairs of strings,
    /// for all the environment variables of the current process.
    ///
    /// See [vars].
    pub fn vars() -> Vars {
        vars()
    }

    /// Fetches the environment variable key from the current process.
    ///
    /// See [var_os].
    pub fn var_os<K: AsRef<OsStr>>(key: K) -> Option<OsString> {
        var_os(key)
    }

    /// Returns an iterator of (variable, value) pairs of OS strings,
    /// for all the environment variables of the current process.
    ///
    /// See [vars_os].
    pub fn vars_os() -> VarsOs {
        vars_os()
    }

    /// Removes the environment variable `key` from the environment
    /// of the currently running process.
    ///
    /// # Safety
    /// See [remove_var].
    #[cfg(all(not(feature = "safe_sys"), feature = "unsafe_thread"))]
    pub unsafe fn remove_var<K: AsRef<OsStr>>(key: K) {
        unsafe { remove_var(key) }
    }

    /// Sets the environment variable `key` to the value `value`
    /// for the currently running process.
    ///
    /// # Safety
    /// See [set_var].
    #[cfg(all(not(feature = "safe_sys"), feature = "unsafe_thread"))]
    pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
        unsafe { set_var(key, value) }
    }
}

/// # Functions related to paths.
#[cfg(feature = "std")]
#[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
impl Env {
    /// Returns the full filesystem path of the current running executable.
    ///
    /// See [current_exe].
    pub fn current_exe() -> IoResult<PathBuf> {
        current_exe()
    }

    /// Returns the current working directory.
    ///
    /// See [current_dir].
    pub fn current_dir() -> IoResult<PathBuf> {
        current_dir()
    }

    /// Changes the current working directory to the specified path.
    ///
    /// See [set_current_dir].
    pub fn set_current_dir<P: AsRef<Path>>(path: P) -> IoResult<()> {
        set_current_dir(path)
    }

    /// Returns the path of a temporary directory.
    ///
    /// See [temp_dir].
    pub fn temp_dir() -> PathBuf {
        temp_dir()
    }

    /// Joins a collection of [Path]s appropriately for the `PATH` environment variable.
    ///
    /// See [join_paths].
    pub fn join_paths<I, T>(paths: I) -> Result<OsString, JoinPathsError>
    where
        I: IntoIterator<Item = T>,
        T: AsRef<OsStr>,
    {
        join_paths(paths)
    }

    /// Parses input according to platform conventions for the `PATH` environment variable.
    ///
    /// See [split_paths].
    pub fn split_paths<T: AsRef<OsStr> + ?Sized>(unparsed: &T) -> SplitPaths<'_> {
        split_paths(unparsed)
    }
}

/// # Constants
impl Env {
    /// A string describing the architecture of the CPU that is currently in use.
    ///
    /// Expected values are:
    /// `aarch64`, `arm`, `arm64ec`, `avr`, `bpf`, `csky`, `hexagon`, `loongarch64`, `m68k`, `mips`, `mips32r6`, `mips64`, `mips64r6`, `msp430`, `nvptx64`, `powerpc`, `powerpc64`, `riscv32`, `riscv64`, `s390x`, `sparc`, `sparc64`, `wasm32`, `wasm64`, `x86`, `x86_64`, and `xtensa`.
    #[rustfmt::skip]
    pub const ARCH: &'static str = {
        #[cfg(feature = "std")]
        { std::env::consts::ARCH }
        #[cfg(not(feature = "std"))]
        { // 27 arches:
            #[cfg(target_arch = "aarch64")]
            { "aarch64" }
            #[cfg(target_arch = "arm")]
            { "arm" }
            #[cfg(target_arch = "arm64ec")]
            { "arm64ec" }
            #[cfg(target_arch = "avr")]
            { "avr" }
            #[cfg(target_arch = "bpf")]
            { "bpf" }
            #[cfg(target_arch = "csky")]
            { "csky" }
            #[cfg(target_arch = "hexagon")]
            { "hexagon" }
            #[cfg(target_arch = "loongarch64")]
            { "loongarch64" }
            #[cfg(target_arch = "m68k")]
            { "m68k" }
            #[cfg(target_arch = "mips")]
            { "mips" }
            #[cfg(target_arch = "mips32r6")]
            { "mips32r6" }
            #[cfg(target_arch = "mips64")]
            { "mips64" }
            #[cfg(target_arch = "mips64r6")]
            { "mips64r6" }
            #[cfg(target_arch = "msp430")]
            { "msp430" }
            #[cfg(target_arch = "nvptx64")]
            { "nvptx64" }
            #[cfg(target_arch = "powerpc")]
            { "powerpc" }
            #[cfg(target_arch = "powerpc64")]
            { "powerpc64" }
            #[cfg(target_arch = "riscv32")]
            { "riscv32" }
            #[cfg(target_arch = "riscv64")]
            { "riscv64" }
            #[cfg(target_arch = "s390x")]
            { "s390x" }
            #[cfg(target_arch = "sparc")]
            { "sparc" }
            #[cfg(target_arch = "sparc64")]
            { "sparc64" }
            #[cfg(target_arch = "wasm32")]
            { "wasm32" }
            #[cfg(target_arch = "wasm64")]
            { "wasm64" }
            #[cfg(target_arch = "x86")]
            { "x86" }
            #[cfg(target_arch = "x86_64")]
            { "x86_64" }
            #[cfg(target_arch = "xtensa")]
            { "xtensa" }
            #[cfg(not(any(
                target_arch = "aarch64",
                target_arch = "arm",
                target_arch = "arm64ec",
                target_arch = "avr",
                target_arch = "bpf",
                target_arch = "csky",
                target_arch = "hexagon",
                target_arch = "loongarch64",
                target_arch = "m68k",
                target_arch = "mips",
                target_arch = "mips32r6",
                target_arch = "mips64",
                target_arch = "mips64r6",
                target_arch = "msp430",
                target_arch = "nvptx64",
                target_arch = "powerpc",
                target_arch = "powerpc64",
                target_arch = "riscv32",
                target_arch = "riscv64",
                target_arch = "s390x",
                target_arch = "sparc",
                target_arch = "sparc64",
                target_arch = "wasm32",
                target_arch = "wasm64",
                target_arch = "x86",
                target_arch = "x86_64",
                target_arch = "xtensa",
            )))]
            { "unknown" }
        }
    };

    /// The family of the operating system.
    ///
    /// The expected values are: `unix`, `wasm`, and `windows`
    #[rustfmt::skip]
    pub const FAMILY: &'static str = {
        #[cfg(feature = "std")]
        { std::env::consts::FAMILY }
        #[cfg(not(feature = "std"))]
        {
            #[cfg(target_family = "unix")]
            { "unix" }
            #[cfg(target_family = "wasm")]
            { "wasm" }
            #[cfg(target_family = "windows")]
            { "windows" }
            #[cfg(not(any(
                target_family = "unix",
                target_family = "wasm",
                target_family = "windows",
            )))]
            { "unknown" }
        }
    };

    /// A string describing the vendor of the CPU that is currently in use.
    ///
    /// Expected values are:
    ///  `apple`, `espressif`, `fortanix`, `ibm`, `kmc`, `nintendo`, `nvidia`, `pc`, `risc0`, `sony`, `sun`, `unikraft`, `unknown`, `uwp`, `win7`, and `wrs`.
    #[rustfmt::skip]
    pub const VENDOR: &'static str = { // 15 vendors
        #[cfg(target_vendor = "apple")]
        { "apple" }
        #[cfg(target_vendor = "espressif")]
        { "espressif" }
        #[cfg(target_vendor = "fortanix")]
        { "fortanix" }
        #[cfg(target_vendor = "ibm")]
        { "ibm" }
        #[cfg(target_vendor = "kmc")]
        { "kmc" }
        #[cfg(target_vendor = "nintendo")]
        { "nintendo" }
        #[cfg(target_vendor = "nvidia")]
        { "nvidia" }
        #[cfg(target_vendor = "pc")]
        { "pc" }
        #[cfg(target_vendor = "risc0")]
        { "risc0" }
        #[cfg(target_vendor = "sony")]
        { "sony" }
        #[cfg(target_vendor = "sun")]
        { "sun" }
        #[cfg(target_vendor = "unikraft")]
        { "unikraft" }
        #[cfg(target_vendor = "uwp")]
        { "uwp" }
        #[cfg(target_vendor = "win7")]
        { "win7" }
        #[cfg(target_vendor = "wrs")]
        { "wrs" }
        #[cfg(not(any(
            target_vendor = "apple",
            target_vendor = "espressif",
            target_vendor = "fortanix",
            target_vendor = "ibm",
            target_vendor = "kmc",
            target_vendor = "nintendo",
            target_vendor = "nvidia",
            target_vendor = "pc",
            target_vendor = "risc0",
            target_vendor = "sony",
            target_vendor = "sun",
            target_vendor = "unikraft",
            target_vendor = "uwp",
            target_vendor = "win7",
            target_vendor = "wrs",
        )))]
        { "unknown" }
    };

    /// A string describing the specific operating system in use.
    ///
    /// The expected values are:
    /// `aix`, `android`, `cuda`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm`.
    #[rustfmt::skip]
    pub const OS: &'static str = {
        #[cfg(feature = "std")]
        { std::env::consts::OS }
        #[cfg(not(feature = "std"))]
        { // 40 oses:
            #[cfg(target_os = "aix")]
            { "aix" }
            #[cfg(target_os = "android")]
            { "android" }
            #[cfg(target_os = "cuda")]
            { "cuda" }
            #[cfg(target_os = "dragonfly")]
            { "dragonfly" }
            #[cfg(target_os = "emscripten")]
            { "emscripten" }
            #[cfg(target_os = "espidf")]
            { "espidf" }
            #[cfg(target_os = "freebsd")]
            { "freebsd" }
            #[cfg(target_os = "fuchsia")]
            { "fuchsia" }
            #[cfg(target_os = "haiku")]
            { "haiku" }
            #[cfg(target_os = "hermit")]
            { "hermit" }
            #[cfg(target_os = "horizon")]
            { "horizon" }
            #[cfg(target_os = "hurd")]
            { "hurd" }
            #[cfg(target_os = "illumos")]
            { "illumos" }
            #[cfg(target_os = "ios")]
            { "ios" }
            #[cfg(target_os = "l4re")]
            { "l4re" }
            #[cfg(target_os = "linux")]
            { "linux" }
            #[cfg(target_os = "macos")]
            { "macos" }
            #[cfg(target_os = "netbsd")]
            { "netbsd" }
            #[cfg(target_os = "none")]
            { "none" }
            #[cfg(target_os = "nto")]
            { "nto" }
            #[cfg(target_os = "nuttx")]
            { "nuttx" }
            #[cfg(target_os = "openbsd")]
            { "openbsd" }
            #[cfg(target_os = "psp")]
            { "psp" }
            // WAIT: 1.84
            // #[cfg(target_os = "psx")]
            // { "psx" }
            #[cfg(target_os = "redox")]
            { "redox" }
            #[cfg(target_os = "rtems")]
            { "rtems" }
            #[cfg(target_os = "solaris")]
            { "solaris" }
            #[cfg(target_os = "solid_asp3")]
            { "solid_asp3" }
            #[cfg(target_os = "teeos")]
            { "teeos" }
            #[cfg(target_os = "trusty")]
            { "trusty" }
            #[cfg(target_os = "tvos")]
            { "tvos" }
            #[cfg(target_os = "uefi")]
            { "uefi" }
            #[cfg(target_os = "unknown")]
            { "unknown" }
            #[cfg(target_os = "visionos")]
            { "visionos" }
            #[cfg(target_os = "vita")]
            { "vita" }
            #[cfg(target_os = "vxworks")]
            { "vxworks" }
            #[cfg(target_os = "wasi")]
            { "wasi" }
            #[cfg(target_os = "watchos")]
            { "watchos" }
            #[cfg(target_os = "windows")]
            { "windows" }
            #[cfg(target_os = "xous")]
            { "xous" }
            #[cfg(target_os = "zkvm")]
            { "zkvm" }
            #[cfg(not(any(
                target_os = "aix",
                target_os = "android",
                target_os = "cuda",
                target_os = "dragonfly",
                target_os = "emscripten",
                target_os = "espidf",
                target_os = "freebsd",
                target_os = "fuchsia",
                target_os = "haiku",
                target_os = "hermit",
                target_os = "horizon",
                target_os = "hurd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "l4re",
                target_os = "linux",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "none",
                target_os = "nto",
                target_os = "nuttx",
                target_os = "openbsd",
                target_os = "psp",
                // WAIT: 1.84
                // target_os = "psx",
                target_os = "redox",
                target_os = "rtems",
                target_os = "solaris",
                target_os = "solid_asp3",
                target_os = "teeos",
                target_os = "trusty",
                target_os = "tvos",
                target_os = "uefi",
                target_os = "unknown",
                target_os = "visionos",
                target_os = "vita",
                target_os = "vxworks",
                target_os = "wasi",
                target_os = "watchos",
                target_os = "windows",
                target_os = "xous",
                target_os = "zkvm",
            )))]
            { "unknown" }
        }
    };

    /// Specifies the file extension used for shared libraries on this platform
    /// that goes after the dot.
    ///
    /// Some possible values:
    /// - so
    /// - dylib
    /// - dll
    #[cfg(feature = "std")]
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
    pub const DLL_EXTENSION: &'static str = std::env::consts::DLL_EXTENSION;

    /// Specifies the filename prefix used for shared libraries on this platform.
    ///
    /// Some possible values:
    /// - lib
    /// - "" (an empty string)
    #[cfg(feature = "std")]
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
    pub const DLL_PREFIX: &'static str = std::env::consts::DLL_PREFIX;

    /// Specifies the filename suffix used for shared libraries on this platform.
    ///
    /// Some possible values:
    /// - .so
    /// - .dylib
    /// - .dll
    #[cfg(feature = "std")]
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
    pub const DLL_SUFFIX: &'static str = std::env::consts::DLL_SUFFIX;

    /// Specifies the file extension, if any, used for executable binaries on this platform.
    ///
    /// Some possible values:
    /// - exe
    /// - "" (an empty string)
    #[cfg(feature = "std")]
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
    pub const EXE_EXTENSION: &'static str = std::env::consts::EXE_EXTENSION;

    /// Specifies the filename suffix used for executable binaries on this platform.
    ///
    /// Some possible values:
    /// - .exe
    /// - .nexe
    /// - .pexe
    /// - "" (an empty string)
    #[cfg(feature = "std")]
    #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "std")))]
    pub const EXE_SUFFIX: &'static str = std::env::consts::EXE_SUFFIX;
}