-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from Aleph-Alpha/repro-path-bug
bug: `#[ts(export_to = "../path")]` can cause `diff_paths` to fail
- Loading branch information
Showing
5 changed files
with
100 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::path::{Component as C, Path, PathBuf}; | ||
|
||
use super::ExportError as E; | ||
|
||
const ERROR_MESSAGE: &str = r#"The path provided with `#[ts(export_to = "..")]` is not valid"#; | ||
pub fn absolute<T: AsRef<Path>>(path: T) -> Result<PathBuf, E> { | ||
let path = path.as_ref(); | ||
|
||
if path.is_absolute() { | ||
return Ok(path.to_owned()); | ||
} | ||
|
||
let path = std::env::current_dir()?.join(path); | ||
|
||
let mut out = Vec::new(); | ||
for comp in path.components() { | ||
match comp { | ||
C::CurDir => (), | ||
C::ParentDir => { | ||
out.pop().ok_or(E::CannotBeExported(ERROR_MESSAGE))?; | ||
} | ||
comp => out.push(comp), | ||
} | ||
} | ||
|
||
Ok(if !out.is_empty() { | ||
out.iter().collect() | ||
} else { | ||
PathBuf::from(".") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![allow(dead_code)] | ||
use ts_rs::TS; | ||
|
||
#[derive(TS)] | ||
#[ts(export, export_to = "../ts-rs/tests-out/path_bug/")] | ||
struct Foo { | ||
bar: Bar, | ||
} | ||
|
||
#[derive(TS)] | ||
#[ts(export_to = "tests-out/path_bug/aaa/")] | ||
struct Bar { | ||
i: i32, | ||
} | ||
|
||
#[test] | ||
fn path_bug() { | ||
Foo::export().unwrap(); | ||
Bar::export().unwrap(); | ||
|
||
let base = std::env::current_dir().unwrap(); | ||
assert!(base.join("./tests-out/path_bug/Foo.ts").is_file()); | ||
assert!(base.join("./tests-out/path_bug/aaa/Bar.ts").is_file()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,3 @@ fn test() { | |
"{ type: \"TaggedType\", a: number, b: number, }" | ||
) | ||
} | ||
|