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

use crate::merge::Merge;

/// You can create this type like `Some(...).into()` or `None.into()`.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MergingOption<T>(Option<T>)
where
    T: Merge + Default;

impl<T> MergingOption<T>
where
    T: Merge + Default,
{
    pub fn into_inner(self) -> Option<T> {
        self.0
    }

    pub fn as_ref(&self) -> Option<&T> {
        self.0.as_ref()
    }
}

impl<T> From<Option<T>> for MergingOption<T>
where
    T: Merge + Default,
{
    #[inline]
    fn from(v: Option<T>) -> Self {
        Self(v)
    }
}

impl<T> Merge for MergingOption<T>
where
    T: Merge + Default,
{
    fn merge(&mut self, other: Self) {
        if let Some(other) = other.0 {
            if self.0.is_none() {
                self.0 = Some(Default::default());
            }

            self.0.as_mut().unwrap().merge(other);
        }
    }
}