-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternative handling of output paths #256
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,17 +34,22 @@ impl DerivedTS { | |
.export | ||
.then(|| self.generate_export_test(&rust_ty, &generics)); | ||
|
||
let export_to = { | ||
let output_path_fn = { | ||
let path = match self.export_to.as_deref() { | ||
Some(dirname) if dirname.ends_with('/') => { | ||
format!("{}{}.ts", dirname, self.ts_name) | ||
} | ||
Some(filename) => filename.to_owned(), | ||
None => format!("bindings/{}.ts", self.ts_name), | ||
None => format!("{}.ts", self.ts_name), | ||
}; | ||
|
||
quote! { | ||
const EXPORT_TO: Option<&'static str> = Some(#path); | ||
fn output_path() -> Option<std::path::PathBuf> { | ||
let path = std::env::var("TS_RS_EXPORT_DIR"); | ||
let path = path.as_deref().unwrap_or("./bindings"); | ||
|
||
Some(std::path::Path::new(path).join(#path)) | ||
} | ||
Comment on lines
+47
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of that is super simple - Take |
||
} | ||
}; | ||
|
||
|
@@ -65,7 +70,6 @@ impl DerivedTS { | |
quote! { | ||
#impl_start { | ||
#assoc_type | ||
#export_to | ||
|
||
fn ident() -> String { | ||
#ident.to_owned() | ||
|
@@ -76,6 +80,7 @@ impl DerivedTS { | |
#decl | ||
#inline | ||
#generics_fn | ||
#output_path_fn | ||
|
||
#[allow(clippy::unused_unit)] | ||
fn dependency_types() -> impl ts_rs::typelist::TypeList | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,9 +83,9 @@ | |
//! | ordered-float-impl | Implement `TS` for types from *ordered_float* | | ||
//! | heapless-impl | Implement `TS` for types from *heapless* | | ||
//! | semver-impl | Implement `TS` for types from *semver* | | ||
//! | ||
//! | ||
//! <br/> | ||
//! | ||
//! | ||
//! If there's a type you're dealing with which doesn't implement `TS`, use either | ||
//! `#[ts(as = "..")]` or `#[ts(type = "..")]`, or open a PR. | ||
//! | ||
|
@@ -132,7 +132,6 @@ use std::{ | |
path::{Path, PathBuf}, | ||
}; | ||
|
||
pub use export::output_path; | ||
pub use ts_rs_macros::TS; | ||
|
||
pub use crate::export::ExportError; | ||
|
@@ -154,9 +153,9 @@ pub mod typelist; | |
/// Bindings can be exported within a test, which ts-rs generates for you by adding `#[ts(export)]` | ||
/// to a type you wish to export to a file. | ||
/// If, for some reason, you need to do this during runtime, you can call [`TS::export`] yourself. | ||
/// | ||
/// | ||
/// **Note:** | ||
/// Annotating a type with `#[ts(export)]` (or exporting it during runtime using | ||
/// Annotating a type with `#[ts(export)]` (or exporting it during runtime using | ||
/// [`TS::export`]) will cause all of its dependencies to be exported as well. | ||
/// | ||
/// ### serde compatibility | ||
|
@@ -179,12 +178,11 @@ pub mod typelist; | |
/// TS_RS_EXPORT_DIR = { value = "<OVERRIDE_DIR>", relative = true } | ||
/// ``` | ||
/// <br/> | ||
/// | ||
/// | ||
/// - **`#[ts(export_to = "..")]`** | ||
/// Specifies where the type should be exported to. Defaults to `bindings/<name>.ts`. | ||
/// Specifies where the type should be exported to. Defaults to `<name>.ts`. | ||
/// The path given to the `export_to` attribute is relative to the `TS_RS_EXPORT_DIR` environment variable, | ||
/// or, if `TS_RS_EXPORT_DIR` is not set, to you project's root directory - more specifically, | ||
/// it'll be relative to the `Cargo.toml` file. | ||
/// or, if `TS_RS_EXPORT_DIR` is not set, to `./bindings` | ||
/// If the provided path ends in a trailing `/`, it is interpreted as a directory. | ||
/// Note that you need to add the `export` attribute as well, in order to generate a test which exports the type. | ||
/// <br/><br/> | ||
|
@@ -299,11 +297,8 @@ pub trait TS { | |
/// ``` | ||
type WithoutGenerics: TS + ?Sized; | ||
|
||
/// The path given to `#[ts(export_to = "...")]` | ||
const EXPORT_TO: Option<&'static str> = None; | ||
|
||
/// JSDoc comment to describe this type in TypeScript - when `TS` is derived, docs are | ||
/// automatically read from your doc comments or `#[doc = ".."]` attrubutes | ||
/// automatically read from your doc comments or `#[doc = ".."]` attributes | ||
const DOCS: Option<&'static str> = None; | ||
|
||
/// Identifier of this type, excluding generic parameters. | ||
|
@@ -409,6 +404,20 @@ pub trait TS { | |
{ | ||
export::export_type_to_string::<Self>() | ||
} | ||
|
||
/// Returns the output path to where `T` should be exported. | ||
/// | ||
/// When deriving `TS`, the output path can be altered using `#[ts(export_to = "...")]`. | ||
/// See the documentation of [`TS`] for more details. | ||
/// | ||
/// The output of this function depends on the environment variable `TS_RS_EXPORT_DIR`, which is | ||
/// used as base directory. If it is not set, `./bindings` is used as default directory. | ||
/// | ||
/// If `T` cannot be exported (e.g because it's a primitive type), this function will return | ||
/// `None`. | ||
fn output_path() -> Option<PathBuf> { | ||
None | ||
} | ||
Comment on lines
+407
to
+420
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding the implementation: I moved |
||
} | ||
|
||
/// A typescript type which is depended upon by other types. | ||
|
@@ -429,11 +438,7 @@ impl Dependency { | |
/// If `T` is not exportable (meaning `T::EXPORT_TO` is `None`), this function will return | ||
/// `None` | ||
pub fn from_ty<T: TS + 'static + ?Sized>() -> Option<Self> { | ||
let exported_to = output_path::<T>() | ||
.ok() | ||
.as_deref() | ||
.and_then(Path::to_str) | ||
.map(ToOwned::to_owned)?; | ||
let exported_to = T::output_path()?.to_str()?.to_owned(); | ||
Some(Dependency { | ||
type_id: TypeId::of::<T>(), | ||
ts_name: T::ident(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before, renaming the file from
Role.ts
toUserRole.ts
required adding thebindings/
prefix back again.With this change, this is no longer necessary.