From d9fb89865165bdd31807c3fbec2def6d3ebb7b53 Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 18 Aug 2023 21:53:36 +0200 Subject: refactor: replace use_dependency_history with a more generic macro --- src/util.rs | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'src/util.rs') diff --git a/src/util.rs b/src/util.rs index 2d2d911..373d784 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,15 +1,41 @@ -#[cfg(not(test))] -macro_rules! use_dependency_history { - () => { - use $crate::dependency_history::DependencyHistory; +//! Internal utilities. + +/// 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); +/// ``` +///
+/// +/// Expands to the following when `cfg(not(test))` +/// ```ignore +/// use crate::dependency_history::DependencyHistory; +/// ``` +///
+/// +/// 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)+ + ); }; -} -#[cfg(test)] -macro_rules! use_dependency_history { - () => { - use $crate::dependency_history::MockDependencyHistory as DependencyHistory; + ([$($part: ident),*] $item_path_part: ident) => { + #[cfg(not(test))] + use $($part::)* $item_path_part; + + ::paste::paste! { + #[cfg(test)] + use $($part::)* [] as $item_path_part; + } }; } -pub(crate) use use_dependency_history; +pub(crate) use use_double; -- cgit v1.2.3-18-g5258