1
use serde_derive::{Deserialize, Serialize};
2

            
3
// GRCOV_EXCL_START
4
bitflags::bitflags! {
5
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
6
    pub struct Extensions: usize {
7
        const UNWRAP_NEWTYPES = 0x1;
8
        const IMPLICIT_SOME = 0x2;
9
        const UNWRAP_VARIANT_NEWTYPES = 0x4;
10
        /// During serialization, this extension emits struct names. See also [`PrettyConfig::struct_names`](crate::ser::PrettyConfig::struct_names) for the [`PrettyConfig`](crate::ser::PrettyConfig) equivalent.
11
        ///
12
        /// During deserialization, this extension requires that structs' names are stated explicitly.
13
        const EXPLICIT_STRUCT_NAMES = 0x8;
14
    }
15
}
16
// GRCOV_EXCL_STOP
17

            
18
impl Extensions {
19
    /// Creates an extension flag from an ident.
20
    #[must_use]
21
33790
    pub fn from_ident(ident: &str) -> Option<Extensions> {
22
87010
        for (name, extension) in Extensions::all().iter_names() {
23
87010
            if ident == name.to_lowercase() {
24
33778
                return Some(extension);
25
53232
            }
26
        }
27

            
28
12
        None
29
33790
    }
30
}
31

            
32
// GRCOV_EXCL_START
33
impl Default for Extensions {
34
    fn default() -> Self {
35
        Extensions::empty()
36
    }
37
}
38
// GRCOV_EXCL_STOP
39

            
40
#[cfg(test)]
41
mod tests {
42
    use super::Extensions;
43

            
44
64
    fn roundtrip_extensions(ext: Extensions) {
45
64
        let ron = crate::to_string(&ext).unwrap();
46
64
        let ext2: Extensions = crate::from_str(&ron).unwrap();
47
64
        assert_eq!(ext, ext2);
48
64
    }
49

            
50
    #[test]
51
4
    fn test_extension_serde() {
52
        // iterate over the powerset of all extensions (i.e. every possible combination of extensions)
53
64
        for bits in Extensions::empty().bits()..=Extensions::all().bits() {
54
64
            let extensions = Extensions::from_bits_retain(bits);
55
64
            roundtrip_extensions(extensions);
56
64
        }
57
4
    }
58
}