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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright notice and licensing information.
// These lines indicate the copyright of the software and its licensing terms.
// SPDX-License-Identifier: Apache-2.0 OR MIT indicates dual licensing under Apache 2.0 or MIT licenses.
// Copyright © 2023-2024 LibMake. All rights reserved.
//! This module provides macros for directory operations, including checking directory existence,
//! creating multiple directories at once, and cleaning up directories.
//!
//! # `macro_check_directory` Macro
//!
//! Checks if a directory exists and creates it if necessary.
//!
//! ## Usage
//!
//! ```rust
//! use libmake::macro_check_directory;
//! use std::path::Path;
//!
//! let path = Path::new("logs");
//! macro_check_directory!(path, "logs");
//! ```
//!
//! ## Arguments
//!
//! * `_dir` - The path to check, as a `std::path::Path`.
//! * `_name` - A string literal representing the directory name. This is used in error messages.
//!
//! ## Behaviour
//!
//! The `macro_check_directory` macro checks if the directory specified by `_dir` exists.
//! If it exists and is not a directory, a panic with an error message is triggered.
//! If the directory doesn't exist, the macro attempts to create it using `std::fs::create_dir_all(_dir)`.
//!
//! # `macro_cleanup_directories` Macro
//!
//! Cleans up multiple directories by invoking the `cleanup_directory` function.
//!
//! ## Usage
//!
//! ```rust
//! use std::path::Path;
//! use libmake::macro_check_directory;
//!
//! let path = Path::new("logs");
//! macro_check_directory!(path, "logs");
//! ```
//!
//! ## Arguments
//!
//! * `$( $_dir:expr ),*` - A comma-separated list of directory paths to clean up.
//!
//! ## Behaviour
//!
//! The `macro_cleanup_directories` macro takes multiple directory paths as arguments
//! and invokes the `cleanup_directory` function for each path.
//!
//! # `macro_create_directories` Macro
//!
//! Creates multiple directories at once.
//!
//! ## Usage
//!
//! ```rust
//! use libmake::{macro_create_directories, macro_cleanup_directories};
//! use std::path::Path;
//!
//! macro_create_directories!("logs", "logs1", "logs2");
//! macro_cleanup_directories!(Path::new("./logs"), Path::new("./logs1"), Path::new("./logs2"));
//! ```
//!
//! ## Arguments
//!
//! * `...` - Variable number of directory paths, each specified as an expression (`expr`).
//!
//! ## Behaviour
//!
//! The `macro_create_directories` macro creates multiple directories at once.
//!
//! ## Example
//!
//! ```rust
//! use libmake::{macro_create_directories, macro_cleanup_directories};
//! use std::path::Path;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let test = Path::new("logs");
//! let test2 = Path::new("logs1");
//! macro_create_directories!(test, test2)?;
//! macro_cleanup_directories!(test, test2);
//! Ok(())
//! }
//! ```
//!
//! # Note
//!
//! These macros assume familiarity with Rust macros and their usage.
//! Users are encouraged to review Rust macro documentation for a better understanding
//! of how to work with macros effectively.
/// # `macro_check_directory` Macro
///
/// Check if a directory exists and create it if necessary.
///
/// ## Usage
///
/// ```rust
/// use libmake::macro_check_directory;
/// use std::path::Path;
///
/// let path = Path::new("logs");
/// macro_check_directory!(path, "logs");
/// ```
///
/// ## Arguments
///
/// * `_dir` - The path to check, as a `std::path::Path`.
/// * `_name` - A string literal representing the directory name. This is used in error messages.
///
/// ## Behaviour
///
/// The `macro_check_directory` macro checks if the directory specified by `_dir` exists. If it exists and is not a directory, a panic with an error message is triggered. If the directory doesn't exist, the macro attempts to create it using `std::fs::create_dir_all(_dir)`. If the creation is successful, no action is taken. If an error occurs during the directory creation, a panic is triggered with an error message indicating the failure.
///
/// Please note that the macro panics on failure. Consider using this macro in scenarios where panicking is an acceptable behaviour, such as during application startup or setup.
///
/// # See Also
///
/// - [`macro_create_directories`] for creating multiple directories
/// - [`macro_cleanup_directories`] for cleaning up directories
///
/// # `macro_cleanup_directories` Macro
///
/// Cleanup multiple directories by invoking the `cleanup_directory` function.
///
/// ## Usage
///
/// ```rust
/// use std::path::Path;
/// use libmake::macro_check_directory;
///
/// let path = Path::new("logs");
/// macro_check_directory!(path, "logs");
/// ```
///
/// ## Arguments
///
/// * `$( $_dir:expr ),*` - A comma-separated list of directory paths to clean up.
///
/// ## Behaviour
///
/// The `macro_cleanup_directories` macro takes multiple directory paths as arguments and invokes the `cleanup_directory` function for each path. It is assumed that the `cleanup_directory` function is available in the crate's utilities module (`$crate::utilities::cleanup_directory`).
///
/// The macro creates an array `directories` containing the provided directory paths and passes it as an argument to `cleanup_directory`. The `cleanup_directory` function is responsible for performing the cleanup operations.
///
/// Please note that the macro uses the `?` operator for error propagation. It expects the `cleanup_directory` function to return a `Result` type. If an error occurs during the cleanup process, it will be propagated up the call stack, allowing the caller to handle it appropriately.
///
/// # See Also
///
/// - [`macro_check_directory`] for checking and creating a single directory
/// - [`macro_create_directories`] for creating multiple directories
///
/// # `macro_create_directories` Macro
///
/// Create multiple directories at once.
///
/// ## Usage
///
/// ```rust
/// use libmake::{macro_create_directories, macro_cleanup_directories};
/// use std::path::Path;
/// macro_create_directories!("logs", "logs1", "logs2");
/// macro_cleanup_directories!(Path::new("./logs"), Path::new("./logs1"), Path::new("./logs2"));
/// ```
///
/// ## Arguments
///
/// * `...` - Variable number of directory paths, each specified as an expression (`expr`).
///
/// ## Behaviour
///
/// The `macro_create_directories` macro creates multiple directories at once. It takes a variable number of directory paths as arguments and uses the `create_directory` utility function from the `$crate` crate to create the directories.
///
/// The directories are specified as expressions and separated by commas. For example, `macro_create_directories!("logs", "logs1", "logs2")` will attempt to create the `logs`, `logs1`, and `logs2`.
///
/// The macro internally creates a slice of the directory paths and passes it to the `create_directory` function. If any error occurs during the directory creation, the macro returns an `Err` value, indicating the first encountered error. Otherwise, it returns `Ok(())`.
///
/// ## Example
///
/// ```rust
/// use libmake::{macro_create_directories, macro_cleanup_directories};
/// use std::path::Path;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let test = Path::new("logs");
/// let test2 = Path::new("logs1");
/// macro_create_directories!(test, test2)?;
/// macro_cleanup_directories!(test, test2);
/// Ok(())
/// }
/// ```
///
/// # See Also
///
/// - [`macro_check_directory`] for checking and creating a single directory
/// - [`macro_cleanup_directories`] for cleaning up directories
///