-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
path.rs
44 lines (36 loc) · 1.06 KB
/
path.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
macro_rules! path {
($($tt:tt)+) => {
tokenize_path!([] [] $($tt)+)
};
}
// Private implementation detail.
macro_rules! tokenize_path {
([$(($($component:tt)+))*] [$($cur:tt)+] / $($rest:tt)+) => {
tokenize_path!([$(($($component)+))* ($($cur)+)] [] $($rest)+)
};
([$(($($component:tt)+))*] [$($cur:tt)*] $first:tt $($rest:tt)*) => {
tokenize_path!([$(($($component)+))*] [$($cur)* $first] $($rest)*)
};
([$(($($component:tt)+))*] [$($cur:tt)+]) => {
tokenize_path!([$(($($component)+))* ($($cur)+)])
};
([$(($($component:tt)+))*]) => {{
let mut path = std::path::PathBuf::new();
$(
path.push(&($($component)+));
)*
path
}};
}
#[test]
fn test_path_macro() {
use std::path::{Path, PathBuf};
struct Project {
dir: PathBuf,
}
let project = Project {
dir: PathBuf::from("../target/tests"),
};
let cargo_dir = path!(project.dir / ".cargo" / "config");
assert_eq!(cargo_dir, Path::new("../target/tests/.cargo/config"));
}