From 5ecae522de8a56dcbd46ab6ec3f8d255cfd7d231 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Wed, 6 Mar 2024 17:53:27 -0300 Subject: [PATCH] add TS::export_path --- ts-rs/src/lib.rs | 27 +++++++++++++++++---------- ts-rs/tests/docs.rs | 30 ++++++++---------------------- ts-rs/tests/export_manually.rs | 14 ++------------ ts-rs/tests/path_bug.rs | 7 ++----- 4 files changed, 29 insertions(+), 49 deletions(-) diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 45b8bcd..e0ed343 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -286,9 +286,23 @@ pub trait TS { /// ``` type WithoutGenerics: TS + ?Sized; + /// The path given to `#[ts(export_to = "...")]` const EXPORT_TO: Option<&'static str> = None; const DOCS: Option<&'static str> = None; + /// Returns the path to which the type will be exported, taking + /// `TS_RS_EXPORT_DIR` into account + fn export_path() -> Option { + let base = std::env::var("TS_RS_EXPORT_DIR") + .ok() + .as_deref() + .map(Path::new) + .unwrap_or_else(|| Path::new(".")) + .to_owned(); + + Some(base.join(Self::EXPORT_TO?)) + } + /// Identifier of this type, excluding generic parameters. fn ident() -> String { // by default, fall back to `TS::name()`. @@ -421,17 +435,10 @@ impl Dependency { /// If `T` is not exportable (meaning `T::EXPORT_TO` is `None`), this function will return /// `None` pub fn from_ty() -> Option { - let base = std::env::var("TS_RS_EXPORT_DIR") - .ok() + let exported_to = T::export_path() .as_deref() - .map(Path::new) - .unwrap_or_else(|| Path::new(".")) - .to_owned(); - - let exported_to = base - .join(T::EXPORT_TO.map(ToOwned::to_owned)?) - .to_string_lossy() - .to_string(); + .and_then(Path::to_str) + .map(ToOwned::to_owned)?; Some(Dependency { type_id: TypeId::of::(), ts_name: T::ident(), diff --git a/ts-rs/tests/docs.rs b/ts-rs/tests/docs.rs index bfef23e..0da2c88 100644 --- a/ts-rs/tests/docs.rs +++ b/ts-rs/tests/docs.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] -use std::{concat, fs, path::PathBuf}; +use std::{concat, fs}; use ts_rs::TS; @@ -96,13 +96,6 @@ struct G { /* ============================================================================================== */ -fn generate_path(path: &str) -> PathBuf { - std::env::current_dir().unwrap() - .join(std::env::var("TS_RS_EXPORT_DIR").unwrap_or_default()) - .join("tests-out/docs/") - .join(path) -} - #[test] fn export_a() { A::export().unwrap(); @@ -144,14 +137,7 @@ fn export_a() { ) }; - let Ok(actual_content) = fs::read_to_string(generate_path("A.ts")) else { - panic!( - "failed to read path: {}\ncurrent dir: {}\nTS_RS_EXPORT_DIR: {}", - generate_path("A.ts").to_string_lossy(), - std::env::current_dir().unwrap().to_string_lossy(), - std::env::var("TS_RS_EXPORT_DIR").unwrap_or_default() - ) - }; + let actual_content = fs::read_to_string(A::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -197,7 +183,7 @@ fn export_b() { ) }; - let actual_content = fs::read_to_string(generate_path("B.ts")).unwrap(); + let actual_content = fs::read_to_string(B::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -230,7 +216,7 @@ fn export_c() { ) }; - let actual_content = fs::read_to_string(generate_path("C.ts")).unwrap(); + let actual_content = fs::read_to_string(C::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -262,7 +248,7 @@ fn export_d() { "export type D = null;" ) }; - let actual_content = fs::read_to_string(generate_path("D.ts")).unwrap(); + let actual_content = fs::read_to_string(D::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -295,7 +281,7 @@ fn export_e() { ) }; - let actual_content = fs::read_to_string(generate_path("E.ts")).unwrap(); + let actual_content = fs::read_to_string(E::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -343,7 +329,7 @@ fn export_f() { ) }; - let actual_content = fs::read_to_string(generate_path("F.ts")).unwrap(); + let actual_content = fs::read_to_string(F::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -391,7 +377,7 @@ fn export_g() { ) }; - let actual_content = fs::read_to_string(generate_path("G.ts")).unwrap(); + let actual_content = fs::read_to_string(G::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } diff --git a/ts-rs/tests/export_manually.rs b/ts-rs/tests/export_manually.rs index 26eed37..038a10b 100644 --- a/ts-rs/tests/export_manually.rs +++ b/ts-rs/tests/export_manually.rs @@ -36,12 +36,7 @@ fn export_manually() { ) }; - let actual_content = fs::read_to_string( - match std::env::var("TS_RS_EXPORT_DIR") { - Ok(path) => format!("{}/tests-out/export_manually/UserFile.ts", path.trim_end_matches('/')), - Err(_) => "tests-out/export_manually/UserFile.ts".to_owned(), - } - ).unwrap(); + let actual_content = fs::read_to_string(User::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } @@ -62,12 +57,7 @@ fn export_manually_dir() { ) }; - let actual_content = fs::read_to_string( - match std::env::var("TS_RS_EXPORT_DIR") { - Ok(path) => format!("{}/tests-out/export_manually/dir/UserDir.ts", path.trim_end_matches('/')), - Err(_) => "tests-out/export_manually/dir/UserDir.ts".to_owned(), - } - ).unwrap(); + let actual_content = fs::read_to_string(UserDir::export_path().unwrap()).unwrap(); assert_eq!(actual_content, expected_content); } diff --git a/ts-rs/tests/path_bug.rs b/ts-rs/tests/path_bug.rs index 11dcc5e..58d060d 100644 --- a/ts-rs/tests/path_bug.rs +++ b/ts-rs/tests/path_bug.rs @@ -17,9 +17,6 @@ struct Bar { fn path_bug() { export_bindings_foo(); - let base = std::env::current_dir() - .unwrap() - .join(std::env::var("TS_RS_EXPORT_DIR").unwrap_or("".to_owned())); - assert!(base.join("../ts-rs/tests-out/path_bug/Foo.ts").is_file()); - assert!(base.join("tests-out/path_bug/aaa/Bar.ts").is_file()); + assert!(Foo::export_path().unwrap().is_file()); + assert!(Bar::export_path().unwrap().is_file()); }