swc_config/config_types/
bool_config.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
use serde::{Deserialize, Serialize};

use crate::merge::Merge;

/// You can create this type like `true.into()` or `false.into()`.
#[derive(
    Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
pub struct BoolConfig<const DEFAULT: bool>(#[serde(default)] Option<bool>);

impl<const DEFAULT: bool> BoolConfig<DEFAULT> {
    /// Creates a new `BoolConfig` with the given value.
    #[inline]
    pub fn new(value: Option<bool>) -> Self {
        Self(value)
    }

    /// Returns the value specified by the user or the default value.
    #[inline]
    pub fn into_bool(self) -> bool {
        self.into()
    }
}

impl<const DEFAULT: bool> From<BoolConfig<DEFAULT>> for bool {
    #[inline]
    fn from(v: BoolConfig<DEFAULT>) -> Self {
        match v.0 {
            Some(v) => v,
            _ => DEFAULT,
        }
    }
}

impl<const DEFAULT: bool> From<Option<bool>> for BoolConfig<DEFAULT> {
    #[inline]
    fn from(v: Option<bool>) -> Self {
        Self(v)
    }
}

impl<const DEFAULT: bool> From<bool> for BoolConfig<DEFAULT> {
    #[inline]
    fn from(v: bool) -> Self {
        Self(Some(v))
    }
}

impl<const DEFAULT: bool> Merge for BoolConfig<DEFAULT> {
    #[inline]
    fn merge(&mut self, other: Self) {
        self.0.merge(other.0);
    }
}