mirror of
https://github.com/xazukx/ts-rs.git
synced 2026-11-15 22:00:26 +00:00
remove old dependency implementation, fix warnings
This commit is contained in:
+10
-53
@@ -2,75 +2,32 @@ use proc_macro2::TokenStream;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::Type;
|
||||
|
||||
pub struct Dependencies {
|
||||
v0: Vec<TokenStream>,
|
||||
v1: Vec<TokenStream>
|
||||
}
|
||||
|
||||
impl Default for Dependencies {
|
||||
fn default() -> Self {
|
||||
Dependencies {
|
||||
v0: Vec::new(),
|
||||
v1: vec![quote![()]]
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Default)]
|
||||
pub struct Dependencies(Vec<TokenStream>);
|
||||
|
||||
impl Dependencies {
|
||||
/// Adds all dependencies from the given type
|
||||
pub fn append_from(&mut self, ty: &Type) {
|
||||
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());));
|
||||
self.0.push(quote![.extend(<#ty as ts_rs::TS>::dependency_types())]);
|
||||
}
|
||||
|
||||
/// 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.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 {
|
||||
if let Some(dep) = ts_rs::Dependency::from_ty::<#ty>() {
|
||||
dependencies.push(dep);
|
||||
}
|
||||
}
|
||||
});
|
||||
self.0.push(quote![.push::<#ty>()]);
|
||||
}
|
||||
|
||||
pub fn append(&mut self, other: Dependencies) {
|
||||
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)
|
||||
self.0.push(quote![.extend(#other)]);
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for Dependencies {
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let dependencies = &self.v0;
|
||||
tokens.extend(quote! {
|
||||
{
|
||||
let mut dependencies = Vec::new();
|
||||
#( #dependencies )*
|
||||
dependencies
|
||||
}
|
||||
})
|
||||
let lines = &self.0;
|
||||
tokens.extend(quote![{
|
||||
use ts_rs::typelist::TypeList;
|
||||
()#(#lines)*
|
||||
}])
|
||||
}
|
||||
}
|
||||
|
||||
+2
-9
@@ -81,7 +81,6 @@ 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);
|
||||
@@ -97,18 +96,12 @@ impl DerivedTS {
|
||||
}
|
||||
#inline_flattened
|
||||
|
||||
fn dependencies() -> Vec<ts_rs::Dependency>
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
#dependencies
|
||||
}
|
||||
|
||||
#[allow(clippy::unused_unit)]
|
||||
fn dependency_types() -> impl ts_rs::typelist::TypeList
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
#dependencies_v2
|
||||
#dependencies
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
|
||||
@@ -7,14 +7,12 @@ use chrono::{
|
||||
};
|
||||
|
||||
use super::{impl_primitives, TS};
|
||||
use crate::Dependency;
|
||||
|
||||
macro_rules! impl_dummy {
|
||||
($($t:ty),*) => {$(
|
||||
impl TS for $t {
|
||||
fn name() -> String { String::new() }
|
||||
fn inline() -> String { String::new() }
|
||||
fn dependencies() -> Vec<Dependency> { vec![] }
|
||||
fn transparent() -> bool { false }
|
||||
}
|
||||
)*};
|
||||
@@ -33,9 +31,6 @@ impl<T: TimeZone + 'static> TS for DateTime<T> {
|
||||
fn inline() -> String {
|
||||
"string".to_owned()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency> {
|
||||
vec![]
|
||||
}
|
||||
fn transparent() -> bool {
|
||||
false
|
||||
}
|
||||
@@ -51,9 +46,6 @@ impl<T: TimeZone + 'static> TS for Date<T> {
|
||||
fn inline() -> String {
|
||||
"string".to_owned()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency> {
|
||||
vec![]
|
||||
}
|
||||
fn transparent() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
+18
-95
@@ -165,6 +165,8 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::typelist::TypeList;
|
||||
|
||||
pub use ts_rs_macros::TS;
|
||||
|
||||
pub use crate::export::ExportError;
|
||||
@@ -292,34 +294,26 @@ pub trait TS {
|
||||
|
||||
fn dependency_types() -> impl TypeList
|
||||
where
|
||||
Self: 'static { () }
|
||||
Self: 'static {}
|
||||
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static;
|
||||
fn dependencies() -> Vec<Dependency> where Self: 'static {
|
||||
use crate::typelist::TypeVisitor;
|
||||
|
||||
//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
|
||||
//}
|
||||
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));
|
||||
|
||||
fn _push_to(l: impl TypeList) -> impl TypeList where Self: 'static {
|
||||
l.push::<Self>()
|
||||
deps
|
||||
}
|
||||
|
||||
/// `true` if this is a transparent type, e.g tuples or a list.
|
||||
/// `true` if this is a transparent type, e.g tuples or a list.
|
||||
/// This is used for resolving imports when using the `export!` macro.
|
||||
fn transparent() -> bool;
|
||||
|
||||
@@ -393,7 +387,6 @@ macro_rules! impl_primitives {
|
||||
$l.to_owned()
|
||||
}
|
||||
fn inline() -> String { $l.to_owned() }
|
||||
fn dependencies() -> Vec<Dependency> { vec![] }
|
||||
fn transparent() -> bool { false }
|
||||
}
|
||||
)*)* };
|
||||
@@ -414,15 +407,6 @@ macro_rules! impl_tuples {
|
||||
{
|
||||
()$(.push::<$i>())*
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[$( Dependency::from_ty::<$i>() ),*]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
fn transparent() -> bool { true }
|
||||
}
|
||||
};
|
||||
@@ -450,12 +434,6 @@ macro_rules! impl_wrapper {
|
||||
{
|
||||
T::dependency_types()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
T::dependencies()
|
||||
}
|
||||
fn transparent() -> bool { T::transparent() }
|
||||
}
|
||||
};
|
||||
@@ -475,12 +453,6 @@ macro_rules! impl_shadow {
|
||||
{
|
||||
<$s>::dependency_types()
|
||||
}
|
||||
fn dependencies() -> Vec<$crate::Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
<$s>::dependencies()
|
||||
}
|
||||
fn transparent() -> bool { <$s>::transparent() }
|
||||
}
|
||||
};
|
||||
@@ -511,12 +483,6 @@ impl<T: TS> TS for Option<T> {
|
||||
{
|
||||
().push::<T>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
@@ -536,15 +502,6 @@ impl<T: TS, E: TS> TS for Result<T, E> {
|
||||
{
|
||||
().push::<T>().push::<E>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>(), Dependency::from_ty::<E>()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -565,12 +522,6 @@ impl<T: TS> TS for Vec<T> {
|
||||
{
|
||||
().push::<T>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
}
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
}
|
||||
@@ -622,12 +573,6 @@ impl<T: TS, const N: usize> TS for [T; N] {
|
||||
{
|
||||
().push::<T>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<T>()].into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
@@ -659,15 +604,6 @@ impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
|
||||
{
|
||||
().push::<K>().push::<V>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<K>(), Dependency::from_ty::<V>()]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
@@ -695,12 +631,6 @@ impl<I: TS> TS for Range<I> {
|
||||
{
|
||||
().push::<I>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<I>()].into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
@@ -728,12 +658,6 @@ impl<I: TS> TS for RangeInclusive<I> {
|
||||
{
|
||||
().push::<I>()
|
||||
}
|
||||
fn dependencies() -> Vec<Dependency>
|
||||
where
|
||||
Self: 'static
|
||||
{
|
||||
[Dependency::from_ty::<I>()].into_iter().flatten().collect()
|
||||
}
|
||||
|
||||
fn transparent() -> bool {
|
||||
true
|
||||
@@ -809,5 +733,4 @@ impl_primitives! {
|
||||
() => "null"
|
||||
}
|
||||
#[rustfmt::skip]
|
||||
pub(crate) use impl_primitives;
|
||||
use crate::typelist::TypeList;
|
||||
pub(crate) use impl_primitives;
|
||||
@@ -171,6 +171,7 @@ fn inline() {
|
||||
|
||||
#[test]
|
||||
#[ignore = "We haven't figured out how to inline generics with bounds yet"]
|
||||
#[allow(unreachable_code)]
|
||||
fn inline_with_bounds() {
|
||||
todo!("FIX ME: https://github.com/Aleph-Alpha/ts-rs/issues/214");
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::BTreeSet;
|
||||
use std::ops::{Range, RangeInclusive};
|
||||
|
||||
use ts_rs::{Dependency, TS};
|
||||
@@ -22,10 +23,9 @@ fn range() {
|
||||
"type RangeTest = { a: { start: number, end: number, }, b: { start: string, end: string, }, c: { start: { start: number, end: number, }, end: { start: number, end: number, }, }, d: { start: number, end: number, }, e: { start: Inner, end: Inner, }, }"
|
||||
);
|
||||
assert_eq!(
|
||||
RangeTest::dependencies(),
|
||||
RangeTest::dependencies().into_iter().collect::<BTreeSet<_>>().into_iter().collect::<Vec<_>>(),
|
||||
vec![
|
||||
Dependency::from_ty::<Inner>().unwrap(),
|
||||
Dependency::from_ty::<Inner>().unwrap()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
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