#[macro_export]
macro_rules! dir_inner {
($path:literal) => {{
let path: &::std::path::Path = $path.as_ref();
$crate::FileTree::new_regular(path)
}};
($file:expr) => {{
$file
}};
}
#[macro_export]
macro_rules! dir {
($path:expr, [$($args:expr),*] ) => {{
let mut children = vec![];
$(
let file = $crate::dir_inner!( $args );
children.push(file);
)*
let path: &::std::path::Path = $path.as_ref();
$crate::FileTree::new_directory(path, children)
}};
}
#[macro_export]
macro_rules! tree {
($($any:tt)*) => {{
let mut result = dir!( $($any)* );
result.make_paths_relative();
result
}};
}
#[macro_export]
macro_rules! file {
($path:expr) => {{
let path: &::std::path::Path = $path.as_ref();
$crate::FileTree::new_regular(path)
}};
}
#[macro_export]
macro_rules! symlink {
($source:expr, $target:expr) => {{
let source: &::std::path::Path = $source.as_ref();
let target: &::std::path::Path = $target.as_ref();
$crate::FileTree::new_symlink(source, target)
}};
}
#[cfg(test)]
mod tests {
use crate::FileTree;
#[test]
fn testing_macros() {
let file = tree!("root", [
"file1",
file!("file2"),
dir!("inner_dir", ["more_file1", "more_file2", symlink!("from", "to")]),
"file3"
]);
#[rustfmt::skip]
let expected = FileTree::new_directory("root", vec![
FileTree::new_regular("root/file1"),
FileTree::new_regular("root/file2"),
FileTree::new_directory("root/inner_dir", vec![
FileTree::new_regular("root/inner_dir/more_file1"),
FileTree::new_regular("root/inner_dir/more_file2"),
FileTree::new_symlink("root/inner_dir/from", "root/inner_dir/to"),
]),
FileTree::new_regular("root/file3"),
]);
assert_eq!(file, expected);
}
}