Macro build_fs_tree::dir [−][src]
macro_rules! dir { ($($key:expr => $value:expr $(,)?)*) => { ... }; }
Expand description
Create representation of a directory.
NOTES:
- Syntax: The syntax used by this macro is similar to the syntax used by maplit except that in this macro, commas are optional.
- Typings: The types of
Path
andFileContent
generic parameter isn’t required to be the types provided by the expressions that users wrote as long as they implementFrom<X>
whereX
is the types of the aforementioned user provided expressions.
Syntax
The syntax used by this macro is similar to the syntax used by maplit except that in this macro, commas are optional.
Example: Without commas
use build_fs_tree::{FileSystemTree, dir, file}; let tree: FileSystemTree<&str, &str> = dir! { "a" => file!("foo") "b" => file!("bar") "c" => dir! { "x" => file!("baz") } }; assert_eq!( tree.dir_content().unwrap() .get("a").unwrap().file_content().unwrap(), &"foo", ); assert_eq!( tree.dir_content().unwrap() .get("b").unwrap().file_content().unwrap(), &"bar", ); assert_eq!( tree.dir_content().unwrap() .get("c").unwrap().dir_content().unwrap() .get("x").unwrap().file_content().unwrap(), &"baz", );
Example: With commas
use build_fs_tree::{FileSystemTree, dir, file}; let tree: FileSystemTree<&str, &str> = dir! { "a" => file!("foo"), "b" => file!("bar"), "c" => dir! { "x" => file!("baz"), }, }; assert_eq!( tree.dir_content().unwrap() .get("a").unwrap().file_content().unwrap(), &"foo", ); assert_eq!( tree.dir_content().unwrap() .get("b").unwrap().file_content().unwrap(), &"bar", ); assert_eq!( tree.dir_content().unwrap() .get("c").unwrap().dir_content().unwrap() .get("x").unwrap().file_content().unwrap(), &"baz", );
Typings
The types of Path
and FileContent
generic parameter isn’t required to be the types
provided by the expressions that users wrote as long as they implement
From<X>
where X
is the types of the aforementioned user
provided expressions.
Example: Where Path
is a String
use build_fs_tree::{FileSystemTree, dir, file}; let tree: FileSystemTree<String, &str> = dir! { "a" => file!("foo") "b" => file!("bar") "c" => dir! { "x" => file!("baz") } }; assert_eq!( tree.dir_content().unwrap() .get("a").unwrap().file_content().unwrap(), &"foo", ); assert_eq!( tree.dir_content().unwrap() .get("b").unwrap().file_content().unwrap(), &"bar", ); assert_eq!( tree.dir_content().unwrap() .get("c").unwrap().dir_content().unwrap() .get("x").unwrap().file_content().unwrap(), &"baz", );
Example: Where Path
is a PathBuf
and FileContent
is a Vec<u8>
use build_fs_tree::{FileSystemTree, dir, file}; use std::path::PathBuf; let tree: FileSystemTree<PathBuf, Vec<u8>> = dir! { "a" => file!("foo") "b" => file!("bar") "c" => dir! { "x" => file!("baz") } }; assert_eq!( tree.dir_content().unwrap() .get(&PathBuf::from("a")).unwrap().file_content().unwrap(), &Vec::from("foo"), ); assert_eq!( tree.dir_content().unwrap() .get(&PathBuf::from("b")).unwrap().file_content().unwrap(), &Vec::from("bar"), ); assert_eq!( tree.dir_content().unwrap() .get(&PathBuf::from("c")).unwrap().dir_content().unwrap() .get(&PathBuf::from("x")).unwrap().file_content().unwrap(), &Vec::from("baz"), );