Skip to content

Commit

Permalink
Merge branch 'main' into cli
Browse files Browse the repository at this point in the history
  • Loading branch information
escritorio-gustavo authored May 20, 2024
2 parents 89430eb + badbac0 commit 147ea87
Show file tree
Hide file tree
Showing 22 changed files with 245 additions and 230 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,12 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
run: |
cargo test --all-features
shopt -s globstar
tsc ts-rs/bindings/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf ts-rs/bindings
tsc bindings/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf bindings
test-export-env:
name: Test ts-rs with TS_RS_EXPORT_DIR
Expand All @@ -118,11 +119,12 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
run: |
TS_RS_EXPORT_DIR=output cargo test --no-default-features --features ts-rs/export
shopt -s globstar
tsc ts-rs/output/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf ts-rs/output
tsc output/**/*.ts --noEmit --noUnusedLocals --strict
rm -rf output
test-no-features:
name: Test ts-rs with --no-default-features
Expand All @@ -136,8 +138,9 @@ jobs:
toolchain: stable
- uses: Swatinem/rust-cache@v2
- name: Test
working-directory: ts-rs
run: |
cargo test --no-default-features --features ts-rs/export
shopt -s globstar
tsc ts-rs/bindings/**/*.ts --noEmit --noUnusedLocals
rm -rf ts-rs/bindings
tsc bindings/**/*.ts --noEmit --noUnusedLocals
rm -rf bindings
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- `#[serde(with = "...")]` requires the use of `#[ts(as = "...")]` or `#[ts(type = "...")]` ([#280](https://github.com/Aleph-Alpha/ts-rs/pull/280))
- Fix incompatibility with serde for `snake_case`, `kebab-case` and `SCREAMING_SNAKE_CASE` ([#298](https://github.com/Aleph-Alpha/ts-rs/pull/298))
- `#[ts(rename_all = "...")]` no longer accepts variations in the string's casing, dashes and underscores to make behavior consistent with serde ([#298](https://github.com/Aleph-Alpha/ts-rs/pull/298))
- Remove `TypeList`, and replace `TS::dependency_types`/`TS::generics` with `TS::visit_dependencies`/`TS::visit_generics`.
This finally resolves "overflow evaluating the requirement", "reached the recursion limit" errors.
Also, compile times should benefit. This is a technically breaking change for those interacting with the `TS` trait
directly. For those just using `#[derive(TS)]` and `#[ts(...)]`, nothing changes!

### Features

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ Feel free to open an issue, discuss using GitHub discussions or open a PR.
[See CONTRIBUTING.md](https://github.com/Aleph-Alpha/ts-rs/blob/main/CONTRIBUTING.md)

### MSRV
The Minimum Supported Rust Version for this crate is 1.75.0
The Minimum Supported Rust Version for this crate is 1.63.0

License: MIT
4 changes: 1 addition & 3 deletions macros/src/attr/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl EnumAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;

#[cfg(feature = "serde-compat")]
{
if cfg!(feature = "serde-compat") {
let serde_attr = crate::utils::parse_serde_attrs::<EnumAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -223,7 +222,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<EnumAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
13 changes: 6 additions & 7 deletions macros/src/attr/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub struct FieldAttr {
pub flatten: bool,
pub docs: String,

#[cfg(feature = "serde-compat")]
pub using_serde_with: bool,
}

Expand All @@ -35,8 +34,7 @@ impl FieldAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;

#[cfg(feature = "serde-compat")]
if !result.skip {
if cfg!(feature = "serde-compat") && !result.skip {
let serde_attr = crate::utils::parse_serde_attrs::<FieldAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -71,7 +69,7 @@ impl Attr for FieldAttr {
nullable: self.optional.nullable || other.optional.nullable,
},
flatten: self.flatten || other.flatten,
#[cfg(feature = "serde-compat")]

using_serde_with: self.using_serde_with || other.using_serde_with,

// We can't emit TSDoc for a flattened field
Expand All @@ -86,8 +84,10 @@ impl Attr for FieldAttr {
}

fn assert_validity(&self, field: &Self::Item) -> Result<()> {
#[cfg(feature = "serde-compat")]
if self.using_serde_with && !(self.type_as.is_some() || self.type_override.is_some()) {
if cfg!(feature = "serde-compat")
&& self.using_serde_with
&& !(self.type_as.is_some() || self.type_override.is_some())
{
syn_err_spanned!(
field;
r#"using `#[serde(with = "...")]` requires the use of `#[ts(as = "...")]` or `#[ts(type = "...")]`"#
Expand Down Expand Up @@ -196,7 +196,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<FieldAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
2 changes: 0 additions & 2 deletions macros/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ pub(super) trait ContainerAttr: Attr {
fn crate_rename(&self) -> Path;
}

#[cfg(feature = "serde-compat")]
#[derive(Default)]
pub(super) struct Serde<T>(pub T)
where
T: Attr;

#[cfg(feature = "serde-compat")]
impl<T> Serde<T>
where
T: Attr,
Expand Down
4 changes: 1 addition & 3 deletions macros/src/attr/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl StructAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;

#[cfg(feature = "serde-compat")]
{
if cfg!(feature = "serde-compat") {
let serde_attr = crate::utils::parse_serde_attrs::<StructAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -145,7 +144,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<StructAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
4 changes: 1 addition & 3 deletions macros/src/attr/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub struct VariantAttr {
impl VariantAttr {
pub fn from_attrs(attrs: &[Attribute]) -> Result<Self> {
let mut result = parse_attrs::<Self>(attrs)?;
#[cfg(feature = "serde-compat")]
if !result.skip {
if cfg!(feature = "serde-compat") && !result.skip {
let serde_attr = crate::utils::parse_serde_attrs::<VariantAttr>(attrs);
result = result.merge(serde_attr.0);
}
Expand Down Expand Up @@ -62,7 +61,6 @@ impl_parse! {
}
}

#[cfg(feature = "serde-compat")]
impl_parse! {
Serde<VariantAttr>(input, out) {
"rename" => out.0.rename = Some(parse_assign_str(input)?),
Expand Down
14 changes: 6 additions & 8 deletions macros/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,24 @@ impl Dependencies {

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

tokens.extend(quote![{
use #crate_rename::typelist::TypeList;
()#(#lines)*
}]);
tokens.extend(quote![
#(#lines;)*
]);
}
}

impl ToTokens for Dependency {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.extend(match self {
Dependency::Transitive { crate_rename, ty } => {
quote![.extend(<#ty as #crate_rename::TS>::dependency_types())]
quote![<#ty as #crate_rename::TS>::visit_dependencies(v)]
}
Dependency::Generics { crate_rename, ty } => {
quote![.extend(<#ty as #crate_rename::TS>::generics())]
quote![<#ty as #crate_rename::TS>::visit_generics(v)]
}
Dependency::Type(ty) => quote![.push::<#ty>()],
Dependency::Type(ty) => quote![v.visit::<#ty>()],
});
}
}
16 changes: 9 additions & 7 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ impl DerivedTS {
#generics_fn
#output_path_fn

#[allow(clippy::unused_unit)]
fn dependency_types() -> impl #crate_rename::typelist::TypeList
fn visit_dependencies(v: &mut impl #crate_rename::TypeVisitor)
where
Self: 'static,
{
Expand Down Expand Up @@ -194,15 +193,18 @@ impl DerivedTS {
let generics = generics
.type_params()
.filter(|ty| !self.concrete.contains_key(&ty.ident))
.map(|TypeParam { ident, .. }| quote![.push::<#ident>().extend(<#ident as #crate_rename::TS>::generics())]);
.map(|TypeParam { ident, .. }| {
quote![
v.visit::<#ident>();
<#ident as #crate_rename::TS>::visit_generics(v);
]
});
quote! {
#[allow(clippy::unused_unit)]
fn generics() -> impl #crate_rename::typelist::TypeList
fn visit_generics(v: &mut impl #crate_rename::TypeVisitor)
where
Self: 'static,
{
use #crate_rename::typelist::TypeList;
()#(#generics)*
#(#generics)*
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn raw_name_to_ts_field(value: String) -> String {
}

/// Parse all `#[ts(..)]` attributes from the given slice.
pub fn parse_attrs<'a, A>(attrs: &'a [Attribute]) -> Result<A>
pub(crate) fn parse_attrs<'a, A>(attrs: &'a [Attribute]) -> Result<A>
where
A: TryFrom<&'a Attribute, Error = Error> + Attr,
{
Expand All @@ -111,15 +111,11 @@ where
}

/// Parse all `#[serde(..)]` attributes from the given slice.
#[cfg(feature = "serde-compat")]
#[allow(unused)]
pub fn parse_serde_attrs<'a, A>(attrs: &'a [Attribute]) -> Serde<A>
where
A: Attr,
Serde<A>: TryFrom<&'a Attribute, Error = Error>,
{
use crate::attr::Serde;

attrs
.iter()
.filter(|a| a.path().is_ident("serde"))
Expand Down Expand Up @@ -223,6 +219,20 @@ mod warning {
writer.print(&buffer)
}
}
#[cfg(not(feature = "serde-compat"))]
mod warning {
use std::fmt::Display;

// Just a stub!
#[allow(unused)]
pub fn print_warning(
title: impl Display,
content: impl Display,
note: impl Display,
) -> std::io::Result<()> {
Ok(())
}
}

/// formats the generic arguments (like A, B in struct X<A, B>{..}) as "<X>" where x is a comma
/// seperated list of generic arguments, or an empty string if there are no type generics (lifetime/const generics are ignored).
Expand Down
2 changes: 1 addition & 1 deletion ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ categories = [
"web-programming",
]
readme = "../README.md"
rust-version = "1.75.0"
rust-version = "1.63.0"

[features]
chrono-impl = ["chrono"]
Expand Down
7 changes: 2 additions & 5 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ mod recursive_export {
use std::{any::TypeId, collections::HashSet, path::Path};

use super::export_into;
use crate::{
typelist::{TypeList, TypeVisitor},
ExportError, TS,
};
use crate::{ExportError, TypeVisitor, TS};

/// Exports `T` to the file specified by the `#[ts(export_to = ..)]` attribute within the given
/// base directory.
Expand Down Expand Up @@ -85,7 +82,7 @@ mod recursive_export {
out_dir,
error: None,
};
T::dependency_types().for_each(&mut visitor);
T::visit_dependencies(&mut visitor);

if let Some(e) = visitor.error {
Err(e)
Expand Down
Loading

0 comments on commit 147ea87

Please sign in to comment.