swc_config/types/
option.rs

1use serde::{Deserialize, Serialize};
2
3use crate::merge::Merge;
4
5/// You can create this type like `Some(...).into()` or `None.into()`.
6#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub struct MergingOption<T>(Option<T>)
8where
9    T: Merge + Default;
10
11impl<T> MergingOption<T>
12where
13    T: Merge + Default,
14{
15    pub fn into_inner(self) -> Option<T> {
16        self.0
17    }
18
19    pub fn as_ref(&self) -> Option<&T> {
20        self.0.as_ref()
21    }
22}
23
24impl<T> From<Option<T>> for MergingOption<T>
25where
26    T: Merge + Default,
27{
28    #[inline]
29    fn from(v: Option<T>) -> Self {
30        Self(v)
31    }
32}
33
34impl<T> Merge for MergingOption<T>
35where
36    T: Merge + Default,
37{
38    fn merge(&mut self, other: Self) {
39        if let Some(other) = other.0 {
40            if self.0.is_none() {
41                self.0 = Some(Default::default());
42            }
43
44            self.0.as_mut().unwrap().merge(other);
45        }
46    }
47}