swc_config/types/
bool_config.rs

1use serde::{Deserialize, Serialize};
2
3use crate::merge::Merge;
4
5/// You can create this type like `true.into()` or `false.into()`.
6#[derive(
7    Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
8)]
9pub struct BoolConfig<const DEFAULT: bool>(#[serde(default)] Option<bool>);
10
11impl<const DEFAULT: bool> BoolConfig<DEFAULT> {
12    /// Creates a new `BoolConfig` with the given value.
13    #[inline]
14    pub fn new(value: Option<bool>) -> Self {
15        Self(value)
16    }
17
18    /// Returns the value specified by the user or the default value.
19    #[inline]
20    pub fn into_bool(self) -> bool {
21        self.into()
22    }
23}
24
25impl<const DEFAULT: bool> From<BoolConfig<DEFAULT>> for bool {
26    #[inline]
27    fn from(v: BoolConfig<DEFAULT>) -> Self {
28        match v.0 {
29            Some(v) => v,
30            _ => DEFAULT,
31        }
32    }
33}
34
35impl<const DEFAULT: bool> From<Option<bool>> for BoolConfig<DEFAULT> {
36    #[inline]
37    fn from(v: Option<bool>) -> Self {
38        Self(v)
39    }
40}
41
42impl<const DEFAULT: bool> From<bool> for BoolConfig<DEFAULT> {
43    #[inline]
44    fn from(v: bool) -> Self {
45        Self(Some(v))
46    }
47}
48
49impl<const DEFAULT: bool> Merge for BoolConfig<DEFAULT> {
50    #[inline]
51    fn merge(&mut self, other: Self) {
52        self.0.merge(other.0);
53    }
54}