windows-bindgen 0.101.0

Code generator for Windows metadata
Documentation
use super::*;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CppEnum {
    pub def: TypeDef,
}

impl Ord for CppEnum {
    fn cmp(&self, other: &Self) -> Ordering {
        self.def.name().cmp(other.def.name())
    }
}

impl PartialOrd for CppEnum {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl CppEnum {
    pub fn type_name(&self) -> TypeName {
        self.def.type_name()
    }

    pub fn write_name(&self, config: &Config) -> TokenStream {
        self.type_name().write(config, &[])
    }

    pub fn write(&self, config: &Config) -> TokenStream {
        if let Some(ty) = config.reader.legacy_core_type(self.def) {
            return config.write_legacy_core_type(&ty);
        }
        let tn = self.def.type_name();

        // The legacy `windows` crate re-exports the `windows-core` status type, which provides the
        // same layout and methods as the 0.62 projection.
        if config.reader.legacy()
            && !config.bindgen.style.is_sys()
            && tn == TypeName("Windows.Win32.Foundation", "WIN32_ERROR")
        {
            let core = config.write_core();
            return quote! { pub use #core WIN32_ERROR; };
        }

        let is_scoped = self.def.has_attribute("ScopedEnumAttribute");

        // Unscoped C enums are bare integer aliases unless legacy metadata calls for newtypes.
        if !config.enum_is_newtype(self.def) {
            let cfg = write_simple_cfg(self, config);
            return config.write_cpp_handle(&Type::CppEnum(self.clone()), self.def, &cfg);
        }

        let name = to_ident(tn.name());
        let underlying_type = self
            .def
            .underlying_type_ext(config.reader)
            .write_name(config);

        let mut derive = DeriveWriter::new(config, tn);
        derive.extend(["Copy", "Clone"]);

        if config.bindgen.style.derive_std_traits() {
            derive.extend(["Default", "Debug", "PartialEq", "Eq"]);
        }

        // Unscoped legacy variants remain namespace-level constants.
        let fields = if is_scoped {
            write_enum_constants(self.def, config)
        } else {
            vec![]
        };
        let fields = if fields.is_empty() {
            quote! {}
        } else {
            quote! { impl #name { #(#fields)* } }
        };

        let flags = if config.bindgen.style.is_sys() || !self.def.has_attribute("FlagsAttribute") {
            quote! {}
        } else {
            write_enum_flags(&name)
        };

        quote! {
            #[repr(transparent)]
            #derive
            pub struct #name(pub #underlying_type);
            #fields
            #flags
        }
    }
}

impl Dependencies for CppEnum {
    fn combine(&self, _dependencies: &mut TypeMap, _reader: &Reader) {}
}