rename TS::Generics to TS::WithoutGenerics

This commit is contained in:
Moritz Bischof
2024-02-23 17:03:53 +01:00
parent c24107c91d
commit f9bba02cc7
4 changed files with 35 additions and 21 deletions
+3 -3
View File
@@ -144,7 +144,7 @@ impl DerivedTS {
}
}
impl TS for #generics {
type Generics = #generics;
type WithoutGenerics = #generics;
fn name() -> String { stringify!(#generics).to_owned() }
fn transparent() -> bool { false }
}
@@ -263,9 +263,9 @@ fn generate_assoc_type(rust_ty: &Ident, generics: &Generics) -> TokenStream {
.collect::<Vec<_>>();
if generics_params.is_empty() {
quote! { type Generics = #rust_ty; }
quote! { type WithoutGenerics = #rust_ty; }
} else {
quote! { type Generics = #rust_ty<#(#generics_params),*>; }
quote! { type WithoutGenerics = #rust_ty<#(#generics_params),*>; }
}
}
+3 -3
View File
@@ -11,7 +11,7 @@ use super::{impl_primitives, TS};
macro_rules! impl_dummy {
($($t:ty),*) => {$(
impl TS for $t {
type Generics = $t;
type WithoutGenerics = $t;
fn name() -> String { String::new() }
fn inline() -> String { String::new() }
fn transparent() -> bool { false }
@@ -23,7 +23,7 @@ impl_primitives!(NaiveDateTime, NaiveDate, NaiveTime, Month, Weekday, Duration =
impl_dummy!(Utc, Local, FixedOffset);
impl<T: TimeZone + 'static> TS for DateTime<T> {
type Generics = Self;
type WithoutGenerics = Self;
fn ident() -> String {
"string".to_owned()
}
@@ -39,7 +39,7 @@ impl<T: TimeZone + 'static> TS for DateTime<T> {
}
impl<T: TimeZone + 'static> TS for Date<T> {
type Generics = Self;
type WithoutGenerics = Self;
fn ident() -> String {
"string".to_owned()
}
+1 -1
View File
@@ -152,7 +152,7 @@ pub mod __private {
pub(crate) fn export_type_to_string<T: TS + ?Sized + 'static>() -> Result<String, ExportError> {
let mut buffer = String::with_capacity(1024);
buffer.push_str(NOTE);
generate_imports::<T::Generics>(&mut buffer)?;
generate_imports::<T::WithoutGenerics>(&mut buffer)?;
generate_decl::<T>(&mut buffer);
Ok(buffer)
}
+28 -14
View File
@@ -275,12 +275,26 @@ pub mod typelist;
/// - `#[ts(skip)]`:
/// Skip this variant
pub trait TS {
type Generics: TS + ?Sized;
/// If this type does not have generic parameters, then `WithoutGenerics` should just be `Self`.
/// If the type does have generic parameters, then all generic parameters must be replaced with
/// a dummy type, e.g `ts_rs::Dummy` or `()`.
/// The only requirement for these dummy types is that `EXPORT_TO` must be `None`.
/// Example:
/// ```ignore
/// struct GenericType<A, B>(A, B);
/// impl<A, B> TS for GenericType<A, B> {
/// type WithoutGenerics = GenericType<ts_rs::Dummy, ts_rs::Dummy>;
/// // ...
/// }
/// ```
type WithoutGenerics: TS + ?Sized;
const EXPORT_TO: Option<&'static str> = None;
const DOCS: Option<&'static str> = None;
/// Identifier of this type, excluding generic parameters.
fn ident() -> String {
// by default, fall back to `TS::name()`.
let name = Self::name();
if name.contains('<') {
@@ -312,7 +326,7 @@ pub trait TS {
panic!("{} cannot be declared", Self::name());
}
/// Name of this type in TypeScript.
/// Name of this type in TypeScript, including generic parameters
fn name() -> String;
/// Formats this types definition in TypeScript, e.g `{ user_id: number }`.
@@ -436,7 +450,7 @@ impl Dependency {
macro_rules! impl_primitives {
($($($ty:ty),* => $l:literal),*) => { $($(
impl TS for $ty {
type Generics = Self;
type WithoutGenerics = Self;
fn name() -> String { $l.to_owned() }
fn inline() -> String { <Self as $crate::TS>::name() }
fn transparent() -> bool { false }
@@ -447,7 +461,7 @@ macro_rules! impl_primitives {
macro_rules! impl_tuples {
( impl $($i:ident),* ) => {
impl<$($i: TS),*> TS for ($($i,)*) {
type Generics = (Dummy, );
type WithoutGenerics = (Dummy, );
fn name() -> String {
format!("[{}]", [$($i::name()),*].join(", "))
}
@@ -474,7 +488,7 @@ macro_rules! impl_tuples {
macro_rules! impl_wrapper {
($($t:tt)*) => {
$($t)* {
type Generics = Self;
type WithoutGenerics = Self;
fn name() -> String { T::name() }
fn inline() -> String { T::inline() }
fn inline_flattened() -> String { T::inline_flattened() }
@@ -499,7 +513,7 @@ macro_rules! impl_wrapper {
macro_rules! impl_shadow {
(as $s:ty: $($impl:tt)*) => {
$($impl)* {
type Generics = <$s as TS>::Generics;
type WithoutGenerics = <$s as TS>::WithoutGenerics;
fn name() -> String { <$s>::name() }
fn inline() -> String { <$s>::inline() }
fn inline_flattened() -> String { <$s>::inline_flattened() }
@@ -515,7 +529,7 @@ macro_rules! impl_shadow {
}
impl<T: TS> TS for Option<T> {
type Generics = Self;
type WithoutGenerics = Self;
fn name() -> String {
format!("{} | null", T::name())
}
@@ -540,7 +554,7 @@ impl<T: TS> TS for Option<T> {
}
impl<T: TS, E: TS> TS for Result<T, E> {
type Generics = Result<Dummy, Dummy>;
type WithoutGenerics = Result<Dummy, Dummy>;
fn name() -> String {
format!("{{ Ok : {} }} | {{ Err : {} }}", T::name(), E::name())
}
@@ -569,7 +583,7 @@ impl<T: TS, E: TS> TS for Result<T, E> {
}
impl<T: TS> TS for Vec<T> {
type Generics = Vec<Dummy>;
type WithoutGenerics = Vec<Dummy>;
fn ident() -> String {
"Array".to_owned()
}
@@ -599,7 +613,7 @@ impl<T: TS> TS for Vec<T> {
// Arrays longer than this limit will be emitted as Array<T>
const ARRAY_TUPLE_LIMIT: usize = 64;
impl<T: TS, const N: usize> TS for [T; N] {
type Generics = [Dummy; N];
type WithoutGenerics = [Dummy; N];
fn name() -> String {
if N > ARRAY_TUPLE_LIMIT {
return Vec::<T>::name();
@@ -642,7 +656,7 @@ impl<T: TS, const N: usize> TS for [T; N] {
}
impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
type Generics = HashMap<Dummy, Dummy>;
type WithoutGenerics = HashMap<Dummy, Dummy>;
fn ident() -> String {
"Record".to_owned()
}
@@ -676,7 +690,7 @@ impl<K: TS, V: TS, H> TS for HashMap<K, V, H> {
}
impl<I: TS> TS for Range<I> {
type Generics = Range<Dummy>;
type WithoutGenerics = Range<Dummy>;
fn name() -> String {
format!("{{ start: {}, end: {}, }}", I::name(), I::name())
}
@@ -694,7 +708,7 @@ impl<I: TS> TS for Range<I> {
}
impl<I: TS> TS for RangeInclusive<I> {
type Generics = RangeInclusive<Dummy>;
type WithoutGenerics = RangeInclusive<Dummy>;
fn name() -> String {
format!("{{ start: {}, end: {}, }}", I::name(), I::name())
}
@@ -793,7 +807,7 @@ impl std::fmt::Display for Dummy {
}
impl TS for Dummy {
type Generics = Self;
type WithoutGenerics = Self;
fn name() -> String {
"Dummy".to_owned()
}