trybuild_internals_api/
path.rs

1use std::path::{Path, PathBuf};
2
3#[macro_export]
4macro_rules! path {
5    ($($tt:tt)+) => {
6        $crate::tokenize_path!([] [] $($tt)+)
7    };
8}
9
10// Private implementation detail.
11#[macro_export]
12macro_rules! tokenize_path {
13    ([$(($($component:tt)+))*] [$($cur:tt)+] /) => {
14        $crate::directory::Directory::new($crate::tokenize_path!([$(($($component)+))*] [$($cur)+]))
15    };
16
17    ([$(($($component:tt)+))*] [$($cur:tt)+] / $($rest:tt)+) => {
18        $crate::tokenize_path!([$(($($component)+))* ($($cur)+)] [] $($rest)+)
19    };
20
21    ([$(($($component:tt)+))*] [$($cur:tt)*] $first:tt $($rest:tt)*) => {
22        $crate::tokenize_path!([$(($($component)+))*] [$($cur)* $first] $($rest)*)
23    };
24
25    ([$(($($component:tt)+))*] [$($cur:tt)+]) => {
26        $crate::tokenize_path!([$(($($component)+))* ($($cur)+)])
27    };
28
29    ([$(($($component:tt)+))*]) => {{
30        let mut path = std::path::PathBuf::new();
31        $(
32            path.push(&($($component)+));
33        )*
34        path
35    }};
36}
37
38#[derive(#[automatically_derived]
impl ::core::cmp::Eq for CanonicalPath {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_receiver_is_total_eq(&self) -> () {
        let _: ::core::cmp::AssertParamIsEq<PathBuf>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for CanonicalPath {
    #[inline]
    fn eq(&self, other: &CanonicalPath) -> bool { self.0 == other.0 }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Ord for CanonicalPath {
    #[inline]
    fn cmp(&self, other: &CanonicalPath) -> ::core::cmp::Ordering {
        ::core::cmp::Ord::cmp(&self.0, &other.0)
    }
}Ord, #[automatically_derived]
impl ::core::cmp::PartialOrd for CanonicalPath {
    #[inline]
    fn partial_cmp(&self, other: &CanonicalPath)
        -> ::core::option::Option<::core::cmp::Ordering> {
        ::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
    }
}PartialOrd, #[automatically_derived]
impl ::core::clone::Clone for CanonicalPath {
    #[inline]
    fn clone(&self) -> CanonicalPath {
        CanonicalPath(::core::clone::Clone::clone(&self.0))
    }
}Clone)]
39pub(crate) struct CanonicalPath(PathBuf);
40
41impl CanonicalPath {
42    pub(crate) fn new(path: &Path) -> Self {
43        if let Ok(canonical) = path.canonicalize() {
44            CanonicalPath(canonical)
45        } else {
46            CanonicalPath(path.to_owned())
47        }
48    }
49}
50
51#[test]
52fn test_path_macro() {
53    struct Project {
54        dir: PathBuf,
55    }
56
57    let project = Project {
58        dir: PathBuf::from("../target/tests"),
59    };
60
61    let cargo_dir = path!(project.dir / ".cargo" / "config.toml");
62    assert_eq!(cargo_dir, Path::new("../target/tests/.cargo/config.toml"));
63}