From f6a6c2a9f2fef4a2773f0a975ce351b715282a6c Mon Sep 17 00:00:00 2001 From: novacrazy Date: Fri, 15 Nov 2024 21:03:32 -0600 Subject: [PATCH] Add external type definitions --- src/models/util/macros.rs | 24 +++++++++++++++++------- ts-bindgen/src/impls/mod.rs | 12 ++++++++---- ts-bindgen/src/registry.rs | 11 ++++++++++- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/models/util/macros.rs b/src/models/util/macros.rs index 17a6577b..2426c0df 100644 --- a/src/models/util/macros.rs +++ b/src/models/util/macros.rs @@ -268,14 +268,18 @@ macro_rules! impl_schema_for_bitflags { // values into bit positions, (1 << 20) becomes 20, // at the cost of excluding combinations of flags if size_of::() >= 16 { - let name = concat!(stringify!($name), "Bit"); - let ty = TypeScriptType::Named(name); + let bits_name = concat!(stringify!($name), "Bit"); - if registry.contains(name) { - return ty; + if registry.contains(bits_name) { + return TypeScriptType::Named(stringify!($name)); } - eprintln!("Note: Generating TypeScript for {} as bit positions", stringify!($name)); + registry.add_external(stringify!($name)); + + eprintln!( + "Note: Generating TypeScript for {} as bit positions, relying on external type for usage", + stringify!($name) + ); let mut members = Vec::new(); for (name, value) in Self::all().iter_names() { @@ -290,12 +294,12 @@ macro_rules! impl_schema_for_bitflags { } registry.insert( - name, + bits_name, TypeScriptType::ConstEnum(members), concat!("Bit positions for ", stringify!($name)), ); - return ty; + return TypeScriptType::Named(stringify!($name)); } // regular enum @@ -315,6 +319,12 @@ macro_rules! impl_schema_for_bitflags { )); } + members.push(( + "ALL".into(), + Some(Discriminator::BinaryHex(Self::all().bits() as _)), + concat!("All bitflags of ", stringify!($name)).into(), + )); + registry.insert( name, TypeScriptType::ConstEnum(members), diff --git a/ts-bindgen/src/impls/mod.rs b/ts-bindgen/src/impls/mod.rs index 1133cf14..4a7d4ef1 100644 --- a/ts-bindgen/src/impls/mod.rs +++ b/ts-bindgen/src/impls/mod.rs @@ -5,14 +5,18 @@ mod core_impl; mod std_impl; impl TypeScriptDef for snowflake::Snowflake { - fn register(_registry: &mut TypeRegistry) -> TypeScriptType { - TypeScriptType::Named("Snowflake") // defined externally + fn register(registry: &mut TypeRegistry) -> TypeScriptType { + // defined externally + registry.add_external("Snowflake"); + TypeScriptType::Named("Snowflake") } } impl TypeScriptDef for timestamp::Timestamp { - fn register(_registry: &mut TypeRegistry) -> TypeScriptType { - TypeScriptType::Named("Timestamp") // defined externally + fn register(registry: &mut TypeRegistry) -> TypeScriptType { + // defined externally + registry.add_external("Timestamp"); + TypeScriptType::Named("Timestamp") } } diff --git a/ts-bindgen/src/registry.rs b/ts-bindgen/src/registry.rs index 9add83d1..89153bb1 100644 --- a/ts-bindgen/src/registry.rs +++ b/ts-bindgen/src/registry.rs @@ -1,4 +1,4 @@ -use indexmap::IndexMap; +use indexmap::{IndexMap, IndexSet}; use std::borrow::Cow; @@ -8,6 +8,7 @@ use crate::TypeScriptType; pub struct TypeRegistry { // use IndexMap to preserve the insertion order types: IndexMap<&'static str, (TypeScriptType, Cow<'static, str>)>, + external: IndexSet>, } impl TypeRegistry { @@ -24,6 +25,14 @@ impl TypeRegistry { pub fn contains(&self, name: &'static str) -> bool { self.types.contains_key(name) } + + pub fn add_external(&mut self, name: impl Into>) { + self.external.insert(name.into()); + } + + pub fn external(&self) -> &IndexSet> { + &self.external + } } use core::fmt::{Display, Error as FmtError, Write};