aboutsummaryrefslogtreecommitdiff
path: root/macros/src/util/mod.rs
blob: 355789603245c0d6b49fadb8b7aa8f40b870dc2d (plain)
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
pub mod error;
pub mod item_impl;
pub mod iterator_ext;
pub mod string;
pub mod syn_ext;
pub mod syn_path;
pub mod tokens;

macro_rules! to_option {
    ($($tokens: tt)+) => {
        Some($($tokens)+)
    };

    () => {
        None
    };
}

macro_rules! or {
    (($($tokens: tt)+) else ($($default: tt)*)) => {
        $($tokens)*
    };

    (() else ($($default: tt)*)) => {
        $($default)*
    };
}

/// Imports the specified item, prepending 'Mock' to the item identifier if the `test`
/// configuration option is set.
///
/// # Examples
/// ```ignore
/// use_double!(crate::dependency_history::DependencyHistory);
/// ```
/// <br>
///
/// Expands to the following when `cfg(not(test))`
/// ```ignore
/// use crate::dependency_history::DependencyHistory;
/// ```
/// <br>
///
/// Expands to the following when `cfg(test)`
/// ```ignore
/// use crate::dependency_history::MockDependencyHistory as DependencyHistory;
/// ```
macro_rules! use_double {
    ($([$($part: ident),*])? $item_path_part: ident :: $($next_part: tt)+) => {
        use_double!(
            [$($($part,)*)? $item_path_part]
            $($next_part)+
        );
    };

    ([$($part: ident),*] $item_path_part: ident) => {
        #[cfg(not(test))]
        use $($part::)* $item_path_part;

        ::paste::paste! {
            #[cfg(test)]
            use $($part::)* [<Mock $item_path_part>] as $item_path_part;
        }
    };
}

pub(crate) use {or, to_option, use_double};