Skip to content

Commit

Permalink
Add external type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
novacrazy committed Nov 16, 2024
1 parent 20f594d commit f6a6c2a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
24 changes: 17 additions & 7 deletions src/models/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Self>() >= 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() {
Expand All @@ -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
Expand All @@ -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),
Expand Down
12 changes: 8 additions & 4 deletions ts-bindgen/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
11 changes: 10 additions & 1 deletion ts-bindgen/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};

use std::borrow::Cow;

Expand All @@ -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<Cow<'static, str>>,
}

impl TypeRegistry {
Expand All @@ -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<Cow<'static, str>>) {
self.external.insert(name.into());
}

pub fn external(&self) -> &IndexSet<Cow<'static, str>> {
&self.external
}
}

use core::fmt::{Display, Error as FmtError, Write};
Expand Down

0 comments on commit f6a6c2a

Please sign in to comment.