mirror of
https://github.com/xazukx/ts-rs.git
synced 2026-11-19 22:40:25 +00:00
implement POC
This commit is contained in:
+35
-6
@@ -2,20 +2,33 @@ use proc_macro2::TokenStream;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::Type;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Dependencies(Vec<TokenStream>);
|
||||
pub struct Dependencies {
|
||||
v0: Vec<TokenStream>,
|
||||
v1: Vec<TokenStream>
|
||||
}
|
||||
|
||||
impl Default for Dependencies {
|
||||
fn default() -> Self {
|
||||
Dependencies {
|
||||
v0: Vec::new(),
|
||||
v1: vec![quote![()]]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Dependencies {
|
||||
/// Adds all dependencies from the given type
|
||||
pub fn append_from(&mut self, ty: &Type) {
|
||||
self.0
|
||||
self.v1.push(quote![.extend(<#ty as ts_rs::TS>::dependency_types())]);
|
||||
self.v0
|
||||
.push(quote!(dependencies.append(&mut <#ty as ts_rs::TS>::dependencies());));
|
||||
}
|
||||
|
||||
/// Adds the given type if it's *not* transparent.
|
||||
/// If it is, all it's child dependencies are added instead.
|
||||
pub fn push_or_append_from(&mut self, ty: &Type) {
|
||||
self.0.push(quote! {
|
||||
self.v1.push(quote![.push::<#ty>()]);
|
||||
self.v0.push(quote! {
|
||||
if <#ty as ts_rs::TS>::transparent() {
|
||||
dependencies.append(&mut <#ty as ts_rs::TS>::dependencies());
|
||||
} else {
|
||||
@@ -27,15 +40,31 @@ impl Dependencies {
|
||||
}
|
||||
|
||||
pub fn append(&mut self, other: Dependencies) {
|
||||
self.0.push(quote! {
|
||||
let other_v2 = other.to_tokens_v2();
|
||||
self.v1.push(quote![.extend(#other_v2)]);
|
||||
self.v0.push(quote! {
|
||||
dependencies.append(&mut #other);
|
||||
})
|
||||
}
|
||||
|
||||
pub fn to_tokens_v2(&self) -> impl ToTokens + '_ {
|
||||
struct ToTokensV2<'a>(&'a Dependencies);
|
||||
impl<'a> ToTokens for ToTokensV2<'a> {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let lines = &self.0.v1;
|
||||
tokens.extend(quote![{
|
||||
use ts_rs::typelist::TypeList;
|
||||
#(#lines)*
|
||||
}])
|
||||
}
|
||||
}
|
||||
ToTokensV2(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Dependencies {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let dependencies = &self.0;
|
||||
let dependencies = &self.v0;
|
||||
tokens.extend(quote! {
|
||||
{
|
||||
let mut dependencies = Vec::new();
|
||||
|
||||
@@ -81,6 +81,7 @@ impl DerivedTS {
|
||||
.unwrap_or_else(TokenStream::new);
|
||||
|
||||
let impl_start = generate_impl(&rust_ty, &generics);
|
||||
let dependencies_v2 = dependencies.to_tokens_v2();
|
||||
quote! {
|
||||
#impl_start {
|
||||
const EXPORT_TO: Option<&'static str> = Some(#export_to);
|
||||
@@ -95,12 +96,21 @@ impl DerivedTS {
|
||||
#inline
|
||||
}
|
||||
#inline_flattened
|
||||
|
||||
fn dependencies() -> Vec<ts_rs::Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
#dependencies
|
||||
}
|
||||
|
||||
fn dependency_types() -> impl ts_rs::typelist::TypeList
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
#dependencies_v2
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
+110
-26
@@ -172,6 +172,7 @@ pub use crate::export::ExportError;
|
||||
#[cfg(feature = "chrono-impl")]
|
||||
mod chrono;
|
||||
mod export;
|
||||
pub mod typelist;
|
||||
|
||||
/// A type which can be represented in TypeScript.
|
||||
/// Most of the time, you'd want to derive this trait instead of implementing it manually.
|
||||
@@ -289,11 +290,34 @@ pub trait TS {
|
||||
panic!("{} cannot be flattened", Self::name())
|
||||
}
|
||||
|
||||
/// Information about types this type depends on.
|
||||
/// This is used for resolving imports when exporting to a file.
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static { () }
|
||||
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static;
|
||||
where
|
||||
Self: 'static;
|
||||
|
||||
//fn dependencies() -> Vec<Dependency> where Self: 'static {
|
||||
// use crate::typelist::{TypeList, TypeVisitor};
|
||||
//
|
||||
// let mut deps: Vec<Dependency> = vec![];
|
||||
// struct Visit<'a>(&'a mut Vec<Dependency>);
|
||||
// impl<'a> TypeVisitor for Visit<'a> {
|
||||
// fn visit<T: TS + 'static + ?Sized>(&mut self) {
|
||||
// if let Some(dep) = Dependency::from_ty::<T>() {
|
||||
// self.0.push(dep);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// Self::dependency_types().for_each(&mut Visit(&mut deps));
|
||||
//
|
||||
// deps
|
||||
//}
|
||||
|
||||
fn _push_to(l: impl TypeList) -> impl TypeList where Self: 'static {
|
||||
l.push::<Self>()
|
||||
}
|
||||
|
||||
/// `true` if this is a transparent type, e.g tuples or a list.
|
||||
/// This is used for resolving imports when using the `export!` macro.
|
||||
@@ -384,14 +408,20 @@ macro_rules! impl_tuples {
|
||||
fn inline() -> String {
|
||||
format!("[{}]", [$($i::inline()),*].join(", "))
|
||||
}
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
()$(.push::<$i>())*
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[$( Dependency::from_ty::<$i>() ),*]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
fn transparent() -> bool { true }
|
||||
}
|
||||
@@ -414,6 +444,12 @@ macro_rules! impl_wrapper {
|
||||
}
|
||||
fn inline() -> String { T::inline() }
|
||||
fn inline_flattened() -> String { T::inline_flattened() }
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
T::dependency_types()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
@@ -433,7 +469,13 @@ macro_rules! impl_shadow {
|
||||
fn name_with_type_args(args: Vec<String>) -> String { <$s>::name_with_type_args(args) }
|
||||
fn inline() -> String { <$s>::inline() }
|
||||
fn inline_flattened() -> String { <$s>::inline_flattened() }
|
||||
fn dependencies() -> Vec<$crate::Dependency>
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
<$s>::dependency_types()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
@@ -463,11 +505,17 @@ impl<T: TS> TS for Option<T> {
|
||||
format!("{} | null", T::inline())
|
||||
}
|
||||
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
().push::<T>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
@@ -482,9 +530,15 @@ impl<T: TS, E: TS> TS for Result<T, E> {
|
||||
fn inline() -> String {
|
||||
format!("{{ Ok : {} }} | {{ Err : {} }}", T::inline(), E::inline())
|
||||
}
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
().push::<T>().push::<E>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>(), Dependency::from_ty::<E>()]
|
||||
.into_iter()
|
||||
@@ -505,13 +559,18 @@ impl<T: TS> TS for Vec<T> {
|
||||
format!("Array<{}>", T::inline())
|
||||
}
|
||||
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
().push::<T>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -557,9 +616,15 @@ impl<T: TS, const N: usize> TS for [T; N] {
|
||||
)
|
||||
}
|
||||
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
().push::<T>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
}
|
||||
@@ -588,9 +653,15 @@ impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
|
||||
format!("Record<{}, {}>", K::inline(), V::inline())
|
||||
}
|
||||
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
().push::<K>().push::<V>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<K>(), Dependency::from_ty::<V>()]
|
||||
.into_iter()
|
||||
@@ -618,9 +689,15 @@ impl<I: TS> TS for Range<I> {
|
||||
format!("{{ start: {}, end: {}, }}", &args[0], &args[0])
|
||||
}
|
||||
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
().push::<I>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<I>()].into_iter().flatten().collect()
|
||||
}
|
||||
@@ -645,9 +722,15 @@ impl<I: TS> TS for RangeInclusive<I> {
|
||||
format!("{{ start: {}, end: {}, }}", &args[0], &args[0])
|
||||
}
|
||||
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
().push::<I>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<I>()].into_iter().flatten().collect()
|
||||
}
|
||||
@@ -657,7 +740,7 @@ impl<I: TS> TS for RangeInclusive<I> {
|
||||
}
|
||||
}
|
||||
|
||||
impl_shadow!(as T: impl<'a, T: TS + ?Sized> TS for &T);
|
||||
impl_shadow!(as T: impl<T: TS + ?Sized> TS for &T);
|
||||
impl_shadow!(as Vec<T>: impl<T: TS, H> TS for HashSet<T, H>);
|
||||
impl_shadow!(as Vec<T>: impl<T: TS> TS for BTreeSet<T>);
|
||||
impl_shadow!(as HashMap<K, V>: impl<K: TS, V: TS> TS for BTreeMap<K, V>);
|
||||
@@ -727,3 +810,4 @@ impl_primitives! {
|
||||
}
|
||||
#[rustfmt::skip]
|
||||
pub(crate) use impl_primitives;
|
||||
use crate::typelist::TypeList;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
use std::any::TypeId;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::TS;
|
||||
|
||||
pub trait TypeVisitor: Sized {
|
||||
fn visit<T: TS + 'static + ?Sized>(&mut self);
|
||||
}
|
||||
|
||||
|
||||
pub trait TypeList: Copy + Clone {
|
||||
fn push<T: TS + 'static + ?Sized>(self) -> impl TypeList {
|
||||
(self, (PhantomData::<T>,))
|
||||
}
|
||||
fn extend(self, l: impl TypeList) -> impl TypeList {
|
||||
(self, l)
|
||||
}
|
||||
|
||||
fn contains<C: Sized + 'static>(self) -> bool;
|
||||
fn for_each(self, v: &mut impl TypeVisitor);
|
||||
}
|
||||
|
||||
impl TypeList for () {
|
||||
fn contains<C: Sized>(self) -> bool { false }
|
||||
fn for_each(self, _: &mut impl TypeVisitor) {}
|
||||
}
|
||||
|
||||
impl<T> TypeList for (PhantomData<T>,) where T: TS + 'static + ?Sized {
|
||||
fn contains<C: Sized + 'static>(self) -> bool {
|
||||
TypeId::of::<C>() == TypeId::of::<T>()
|
||||
}
|
||||
|
||||
fn for_each(self, v: &mut impl TypeVisitor) {
|
||||
v.visit::<T>();
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, B> TypeList for (A, B) where A: TypeList, B: TypeList {
|
||||
fn contains<C: Sized + 'static>(self) -> bool {
|
||||
self.0.contains::<C>() || self.1.contains::<C>()
|
||||
}
|
||||
|
||||
fn for_each(self, v: &mut impl TypeVisitor) {
|
||||
self.0.for_each(v);
|
||||
self.1.for_each(v);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
use std::collections::{BTreeSet, HashMap};
|
||||
use ts_rs::{Dependency, TS};
|
||||
use ts_rs::typelist::TypeList;
|
||||
use ts_rs::typelist::TypeVisitor;
|
||||
|
||||
fn get_deps<T: TS + 'static>() -> BTreeSet<Dependency> {
|
||||
use ts_rs::typelist::{TypeList, TypeVisitor};
|
||||
|
||||
let mut deps = BTreeSet::new();
|
||||
struct Visit<'a>(&'a mut BTreeSet<Dependency>);
|
||||
impl<'a> TypeVisitor for Visit<'a> {
|
||||
fn visit<T: TS + 'static + ?Sized>(&mut self) {
|
||||
if let Some(dep) = Dependency::from_ty::<T>() {
|
||||
self.0.insert(dep);
|
||||
}
|
||||
}
|
||||
}
|
||||
T::dependency_types().for_each(&mut Visit(&mut deps));
|
||||
|
||||
deps
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[test]
|
||||
fn test() {
|
||||
#[derive(TS)]
|
||||
struct A {}
|
||||
|
||||
#[derive(TS)]
|
||||
struct B {}
|
||||
|
||||
#[derive(TS)]
|
||||
struct X {
|
||||
x: i32,
|
||||
a: A,
|
||||
a2: A,
|
||||
map: HashMap<A, B>,
|
||||
}
|
||||
|
||||
println!("{:?}", X::dependencies().into_iter().collect::<BTreeSet<_>>());
|
||||
println!("{:?}", get_deps::<X>());
|
||||
}
|
||||
Reference in New Issue
Block a user