Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate somewhat cleaner TypeLists #292

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 58 additions & 23 deletions macros/src/deps.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,89 @@
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use std::collections::HashSet;
use std::rc::Rc;
use syn::{Path, Type};

pub struct Dependencies {
dependencies: Vec<TokenStream>,
crate_rename: Path,
pub types: Vec<Type>,

// Types which are used in the Rust definition. This is tracked to generate a good `where`
// bound later
used_types: HashSet<Rc<Type>>,
// Types on which the type has a dependency, making it show up in the resulting `TypeList`.
direct_deps: HashSet<Rc<Type>>,
// Types whose dependencies we depend on. The `dependency_types()` of every type in this set
// is appended to the resulting `TypeList`
transitive_deps: HashSet<Rc<Type>>,
// Types whose generics we depend on. The `generics()` of every type in this set is appended
// to the resulting `TypeList`
generics_deps: HashSet<Rc<Type>>,
}

impl Dependencies {
pub fn new(crate_rename: Path) -> Self {
Self {
dependencies: Vec::default(),
crate_rename,
types: Vec::default(),
used_types: Default::default(),
direct_deps: Default::default(),
transitive_deps: Default::default(),
generics_deps: Default::default(),
}
}

pub fn used_types(&self) -> impl Iterator<Item = &Type> {
self.used_types.iter().map(Rc::as_ref)
}

/// Adds all dependencies from the given type
pub fn append_from(&mut self, ty: &Type) {
let crate_rename = &self.crate_rename;
self.dependencies
.push(quote![.extend(<#ty as #crate_rename::TS>::dependency_types())]);
self.types.push(ty.clone());
let ty = Rc::new(ty.clone());
self.used_types.insert(ty.clone());

self.transitive_deps.insert(ty);
}

/// Adds the given type.
pub fn push(&mut self, ty: &Type) {
let crate_rename = &self.crate_rename;
self.dependencies.push(quote![.push::<#ty>()]);
self.dependencies.push(quote![
.extend(<#ty as #crate_rename::TS>::generics())
]);
self.types.push(ty.clone());
let ty = Rc::new(ty.clone());
self.used_types.insert(ty.clone());

self.direct_deps.insert(ty.clone());
self.generics_deps.insert(ty.clone());
}

pub fn append(&mut self, mut other: Dependencies) {
if !other.dependencies.is_empty() {
self.dependencies.push(quote![.extend(#other)]);
}
self.types.append(&mut other.types);
pub fn append(&mut self, other: Dependencies) {
self.used_types.extend(other.used_types.into_iter());
self.direct_deps.extend(other.direct_deps.into_iter());
self.transitive_deps
.extend(other.transitive_deps.into_iter());
self.generics_deps.extend(other.generics_deps.into_iter());
}
}

impl ToTokens for Dependencies {
fn to_tokens(&self, tokens: &mut TokenStream) {
let crate_rename = &self.crate_rename;
let lines = &self.dependencies;
let rename = &self.crate_rename;

let direct = self.direct_deps.iter().map(|ty| {
quote! {
.push::<#ty>()
}
});
let transitive = self.transitive_deps.iter().map(|ty| {
quote! {
.extend(<#ty as #rename::TS>::dependency_types())
}
});
let generics = self.generics_deps.iter().map(|ty| {
quote! {
.extend(<#ty as #rename::TS>::generics())
}
});

tokens.extend(quote![{
use #crate_rename::typelist::TypeList;
()#(#lines)*
use #rename::typelist::TypeList;
() #(#direct)* #(#transitive)* #(#generics)*
}])
}
}
2 changes: 1 addition & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ fn generate_where_clause(
let is_type_param = |id: &Ident| generics.type_params().any(|p| &p.ident == id);

let mut used_types = HashSet::new();
for ty in &dependencies.types {
for ty in dependencies.used_types() {
used_type_params(&mut used_types, ty, is_type_param);
}
used_types.into_iter()
Expand Down
215 changes: 51 additions & 164 deletions ts-rs/tests/recursion_limit.rs
Original file line number Diff line number Diff line change
@@ -1,170 +1,57 @@
#[rustfmt::skip]
#[derive(Debug, ts_rs::TS)]
#[ts(export)]
pub enum Iso4217CurrencyCode {
AED,
AFN,
ALL,
AMD,
ANG,
AO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IMP, INR, IQD, IRR, ISK, JEP, JMD, JOD, JPY, KES, KGS, KHR, KMF, KPW,
KRW, KWD, KYD, KZT, LAK, LBP, LKR, LRD, LSL, LYD, MAD, MDL, MGA, MKD, MMK, MNT, MOP, MRU, MUR,
MVR, MWK, MXN, MYR, MZN, NAD, NGN, NIO, NOK, NPR, NZD, OMR, PAB, PEN, PGK, PHP, PKR, PLN, PYG,
QAR, RON, RSD, RUB, RWF, SAR, SBD, SCR, SDG, SEK, SGD, SHP, SLL, SOS, SPL, SRD, STN, SVC, SYP,
SZL, THB, TJS, TMT, TND, TOP, TRY, TTD, TVD, TWD, TZS, UAH, UGX, USD, UYU, UZS, VEF, VND, VUV,
WST, XAF, XCD, XDR, XOF, XPF, YER, ZAR, ZMW, ZWD,
}

fn main() {
println!("{:?}", Iso4217CurrencyCode::USD)
#[rustfmt::skip]
#[derive(Debug, ts_rs::TS)]
#[ts(export)]
pub enum VeryBigEnum {
V001(String), V002(String), V003(String), V004(String), V005(String), V006(String), V007(String),
V008(String), V009(String), V010(String), V011(String), V012(String), V013(String), V014(String),
V015(String), V016(String), V017(String), V018(String), V019(String), V020(String), V021(String),
V022(String), V023(String), V024(String), V025(String), V026(String), V027(String), V028(String),
V029(String), V030(String), V031(String), V032(String), V033(String), V034(String), V035(String),
V036(String), V037(String), V038(String), V039(String), V040(String), V041(String), V042(String),
V043(String), V044(String), V045(String), V046(String), V047(String), V048(String), V049(String),
V050(String), V051(String), V052(String), V053(String), V054(String), V055(String), V056(String),
V057(String), V058(String), V059(String), V060(String), V061(String), V062(String), V063(String),
V064(String), V065(String), V066(String), V067(String), V068(String), V069(String), V070(String),
V071(String), V072(String), V073(String), V074(String), V075(String), V076(String), V077(String),
V078(String), V079(String), V080(String), V081(String), V082(String), V083(String), V084(String),
V085(String), V086(String), V087(String), V088(String), V089(String), V090(String), V091(String),
V092(String), V093(String), V094(String), V095(String), V096(String), V097(String), V098(String),
V099(String), V100(String), V101(String), V102(String), V103(String), V104(String), V105(String),
V106(String), V107(String), V108(String), V109(String), V110(String), V111(String), V112(String),
V113(String), V114(String), V115(String), V116(String), V117(String), V118(String), V119(String),
V120(String), V121(String), V122(String), V123(String), V124(String), V125(String), V126(String),
V127(String), V128(String), V129(String), V130(String), V131(String), V132(String), V133(String),
V134(String), V135(String), V136(String), V137(String), V138(String), V139(String), V140(String),
V141(String), V142(String), V143(String), V144(String), V145(String), V146(String), V147(String),
V148(String), V149(String), V150(String), V151(String), V152(String), V153(String), V154(String),
V155(String), V156(String), V157(String), V158(String), V159(String), V160(String), V161(String),
V162(String), V163(String), V164(String), V165(String), V166(String), V167(String), V168(String),
V169(String), V170(String), V171(String), V172(String), V173(String), V174(String), V175(String),
V176(String), V177(String), V178(String), V179(String), V180(String), V181(String), V182(String),
V183(String), V184(String), V185(String), V186(String), V187(String), V188(String), V189(String),
V190(String), V191(String), V192(String), V193(String), V194(String), V195(String), V196(String),
V197(String), V198(String), V199(String), V200(String), V201(String), V202(String), V203(String),
V204(String), V205(String), V206(String), V207(String), V208(String), V209(String), V210(String),
V211(String), V212(String), V213(String), V214(String), V215(String), V216(String), V217(String),
V218(String), V219(String), V220(String), V221(String), V222(String), V223(String), V224(String),
V225(String), V226(String), V227(String), V228(String), V229(String), V230(String), V231(String),
V232(String), V233(String), V234(String), V235(String), V236(String), V237(String), V238(String),
V239(String), V240(String), V241(String), V242(String), V243(String), V244(String), V245(String),
V246(String), V247(String), V248(String), V249(String), V250(String), V251(String), V252(String),
V253(String), V254(String), V255(String), V256(String),
}