#[macro_export]
macro_rules! path {
($($tt:tt)+) => {
$crate::__tokenize_path!([] [] $($tt)+)
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __tokenize_path {
([$(($($component:tt)+))*] [$($cur:tt)+] / $($rest:tt)+) => {
$crate::__tokenize_path!([$(($($component)+))* ($($cur)+)] [] $($rest)+)
};
([$(($($component:tt)+))*] [$($cur:tt)*] $first:tt $($rest:tt)*) => {
$crate::__tokenize_path!([$(($($component)+))*] [$($cur)* $first] $($rest)*)
};
([$(($($component:tt)+))*] [$($cur:tt)+]) => {
$crate::__tokenize_path!([$(($($component)+))* ($($cur)+)])
};
([$(($($component:tt)+))*]) => {{
let mut path = ::std::path::PathBuf::new();
$(
path.push(&($($component)+));
)*
path
}};
}
#[cfg(test)]
mod tests {
#[test]
fn test_path_macro() {
use std::path::{Path, PathBuf};
let p = path!("a" / "b" / "c");
#[cfg(unix)]
assert_eq!(p, Path::new(r"a/b/c"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"a\b\c"));
let p = path!("../a/b");
assert_eq!(p, Path::new("../a/b"));
let p = path!("../a/b" / "c" / "d");
#[cfg(unix)]
assert_eq!(p, Path::new(r"../a/b/c/d"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"../a/b\c\d"));
let p = path!(PathBuf::from("../a/b") / "c" / "d");
#[cfg(unix)]
assert_eq!(p, Path::new(r"../a/b/c/d"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"../a/b\c\d"));
let p = path!(Path::new("../a/b") / "c" / "d");
#[cfg(unix)]
assert_eq!(p, Path::new(r"../a/b/c/d"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"../a/b\c\d"));
let p = path!("x" / PathBuf::from("../a/b") / "c" / "d");
#[cfg(unix)]
assert_eq!(p, Path::new(r"x/../a/b/c/d"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"x\../a/b\c\d"));
let p = path!("x" / Path::new("../a/b") / "c" / "d");
#[cfg(unix)]
assert_eq!(p, Path::new(r"x/../a/b/c/d"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"x\../a/b\c/d"));
let p = path!("../a/b" / "c/d");
#[cfg(unix)]
assert_eq!(p, Path::new(r"../a/b/c/d"));
#[cfg(windows)]
assert_eq!(p, Path::new(r"../a/b\c/d"));
}
}