ron/
extensions.rs

1use serde_derive::{Deserialize, Serialize};
2
3// GRCOV_EXCL_START
4bitflags::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
18impl Extensions {
19    /// Creates an extension flag from an ident.
20    #[must_use]
21    pub fn from_ident(ident: &str) -> Option<Extensions> {
22        for (name, extension) in Extensions::all().iter_names() {
23            if ident == name.to_lowercase() {
24                return Some(extension);
25            }
26        }
27
28        None
29    }
30}
31
32// GRCOV_EXCL_START
33impl Default for Extensions {
34    fn default() -> Self {
35        Extensions::empty()
36    }
37}
38// GRCOV_EXCL_STOP
39
40#[cfg(test)]
41mod tests {
42    use super::Extensions;
43
44    fn roundtrip_extensions(ext: Extensions) {
45        let ron = crate::to_string(&ext).unwrap();
46        let ext2: Extensions = crate::from_str(&ron).unwrap();
47        assert_eq!(ext, ext2);
48    }
49
50    #[test]
51    fn test_extension_serde() {
52        // iterate over the powerset of all extensions (i.e. every possible combination of extensions)
53        for bits in Extensions::empty().bits()..=Extensions::all().bits() {
54            let extensions = Extensions::from_bits_retain(bits);
55            roundtrip_extensions(extensions);
56        }
57    }
58}