macro_rules! well_known_namespaces {
( $(
$ns:literal in $module:ident $( ( $( $ty:ident in $ty_module:ident ),+ $(,)? ) )?
=> $variant:ident($sub:ident)
),+ $(,)? ) => {
#[derive(Debug, Copy, Clone, PartialEq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet), repr(C))]
pub enum WellKnown {
$(
#[doc = concat!("A type in the `", $ns, "` namespace.")]
$variant($sub),
)+
}
impl WellKnown {
pub fn from_full_name(namespace: &str, name: &str) -> Option<Self> {
Some(match namespace {
$( $ns => Self::$variant($sub::from_type_name(name)?), )+
_ => return None,
})
}
pub fn metadata_full_name(&self) -> (&'static str, &'static str) {
match self {
$( Self::$variant(ty) => ($ns, ty.metadata_name()), )+
}
}
pub fn full_name(&self) -> (&'static str, &'static str) {
match self {
$( Self::$variant(ty) => ($ns, ty.name()), )+
}
}
pub fn namespace(&self) -> &'static str {
self.full_name().0
}
pub fn type_name(&self) -> &'static str {
self.full_name().1
}
pub fn module(&self) -> WellKnownModule {
match self {
$(
Self::$variant(ty) => match ty {
$( $( $sub::$ty => WellKnownModule::$ty_module, )+ )?
#[allow(unreachable_patterns)]
_ => WellKnownModule::$module,
},
)+
}
}
}
$(
impl $sub {
pub fn from_type_name(name: &str) -> Option<Self> {
name.parse().ok()
}
pub fn name(&self) -> &'static str {
strip_arity(self.into())
}
pub fn metadata_name(&self) -> &'static str {
self.into()
}
}
impl TypeName for $sub {
fn type_name(&self) -> (Cow<'_, str>, Cow<'_, str>) {
let (namespace, name) = WellKnown::$variant(*self).metadata_full_name();
(Cow::Borrowed(namespace), Cow::Borrowed(name))
}
}
)+
};
}
macro_rules! wk_module_token {
(Microsoft) => {
[0xb0, 0x3f, 0x5f, 0x7f, 0x11, 0xd5, 0x0a, 0x3a]
};
(Ecma) => {
[0xb7, 0x7a, 0x5c, 0x56, 0x19, 0x34, 0xe0, 0x89]
};
(NetStandard) => {
[0xcc, 0x7b, 0x13, 0xff, 0xcd, 0x2d, 0xdd, 0x51]
};
(CoreLib) => {
[0x7c, 0xec, 0x85, 0xd7, 0xbe, 0xa7, 0x79, 0x8e]
};
}
macro_rules! well_known_modules {
( $( $variant:ident => $name:literal : $token:ident ),+ $(,)? ) => {
#[allow(non_camel_case_types)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, EnumString, IntoStaticStr)]
#[cfg_attr(feature = "reflect", derive(facet::Facet), repr(u8))]
pub enum WellKnownModule {
$(
#[doc = concat!("`", $name, "`")]
$variant,
)+
}
impl WellKnownModule {
pub fn assembly_name(&self) -> &'static str {
match self {
$( Self::$variant => $name, )+
}
}
pub fn from_assembly_name(name: &str) -> Option<Self> {
match name {
$( $name => Some(Self::$variant), )+
_ => None,
}
}
pub fn public_key_token(&self) -> [u8; 8] {
match self {
$( Self::$variant => wk_module_token!($token), )+
}
}
}
};
}
macro_rules! wk_sig_type {
(void) => { WellKnownSigType::Void };
(bool) => { WellKnownSigType::Bool };
(char) => { WellKnownSigType::Char };
(i32) => { WellKnownSigType::I32 };
(i64) => { WellKnownSigType::I64 };
(f64) => { WellKnownSigType::F64 };
(nint) => { WellKnownSigType::NInt };
(string) => { WellKnownSigType::String };
(object) => { WellKnownSigType::Object };
({ $wk:expr }) => { WellKnownSigType::Class($wk) };
((valuetype { $wk:expr })) => { WellKnownSigType::ValueType($wk) };
([$($inner:tt)+]) => { WellKnownSigType::Array(&wk_sig_type!($($inner)+)) };
}
macro_rules! wk_is_instance {
(Static) => {
false
};
(Instance) => {
true
};
}
macro_rules! well_known_methods {
( $(
$group:ident($sub:ident) = $declaring:expr => {
$( $variant:ident = $kind:ident $name:literal ( $($param:tt),* $(,)? ) -> $ret:tt ; )+
}
)+ ) => {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet), repr(C))]
pub enum WellKnownMethod {
$(
#[doc = concat!("A method of `", stringify!($group), "`.")]
$group($sub),
)+
}
impl WellKnownMethod {
pub fn declaring(self) -> WellKnown {
match self { $( Self::$group(_) => $declaring, )+ }
}
pub fn method_name(self) -> &'static str {
match self { $( Self::$group(m) => m.method_name(), )+ }
}
pub fn is_instance(self) -> bool {
match self { $( Self::$group(m) => m.is_instance(), )+ }
}
pub fn params(self) -> &'static [WellKnownSigType] {
match self { $( Self::$group(m) => m.params(), )+ }
}
pub fn ret(self) -> WellKnownSigType {
match self { $( Self::$group(m) => m.ret(), )+ }
}
pub fn param_count(self) -> usize {
self.params().len()
}
}
$(
impl From<$sub> for WellKnownMethod {
fn from(method: $sub) -> Self {
WellKnownMethod::$group(method)
}
}
)+
$(
#[doc = concat!(
"Well-known methods of `", stringify!($group), "`, for [`WellKnownMethod::",
stringify!($group), "`]. Each variant's doc is its signature."
)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "reflect", derive(facet::Facet), repr(u8))]
pub enum $sub {
$(
#[doc = concat!(
stringify!($kind), " `", $name, "(", stringify!($($param),*), ") -> ",
stringify!($ret), "`"
)]
$variant,
)+
}
impl $sub {
fn method_name(self) -> &'static str {
match self { $( Self::$variant => $name, )+ }
}
fn is_instance(self) -> bool {
match self { $( Self::$variant => wk_is_instance!($kind), )+ }
}
fn params(self) -> &'static [WellKnownSigType] {
match self { $( Self::$variant => &[ $( wk_sig_type!($param) ),* ], )+ }
}
fn ret(self) -> WellKnownSigType {
match self { $( Self::$variant => wk_sig_type!($ret), )+ }
}
}
)+
};
}