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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! A small macro that gives us the path separator of the target system
//!
//! This can be used in conjunction with `include!(..)` or `include_str!(..)`
//! and `concat!(..)` to build paths.
//!
//! # Examples
//!
//! ```rust, ignore
//! #[macro_use] extern crate pathsep;
//!
//! include!(concat!(env!("OUT_DIR"), path_separator!(), "generated.rs"));
//! ```
//!
//! The same can be more succinctly written as:
//!
//! ```rust, ignore
//! #[macro_use] extern crate pathsep;
//!
//! include!(path_join!(env!("OUT_DIR"), "generated.rs"));
//! ```

/// Returns the path separator of the target system as a string.
///
/// On Unices, this is equal to "/", while on Windows it is "\\".
///
/// # Examples
///
/// ```rust
/// #[macro_use] extern crate pathsep;
///
/// assert_eq!(path_separator!(), "/");
/// ```
#[cfg(not(target_os = "windows"))]
#[macro_export]
macro_rules! path_separator {
    {} => { "/" }
}

/// Returns the path separator of the target system as a string.
///
/// On Unices, this is equal to "/", while on Windows it is "\\".
///
/// # Examples
///
/// ```rust
/// #[macro_use] extern crate pathsep;
///
/// assert_eq!(path_separator!(), "\\");
/// ```
#[cfg(target_os = "windows")]
#[macro_export]
macro_rules! path_separator {
    {} => { "\\" }
}

/// Join a path with the correct path separator
///
/// # Examples
///
/// Join a relative path of three args
///
/// ```rust
///# #[macro_use] extern crate pathsep;
/// assert_eq!(join_path!("src","def","impl"),
///     concat!("src", path_separator!(), "def", path_separator!(), "impl"));
/// ```
///
/// Make the path absolute by prepending a `/`
///
/// ```rust
///# #[macro_use] extern crate pathsep;
/// assert_eq!(join_path!(/ "var","tmp"),
///     concat!(path_separator!(), "var", path_separator!(), "tmp"));
/// ```
#[macro_export]
macro_rules! join_path {
    () => { "" };
    (/) => { path_separator!() };
    (/ $($s:expr),*) => { concat!(path_separator!(), join_path!($($s),*)) };
    ($first:expr) => { $first };
    ($first:expr, $( $s:expr ),*) => {
        concat!($first, path_separator!(), join_path!($($s),*));
    };
}