hao 0.0.15

Read, edit, create and write .net assemblies
Documentation
use super::{well_known::TypeName, Entry, MemberRef, Method, ModuleRef};
use crate::dotnet::{il::MemberToken, md::streams::tables_stream::PInvokeMapFlags};

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum CustomAttributeCtor {
    Method(Entry<Method>),
    MemberRef(Entry<MemberRef>),
}

/// A custom attribute. The value is kept as the raw blob (II.23.3).
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct CustomAttribute {
    pub constructor: CustomAttributeCtor,
    pub value: Vec<u8>,
}

impl TryFrom<MemberToken> for CustomAttributeCtor {
    type Error = MemberToken;

    fn try_from(value: MemberToken) -> std::result::Result<Self, MemberToken> {
        match value {
            MemberToken::Method(m) => Ok(Self::Method(m)),
            MemberToken::MemberRef(m) => Ok(Self::MemberRef(m)),
            other => Err(other),
        }
    }
}

impl CustomAttribute {
    /// An attribute applied with its parameterless constructor, like `[Serializable]`.
    pub fn without_arguments(constructor: CustomAttributeCtor) -> Self {
        // The prolog and a zero count of named arguments.
        Self {
            constructor,
            value: vec![0x01, 0x00, 0x00, 0x00],
        }
    }

    /// Full name of the attribute type.
    pub fn type_name(&self) -> String {
        match &self.constructor {
            CustomAttributeCtor::Method(m) => m
                .get()
                .declaring_type
                .upgrade()
                .map(|t| t.get().full_name())
                .unwrap_or_default(),
            CustomAttributeCtor::MemberRef(m) => m
                .get()
                .parent
                .as_type()
                .map(|t| t.full_name())
                .unwrap_or_default(),
        }
    }

    /// Whether the attribute's type is `ty`, e.g.
    /// `ca.is_type(SystemReflectionType::DefaultMemberAttribute)`.
    pub fn is_type(&self, ty: impl TypeName) -> bool {
        match &self.constructor {
            CustomAttributeCtor::Method(m) => m
                .get()
                .declaring_type
                .upgrade()
                .is_some_and(|t| t.get().full_name_is(ty)),
            CustomAttributeCtor::MemberRef(m) => {
                m.get().parent.as_type().is_some_and(|t| t.full_name_is(ty))
            }
        }
    }
}

/// Declarative security (II.22.11). The permission set is the raw blob.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct DeclSecurity {
    pub action: u16,
    pub permission_set: Vec<u8>,
    pub custom_attributes: Vec<super::CustomAttribute>,
}

/// P/Invoke information for a method or field.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
pub struct ImplMap {
    pub flags: PInvokeMapFlags,
    pub import_name: String,
    pub scope: Entry<ModuleRef>,
}