diff --git a/README.md b/README.md index 6aecd20b..b7684f0c 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,7 @@ export type User = { user_id: number, first_name: string, last_name: string, }; | smol_str-impl | Implement `TS` for types from *smol_str* | | tokio-impl | Implement `TS` for types from *tokio* | | jiff-impl | Implement `TS` for types from *jiff* | +| export-bindings | **Enabled by default**
See the section below on "controlling binding generation" for more information. |
@@ -130,6 +131,34 @@ When ts-rs encounters an unsupported serde attribute, a warning is emitted, unle | `TS_RS_IMPORT_EXTENSION` | File extension used in `import` statements | *none* | | `TS_RS_LARGE_INT` | Binding used for large integer types (`i64`, `u64`, `i128`, `u128`) | `bigint` | +### Controlling when the bindings are generated + +Feature "export-bindings", which is enabled by default, instructs ts-rs to export the bindings +when you run `cargo test`. This behavior may be undesirable if you need to export only at a +specific time (e.g., during `./configure` or a build pipeline.) + +To opt out of this automatic binding generation, disable the default features in your +Cargo.toml: + +```toml +[dependencies] +ts-rs = { version = "11", default-features = false, features = [ + "serde-compat", + "no-serde-warnings", +] } +``` + +Now, you may export the bindings on demand using the `--features` flag: + +```bash +cargo test --features ts-rs/export-bindings +``` + +Or, if you want to merely export the bindings and run no other tests: + +```bash +cargo test --features ts-rs/export-bindings -- 'export_bindings_' +``` ### Contributing Contributions are always welcome! diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 3ffe2b09..4eb62be9 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -9,8 +9,10 @@ homepage = "https://github.com/Aleph-Alpha/ts-rs" repository = "https://github.com/Aleph-Alpha/ts-rs" [features] +default = ["export-bindings"] serde-compat = ["termcolor"] no-serde-warnings = [] +export-bindings = [] [lib] proc-macro = true diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 00613489..9e5fc5d1 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -4,7 +4,7 @@ use std::collections::{HashMap, HashSet}; use proc_macro2::{Ident, TokenStream}; -use quote::{format_ident, quote}; +use quote::quote; use syn::{ parse_quote, spanned::Spanned, ConstParam, Expr, GenericParam, Generics, Item, LifetimeParam, Path, Result, Type, TypeArray, TypeParam, TypeParen, TypePath, TypeReference, TypeSlice, @@ -181,8 +181,9 @@ impl DerivedTS { } } + #[cfg(feature = "export-bindings")] fn generate_export_test(&self, rust_ty: &Ident, generics: &Generics) -> TokenStream { - let test_fn = format_ident!( + let test_fn = quote::format_ident!( "export_bindings_{}", rust_ty.to_string().to_lowercase().replace("r#", "") ); @@ -203,6 +204,10 @@ impl DerivedTS { } } } + #[cfg(not(feature = "export-bindings"))] + fn generate_export_test(&self, _rust_ty: &Ident, _generics: &Generics) -> TokenStream { + quote! {} + } fn generate_generics_fn(&self, generics: &Generics) -> TokenStream { let crate_rename = &self.crate_rename; diff --git a/ts-rs/Cargo.toml b/ts-rs/Cargo.toml index f00cac4c..3c38bb2a 100644 --- a/ts-rs/Cargo.toml +++ b/ts-rs/Cargo.toml @@ -18,7 +18,7 @@ readme = "../README.md" rust-version = "1.78.0" [features] -default = ["serde-compat"] +default = ["serde-compat", "export-bindings"] chrono-impl = ["chrono"] bigdecimal-impl = ["bigdecimal"] @@ -38,6 +38,7 @@ no-serde-warnings = ["ts-rs-macros/no-serde-warnings"] import-esm = [] tokio-impl = ["tokio"] jiff-impl = ["jiff"] +export-bindings = ["ts-rs-macros/export-bindings"] [dev-dependencies] serde = { version = "1.0", features = ["derive"] } @@ -46,7 +47,7 @@ chrono = { version = "0.4", features = ["serde"] } tokio = { version = "1.40", features = ["sync", "rt"] } [dependencies] -ts-rs-macros = { version = "=11.1.0", path = "../macros" } +ts-rs-macros = { version = "=11.1.0", path = "../macros", default-features = false } thiserror = "2" heapless = { version = ">= 0.7, < 0.9", optional = true } diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 5c670484..8ee1cfeb 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -92,6 +92,7 @@ //! | smol_str-impl | Implement `TS` for types from *smol_str* | //! | tokio-impl | Implement `TS` for types from *tokio* | //! | jiff-impl | Implement `TS` for types from *jiff* | +//! | export-bindings | **Enabled by default**
See the section below on "controlling binding generation" for more information. | //! //!
//! @@ -128,6 +129,34 @@ //! | `TS_RS_IMPORT_EXTENSION` | File extension used in `import` statements | *none* | //! | `TS_RS_LARGE_INT` | Binding used for large integer types (`i64`, `u64`, `i128`, `u128`) | `bigint` | //! +//! ## Controlling when the bindings are generated +//! +//! Feature "export-bindings", which is enabled by default, instructs ts-rs to export the bindings +//! when you run `cargo test`. This behavior may be undesirable if you need to export only at a +//! specific time (e.g., during `./configure` or a build pipeline.) +//! +//! To opt out of this automatic binding generation, disable the default features in your +//! Cargo.toml: +//! +//! ```toml +//! [dependencies] +//! ts-rs = { version = "11", default-features = false, features = [ +//! "serde-compat", +//! "no-serde-warnings", +//! ] } +//! ``` +//! +//! Now, you may export the bindings on demand using the `--features` flag: +//! +//! ```bash +//! cargo test --features ts-rs/export-bindings +//! ``` +//! +//! Or, if you want to merely export the bindings and run no other tests: +//! +//! ```bash +//! cargo test --features ts-rs/export-bindings -- 'export_bindings_' +//! ``` //! //! ## Contributing //! Contributions are always welcome!