hao 0.0.19

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};

/// The constructor a [`CustomAttribute`] is applied with; its declaring type is
/// the attribute type.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[cfg_attr(feature = "reflect", repr(C))]
pub enum CustomAttributeCtor {
    /// A constructor of an attribute type defined in this module.
    Method(Entry<Method>),
    /// A constructor of an attribute type from elsewhere.
    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 {
    /// The attribute's constructor, which also identifies its type.
    pub constructor: CustomAttributeCtor,
    /// Constructor arguments and named fields/properties, encoded; see
    /// [`decode`](Self::decode) to read them.
    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 {
    /// The `SecurityAction`, e.g. 2 for `Demand`.
    pub action: u16,
    /// The encoded permission set.
    pub permission_set: Vec<u8>,
    /// Attributes applied to this row.
    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 {
    /// Character set, calling convention, `SetLastError` and name mangling options.
    pub flags: PInvokeMapFlags,
    /// The native function's name (`EntryPoint`).
    pub import_name: String,
    /// The native library it's imported from.
    pub scope: Entry<ModuleRef>,
}