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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#![no_implicit_prelude]

/// Create representation of a [directory](crate::FileSystemTree::Directory).
///
/// **NOTES:**
/// * **Syntax:** The syntax used by this macro is similar to the syntax used by
///   [maplit](https://docs.rs/maplit/1.0.2/maplit/) except that in this macro, commas are
///   optional.
/// * **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>`](::std::convert::From) where `X` is the types of the aforementioned user
///   provided expressions.
///
/// # Syntax
///
/// The syntax used by this macro is similar to the syntax used by
/// [maplit](https://docs.rs/maplit/1.0.2/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")
///     }
/// };
///
/// # dbg!(&tree);
/// 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"),
///     },
/// };
///
/// # dbg!(&tree);
/// 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>`](::std::convert::From) 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")
///     }
/// };
///
/// # dbg!(&tree);
/// 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")
///     }
/// };
///
/// # dbg!(&tree);
/// 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"),
/// );
/// ```
#[macro_export]
macro_rules! dir {
    ($($key:expr => $value:expr $(,)?)*) => {{
        let mut __directory_content = ::std::collections::BTreeMap::new();
        $(
            let _ = ::std::collections::BTreeMap::insert(
                &mut __directory_content,
                ::std::convert::From::from($key),
                $value
            );
        )*
        $crate::FileSystemTree::Directory(__directory_content)
    }};
}

/// Create representation of a [file](crate::FileSystemTree::File).
///
/// # Syntax
///
/// **Example:**
///
/// ```
/// use build_fs_tree::{FileSystemTree, file};
/// let file: FileSystemTree<&str, &str> = file!("CONTENT OF THE FILE");
/// assert_eq!(file, FileSystemTree::File("CONTENT OF THE FILE"));
/// ```
///
/// # Typings
///
/// This macro calls [`From::from`](::std::convert::From::from) under the hood.
///
/// **Example:** Where `FileContent` is a `String`
///
/// ```
/// use build_fs_tree::{FileSystemTree, file};
/// let file: FileSystemTree<&str, String> = file!("CONTENT OF THE FILE");
/// assert_eq!(file, FileSystemTree::File("CONTENT OF THE FILE".to_string()));
/// ```
///
/// **Example:** Where `FileContent` is a `Vec<u8>`
///
/// ```
/// use build_fs_tree::{FileSystemTree, file};
/// let file: FileSystemTree<&str, Vec<u8>> = file!("CONTENT OF THE FILE");
/// assert_eq!(file, FileSystemTree::File("CONTENT OF THE FILE".into()));
/// ```
#[macro_export]
macro_rules! file {
    ($content:expr) => {
        $crate::FileSystemTree::File(::std::convert::From::from($content))
    };
}