azalia-config 0.1.13

🐻‍❄️🪚 Defines traits, types, and utilities for dealing with application configuration
Documentation
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// 🐻‍❄️🪚 azalia: Noelware's Rust commons library.
// Copyright (c) 2024-2025 Noelware, LLC. <team@noelware.org>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//! Traits, types, and utilities when dealing with system environment variables.

use std::{
    char::ParseCharError,
    collections::{BTreeMap, BTreeSet, HashSet},
    convert::Infallible,
    env::{VarError, remove_var},
    ffi::OsStr,
    fmt::{Debug, Display},
    hash::{Hash, Hasher},
    marker::PhantomData,
    num::{
        NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8, NonZeroU16, NonZeroU32,
        NonZeroU64, NonZeroU128, NonZeroUsize, ParseFloatError, ParseIntError,
    },
    rc::Rc,
    str::ParseBoolError,
};

/// When reading from the system environment variables, types might want to convert
/// the value from `getenv` to something useful and this is where this trait comes in.
pub trait FromEnvValue: Sized {
    /// Implicit conversion between a environment variable's value to `Self::Output`.
    fn from_env_value(value: String) -> Self;
}

impl FromEnvValue for String {
    fn from_env_value(value: String) -> Self {
        value
    }
}

/// Analognous to [`FromEnvValue`] but it can fail if the given value is not right.
pub trait TryFromEnvValue: Sized {
    /// Error type.
    type Error;

    // TODO(@auguwu):
    // add `type Output = Self` once GAT defaults are stablised (probably never)

    /// Implicit conversion between a environment variable's value to `Ok(Self::Output)`
    /// if successful.
    fn try_from_env_value(value: String) -> Result<Self, Self::Error>;
}

impl<K: TryFromEnvValue + Eq + Hash, V: TryFromEnvValue> TryFromEnvValue for std::collections::HashMap<K, V> {
    type Error = MapTryFromEnvError<K::Error, V::Error>;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        let elements = value.split(',');
        let size_hint = elements.size_hint().0;
        let mut map = std::collections::HashMap::with_capacity(size_hint);

        for line in elements {
            if let Some((key, value)) = line.split_once('=') {
                if value.contains('=') {
                    continue;
                }

                let key = K::try_from_env_value(key.to_owned()).map_err(MapTryFromEnvError::Key)?;
                let value = V::try_from_env_value(value.to_owned()).map_err(MapTryFromEnvError::Value)?;

                map.insert(key, value);
            }
        }

        Ok(map)
    }
}

impl<K: TryFromEnvValue + Ord, V: TryFromEnvValue> TryFromEnvValue for BTreeMap<K, V> {
    type Error = MapTryFromEnvError<K::Error, V::Error>;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        let elements = value.split(',');
        let mut map = BTreeMap::new();

        for line in elements {
            if let Some((key, value)) = line.split_once('=') {
                if value.contains('=') {
                    continue;
                }

                let key = K::try_from_env_value(key.to_owned()).map_err(MapTryFromEnvError::Key)?;
                let value = V::try_from_env_value(value.to_owned()).map_err(MapTryFromEnvError::Value)?;

                map.insert(key, value);
            }
        }

        Ok(map)
    }
}

/// Error variant for <code>impl [`TryFromEnvValue`] for [`std::collections::HashMap`]<K, V></code>
/// and <code>impl [`TryFromEnvValue`] for [`std::collections::BTreeMap`]<K, V></code>.
#[derive(Debug)]
pub enum MapTryFromEnvError<K, V> {
    Key(K),
    Value(V),
}

impl<K: Display, V: Display> Display for MapTryFromEnvError<K, V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Key(s) => Display::fmt(s, f),
            Self::Value(v) => Display::fmt(v, f),
        }
    }
}

impl<K: std::error::Error + 'static, V: std::error::Error + 'static> std::error::Error for MapTryFromEnvError<K, V> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Key(k) => Some(k),
            Self::Value(v) => Some(v),
        }
    }
}

impl<T: TryFromEnvValue + Eq + Hash> TryFromEnvValue for HashSet<T> {
    type Error = T::Error;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        value
            .split(',')
            .map(|v| T::try_from_env_value(v.to_owned()))
            .collect::<Result<_, T::Error>>()
    }
}

impl<T: TryFromEnvValue + Ord> TryFromEnvValue for BTreeSet<T> {
    type Error = T::Error;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        value
            .split(',')
            .map(|v| T::try_from_env_value(v.to_owned()))
            .collect::<Result<_, T::Error>>()
    }
}

impl<T: TryFromEnvValue> TryFromEnvValue for Vec<T> {
    type Error = T::Error;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        value
            .split(',')
            .map(|v| T::try_from_env_value(v.to_owned()))
            .collect::<Result<Vec<_>, T::Error>>()
    }
}

#[cfg(feature = "tracing")]
#[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "tracing")))]
impl TryFromEnvValue for tracing::Level {
    type Error = InvalidLevel;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        match &*value.to_ascii_lowercase() {
            "trace" => Ok(tracing::Level::TRACE),
            "info" | "information" => Ok(tracing::Level::INFO),
            "debug" => Ok(tracing::Level::DEBUG),
            "warn" | "warning" => Ok(tracing::Level::WARN),
            "error" => Ok(tracing::Level::ERROR),
            level => Err(InvalidLevel(level.to_owned())),
        }
    }
}

#[cfg(feature = "tracing")]
#[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "tracing")))]
#[derive(Debug)]
/// A invalid level was given from the [`TryFromEnvValue`] implementation
/// for [`tracing::Level`]
pub struct InvalidLevel(String);

#[cfg(feature = "tracing")]
#[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "tracing")))]
impl std::fmt::Display for InvalidLevel {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "invalid log level: '{}'", self.0)
    }
}

#[cfg(feature = "tracing")]
#[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "tracing")))]
impl std::error::Error for InvalidLevel {}

macro_rules! impl_try_from_env {
    ($($(#[$meta:meta])* $Ty:ty: $Error:ty;)*) => {
        $(
            $(#[$meta])*
            /// This implementation will forward to the [`FromStr`] implementation
            /// of the concrete type.
            impl $crate::env::TryFromEnvValue for $Ty {
                type Error = $Error;

                fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
                    value.parse()
                }
            }
        )*
    };
}

impl_try_from_env!(
    bool: ParseBoolError;
    char: ParseCharError;

    f32: ParseFloatError;
    f64: ParseFloatError;

    NonZeroI8: ParseIntError;
    NonZeroI16: ParseIntError;
    NonZeroI32: ParseIntError;
    NonZeroI64: ParseIntError;
    NonZeroI128: ParseIntError;
    NonZeroIsize: ParseIntError;

    i8: ParseIntError;
    i16: ParseIntError;
    i32: ParseIntError;
    i64: ParseIntError;
    i128: ParseIntError;
    isize: ParseIntError;

    NonZeroU8: ParseIntError;
    NonZeroU16: ParseIntError;
    NonZeroU32: ParseIntError;
    NonZeroU64: ParseIntError;
    NonZeroU128: ParseIntError;
    NonZeroUsize: ParseIntError;

    u8: ParseIntError;
    u16: ParseIntError;
    u32: ParseIntError;
    u64: ParseIntError;
    u128: ParseIntError;
    usize: ParseIntError;

    std::path::PathBuf: Infallible;

    #[cfg(feature = "sentry")]
    #[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "sentry")))]
    sentry_types::Dsn: sentry_types::ParseDsnError;

    #[cfg(feature = "url")]
    #[cfg_attr(any(noeldoc, docsrs), doc(cfg(feature = "url")))]
    url::Url: url::ParseError;
);

impl<T: FromEnvValue> TryFromEnvValue for T {
    type Error = Infallible;

    fn try_from_env_value(value: String) -> Result<Self, Self::Error> {
        Ok(T::from_env_value(value))
    }
}

/// Parses an environment variable from a [`FromEnvValue`] implementation.
pub fn parse<K: Into<String>, V: FromEnvValue>(key: K) -> Result<V, VarError> {
    std::env::var(key.into()).map(V::from_env_value)
}

/// Parses an environment variable from a [`TryFromEnvValue`] implementation.
pub fn try_parse<K: Into<String>, V: TryFromEnvValue>(key: K) -> Result<V, TryParseError<V::Error>> {
    match std::env::var(key.into()) {
        Ok(value) => V::try_from_env_value(value).map_err(TryParseError::Parse),
        Err(e) => Err(TryParseError::System(e)),
    }
}

/// Analogous to [`try_parse`] but uses a closure to compute the default value.
pub fn try_parse_or<K: Into<String>, V: TryFromEnvValue>(
    key: K,
    default: impl FnOnce() -> V,
) -> Result<V, TryParseError<V::Error>> {
    match try_parse(key) {
        Ok(value) => Ok(value),
        Err(TryParseError::System(std::env::VarError::NotPresent)) => Ok(default()),
        Err(e) => Err(e),
    }
}

/// Analogous to [`try_parse`] but uses a default value if the environment variable was not found.
pub fn try_parse_or_else<K: Into<String>, V: TryFromEnvValue>(
    key: K,
    default: V,
) -> Result<V, TryParseError<V::Error>> {
    match std::env::var(key.into()) {
        Ok(value) => V::try_from_env_value(value).map_err(TryParseError::Parse),
        Err(VarError::NotPresent) => Ok(default),
        Err(e) => Err(TryParseError::System(e)),
    }
}

/// Anlogous to [`try_parse`] but returns a <code>[`Option`]\<V\></code> instead.
///
/// When the environment variable by the name of `key` doesn't exist, it'll return `None`.
pub fn try_parse_optional<K: Into<String>, V: TryFromEnvValue>(key: K) -> Result<Option<V>, TryParseError<V::Error>> {
    match std::env::var(key.into()) {
        Ok(value) => V::try_from_env_value(value).map(Some).map_err(TryParseError::Parse),
        Err(VarError::NotPresent) => Ok(None),
        Err(e) => Err(TryParseError::System(e)),
    }
}

/// Error variant for [`try_parse`].
#[derive(Debug)]
pub enum TryParseError<V> {
    System(VarError),
    Parse(V),
}

impl<V: Display> Display for TryParseError<V> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TryParseError::System(s) => Display::fmt(s, f),
            TryParseError::Parse(s) => Display::fmt(s, f),
        }
    }
}

impl<V: std::error::Error + 'static> std::error::Error for TryParseError<V> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::System(v) => Some(v),
            Self::Parse(v) => Some(v),
        }
    }
}

/// Represents a trait that allows conversion of a collection of system environment
/// variables for structs or enumerations.
///
/// ## Example
/// ```ignore
/// use azalia_config::env::FromEnv;
///
/// pub struct Config {
///     pub a: String,
/// }
///
/// impl FromEnv for Config {
///     fn from_env() -> Self {
///         Config { a: Default::default() }
///     }
/// }
///
/// let config = Config::from_env();
/// // => Config { a: "" }
/// ```
#[deprecated(
    since = "0.1.0",
    note = "trait is no longer needed as of azalia v0.1.0 (preparation of crates.io ver)"
)]
pub trait FromEnv: Sized {
    /// Implicit conversion to return `Self`.
    fn from_env() -> Self;
}

/// Analogous to [`FromEnv`] but falliable.
///
/// ***This is also a derive macro when the `macros` feature is enabled:
/// <code>#[derive([`TryFromEnv`][derive-redirect])</code>***
///
/// ## Notes
/// The **#[derive([`TryFromEnv`][derive-redirect])]** macro is unstable! Add
/// the `unstable` crate feature to use it.
///
/// For the derive macro, specifying the error type is required:
///
/// ```ignore
/// #[derive(TryFromEnv)]
/// #[env(Box<dyn std::error::Error>)]
/// ```
///
/// Since the procedural macro would have no idea on how to propagate errors
/// based off the context, it is required.
///
/// ## Example
/// ```ignore
/// use azalia_config::env::TryFromEnv;
///
/// #[derive(TryFromEnv)]
/// #[env(Box<dyn std::error::Error>, prefix = "APP")]
/// pub struct Config {
///     #[env("A", default)]
///     pub a: String,
/// }
///
/// let config = Config::try_from_env();
/// assert!(config.is_ok());
/// ```
///
/// [derive-redirect]: derive.TryFromEnv.html
pub trait TryFromEnv: Sized {
    /// Error type
    type Error;

    /// Implicit conversion to return a result of `Self`.
    fn try_from_env() -> Result<Self, Self::Error>;
}

#[allow(deprecated)]
impl<T: FromEnv> TryFromEnv for T {
    type Error = Infallible;

    fn try_from_env() -> Result<Self, Self::Error> {
        Ok(T::from_env())
    }
}

/// A guard type that drops the environment variable once the scope
/// is being dropped.
///
/// This type is [`!Send`](std::marker::Send) and [`!Sync`](std::marker::Sync) as it is unsafe
/// to drop environment variables in different threads.
///
/// ## Safety
///
/// <div class="warning">
///
/// As of Rust edition **2024**, `{set,remove}_var` is considered unsafe and
/// will call either way but this is a fair warning when using in a non-testing
/// environment.
///
/// </div>
///
/// This is only meant in testing environments so it is not our issue to deal
/// with if anything outside of testing goes unsound.
pub struct EnvGuard {
    name: String,
    _non_send_and_sync: PhantomData<Rc<()>>,
}

impl EnvGuard {
    /// Enters the guard and sets the name of the environment variable
    /// to the value of **1**.
    ///
    /// ## Safety
    /// Environment variables are inheritely unsafe to test! See the [`EnvGuard`]'s
    /// Safety documentation about it.
    ///
    /// ## Example
    /// ```
    /// use azalia_config::env::EnvGuard;
    /// use std::env;
    ///
    /// // The guard lives on this scope
    /// {
    ///     let _guard = EnvGuard::enter("HELLO");
    ///     assert!(env::var("HELLO").is_ok());
    /// }
    ///
    /// // and it'll be removed when dropped from scope
    /// assert!(env::var("HELLO").is_err());
    /// ```
    pub fn enter(name: impl Into<String>) -> Self {
        EnvGuard::enter_with(name, "1")
    }

    /// Enters the guard and sets the **name** to a correspondant **value** into
    /// the system environment variables.
    ///
    /// ## Safety
    /// Environment variables are inheritely unsafe to test! See the [`EnvGuard`]'s
    /// Safety documentation about it.
    ///
    /// ## Example
    /// ```
    /// use azalia_config::env::EnvGuard;
    /// use std::env;
    ///
    /// // The guard lives on this scope
    /// {
    ///     let guard = EnvGuard::enter_with("HELLO", "world");
    ///     assert_eq!(env::var("HELLO"), Ok(String::from("world")));
    /// }
    ///
    /// // and it'll be removed when dropped from scope
    /// assert!(env::var("HELLO").is_err());
    /// ```
    pub fn enter_with(name: impl Into<String>, value: impl AsRef<OsStr>) -> Self {
        let name = name.into();

        // Safety: rationale in Safety section of the struct
        unsafe { std::env::set_var(&name, value) };
        EnvGuard {
            name,
            _non_send_and_sync: PhantomData,
        }
    }
}

impl PartialEq for EnvGuard {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
    }
}

impl Eq for EnvGuard {}

impl Hash for EnvGuard {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
    }
}

impl Drop for EnvGuard {
    fn drop(&mut self) {
        unsafe { remove_var(&self.name) }
    }
}

/// A guard analogous to [`EnvGuard`] but holds a set of guards to be dropped
/// once the scope is finished.
///
/// This type is [`!Send`](std::marker::Send) and [`!Sync`](std::marker::Sync) as it is unsafe
/// to drop environment variables in different threads.
///
/// ## Safety
///
/// <div class="warning">
///
/// As of Rust edition **2024**, `{set,remove}_var` is considered unsafe and
/// will call either way but this is a fair warning when using in a non-testing
/// environment.
///
/// </div>
///
/// This is only meant in testing environments so it is not our issue to deal
/// with if anything outside of testing goes unsound.
pub struct MultipleEnvGuard {
    _variables: HashSet<EnvGuard>,
    _non_send_sync: PhantomData<Rc<()>>,
}

impl MultipleEnvGuard {
    /// Enters the guard and sets a iterator of `(key, value)` as [`EnvGuard`]s. On [`Drop`], it'll
    /// call [`remove_var`] of the specified environment variables.
    ///
    /// ## Safety
    /// Environment variables are inheritely unsafe to test! See the [`MultipleEnvGuard`]'s
    /// Safety documentation about it.
    ///
    /// ## Example
    /// ```
    /// use azalia_config::env::MultipleEnvGuard;
    /// use std::env::var;
    ///
    /// {
    ///     let _guard = MultipleEnvGuard::enter([
    ///         ("HELLO", "world"),
    ///         ("NOEL_IS_CUTE", "true")
    ///     ]);
    ///
    ///     assert_eq!(var("HELLO"), Ok(String::from("world")));
    ///     assert_ne!(var("NOEL_IS_CUTE"), Ok(String::from("false")));
    /// }
    ///
    /// assert!(var("HELLO").is_err());
    /// assert!(var("NOEL_IS_CUTE").is_err());
    /// ```
    pub fn enter(values: impl IntoIterator<Item = (impl Into<String>, impl AsRef<OsStr>)>) -> Self {
        MultipleEnvGuard {
            _non_send_sync: PhantomData,
            _variables: values
                .into_iter()
                .map(|(key, value)| EnvGuard::enter_with(key, value))
                .collect(),
        }
    }
}

/// Enters the [`EnvGuard`] by setting **key** to **1** and calls `f`.
///
/// ## Safety
/// Environment variables are inheritely unsafe to test! See the [`EnvGuard`]'s
/// Safety documentation about it.
pub fn enter(key: impl Into<String>, f: impl FnOnce()) {
    let _guard = EnvGuard::enter(key);
    f()
}

/// Enters the [`EnvGuard`] by setting **key** to the **value** and calls `f`.
///
/// ## Safety
/// Environment variables are inheritely unsafe to test! See the [`EnvGuard`]'s
/// Safety documentation about it.
pub fn enter_with(key: impl Into<String>, value: impl AsRef<OsStr>, f: impl FnOnce()) {
    let _guard = EnvGuard::enter_with(key, value);
    f()
}

/// Enters the [`EnvGuard`] by setting multiple environment variables via an iterator
/// implementation and calls **f**.
///
/// ## Safety
/// Environment variables are inheritely unsafe to test! See the [`MultipleEnvGuard`]'s
/// Safety documentation about it.
pub fn enter_multiple(iter: impl IntoIterator<Item = (impl Into<String>, impl AsRef<OsStr>)>, f: impl FnOnce()) {
    let _guard = MultipleEnvGuard::enter(iter);
    f()
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

    // this is a hack since we export `test` if `--features unstable` is enabled
    #[test]
    fn drop_multiple_env_guard() {
        {
            let mut guards = HashSet::new();
            guards.insert(EnvGuard::enter("HELLO"));

            assert!(std::env::var("HELLO").is_ok());
        }

        assert!(std::env::var("HELLO").is_err());

        {
            let _guard = MultipleEnvGuard::enter([("HELLO", "world")]);
            assert!(std::env::var("HELLO").is_ok());
        }

        assert!(std::env::var("HELLO").is_err());
    }

    #[test]
    fn map_try_from_env_value() {
        assert!(<HashMap<String, String> as TryFromEnvValue>::try_from_env_value("hello=world".into()).is_ok());
        assert!(<HashMap<String, String> as TryFromEnvValue>::try_from_env_value("helloworld".into()).is_ok());
        assert!(<HashMap<String, String> as TryFromEnvValue>::try_from_env_value("".into()).is_ok());
        assert!(
            <HashMap<String, String> as TryFromEnvValue>::try_from_env_value(
                "hello=world,weow=fluff;wwww,s=true".into()
            )
            .is_ok()
        );

        assert!(<BTreeMap<String, String> as TryFromEnvValue>::try_from_env_value("hello=world".into()).is_ok());
        assert!(<BTreeMap<String, String> as TryFromEnvValue>::try_from_env_value("helloworld".into()).is_ok());
        assert!(<BTreeMap<String, String> as TryFromEnvValue>::try_from_env_value("".into()).is_ok());
        assert!(
            <BTreeMap<String, String> as TryFromEnvValue>::try_from_env_value(
                "hello=world,weow=fluff;wwww,s=true".into()
            )
            .is_ok()
        );
    }

    #[test]
    fn set_try_from_env_value() {
        assert!(<HashSet<String> as TryFromEnvValue>::try_from_env_value("hello,world".into()).is_ok());
        assert!(<HashSet<String> as TryFromEnvValue>::try_from_env_value("helloworld".into()).is_ok());
        assert!(<HashSet<String> as TryFromEnvValue>::try_from_env_value("".into()).is_ok());
        assert!(<HashSet<String> as TryFromEnvValue>::try_from_env_value("hello,world,weow,fluff".into()).is_ok());

        assert!(<BTreeSet<String> as TryFromEnvValue>::try_from_env_value("hello,world".into()).is_ok());
        assert!(<BTreeSet<String> as TryFromEnvValue>::try_from_env_value("helloworld".into()).is_ok());
        assert!(<BTreeSet<String> as TryFromEnvValue>::try_from_env_value("".into()).is_ok());
        assert!(<BTreeSet<String> as TryFromEnvValue>::try_from_env_value("hello,world,weow,fluff".into()).is_ok());
    }
}