Skip to content

Commit

Permalink
small refactor w/ some better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
stegaBOB committed Jan 21, 2024
1 parent ac4934d commit 52e5601
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
5 changes: 1 addition & 4 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ impl DerivedTS {
None => {
get_export_to = quote! {
fn get_export_to() -> Option<String> {
ts_rs::provided_default_path().as_ref().map_or_else(
|| Self::EXPORT_TO.map(ToString::to_string),
|path| Some(format!("{path}/{}.ts", Self::name())),
)
ts_rs::__private::get_export_to_path::<Self>()
}
};
format!("bindings/{}.ts", self.name)
Expand Down
23 changes: 19 additions & 4 deletions ts-rs/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,26 @@ pub(crate) fn export_type_to<T: TS + ?Sized + 'static, P: AsRef<Path>>(
Ok(())
}

pub(crate) const EXPORT_PATH_ENV_VAR: &str = "TS_RS_EXPORT_PATH";
#[doc(hidden)]
pub mod __private {
use super::*;

const EXPORT_PATH_ENV_VAR: &str = "TS_RS_EXPORT_PATH";
fn provided_default_path() -> &'static Option<String> {
static EXPORT_TO: OnceLock<Option<String>> = OnceLock::new();
EXPORT_TO.get_or_init(|| std::env::var(EXPORT_PATH_ENV_VAR).ok())
}

pub fn provided_default_path() -> &'static Option<String> {
static EXPORT_TO: OnceLock<Option<String>> = OnceLock::new();
EXPORT_TO.get_or_init(|| std::env::var(EXPORT_PATH_ENV_VAR).ok())
/// Returns the path to where `T` should be exported using the `TS_RS_EXPORT_PATH` environment variable.
///
/// This should only be used by the TS derive macro; the `get_export_to` trait method should not
/// be overridden if the `#[ts(export_to = ..)]` attribute exists.
pub fn get_export_to_path<T: TS + ?Sized>() -> Option<String> {
provided_default_path().as_ref().map_or_else(
|| T::EXPORT_TO.map(ToString::to_string),
|path| Some(format!("{path}/{}.ts", T::name())),
)
}
}

/// Returns the generated defintion for `T`.
Expand Down
2 changes: 1 addition & 1 deletion ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ use std::{

pub use ts_rs_macros::TS;

pub use crate::export::{provided_default_path, ExportError};
pub use crate::export::{ExportError, __private};

#[cfg(feature = "chrono-impl")]
mod chrono;
Expand Down

0 comments on commit 52e5601

Please sign in to comment.