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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use std::path::{Path, PathBuf};
/// Extension trait for `PathBuf` to help with common
/// task on directories.
pub trait BaseDirsEx {
/// Recursively create a directory and all of its parent components if they are missing.
/// If the directory already exists, this function does nothing.
///
/// # Returns
/// The path of the directory that was created.
///
/// # Errors
/// If the directory creation fails, the error is returned.
///
/// # Example
/// ```no_run
/// use cross_xdg::{BaseDirs, BaseDirsEx};
/// use std::path::PathBuf;
///
/// let base_dirs = BaseDirs::new().unwrap();
/// let my_sub_config_dir = base_dirs.config_home()
/// .join("my_sub_dir")
/// .create()
/// .unwrap();
/// ```
fn create(&self) -> Result<PathBuf, std::io::Error>;
/// Copy the content of a file to the directory.
/// If the source is a file, it will be copied to the directory.
/// If the file already exists, it will be overwritten.
/// The destination directory will be created if it does not exist.
///
/// # Parameters
/// - `name`: The name of the file to create.
/// - `src`: The source file to copy.
///
/// # Returns
/// The path of the file where the source was copied.
///
/// # Examles
/// ```no_run
/// use cross_xdg::{BaseDirs, BaseDirsEx};
/// use std::path::PathBuf;
///
/// let base_dirs = BaseDirs::new().unwrap();
///
/// let src = PathBuf::from("/tmp/src.txt");
/// let my_sub_config_dir = base_dirs.config_home()
/// .join("my_sub_dir")
/// .copy_over("dest.txt", src)
/// .unwrap();
/// ```
fn copy_over(&self, name: &str, src: impl AsRef<Path>) -> Result<PathBuf, std::io::Error>;
/// Copy the content of a file to the directory.
/// If the source is a file, it will be copied to the directory.
/// If the file already exists, it will *not* be overwritten.
/// The destination directory will be created if it does not exist.
/// The file will be created with the name specified in the `name` parameter.
///
/// # Parameters
/// - `name`: The name of the file to create.
/// - `src`: The source file to copy.
///
/// # Returns
/// The path of the file where the source was copied.
///
/// # Examles
/// ```no_run
/// use cross_xdg::{BaseDirs, BaseDirsEx};
/// use std::path::PathBuf;
///
/// let base_dirs = BaseDirs::new().unwrap();
/// let src = PathBuf::from("/tmp/src.txt");
/// let my_sub_config_dir = base_dirs.config_home()
/// .join("my_sub_dir")
/// .copy("dest.txt", src)
/// .unwrap();
/// ```
fn copy(&self, name: &str, src: impl AsRef<Path>) -> Result<PathBuf, std::io::Error>;
/// Write the content of a byte slice to a file in the directory.
/// The destination directory will be created if it does not exist.
/// If the file already exists, it will be overwritten.
/// The file will be created with the name specified in the `name` parameter.
///
/// # Parameters
/// - `name`: The name of the file to create.
/// - `src`: The content to write to the file.
///
/// # Returns
/// The path of the file that was created.
///
/// # Examples
/// ```no_run
/// use cross_xdg::{BaseDirs, BaseDirsEx};
/// use std::path::PathBuf;
///
/// let base_dirs = BaseDirs::new().unwrap();
/// let my_file = base_dirs.config_home()
/// .write_over("hello.txt", b"Hello, World!")
/// .unwrap();
/// ```
fn write_over(&self, name: &str, src: &[u8]) -> Result<PathBuf, std::io::Error>;
/// Write the content of a byte slice to a file in the directory.
/// The destination directory will be created if it does not exist.
/// If the file already exists, it will not be overwritten.
/// The file will be created with the name specified in the `name` parameter.
///
/// # Parameters
/// - `name`: The name of the file to create.
/// - `src`: The content to write to the file.
///
/// # Returns
/// The path of the file that was created.
///
/// # Examples
/// ```no_run
/// use cross_xdg::{BaseDirs, BaseDirsEx};
/// use std::path::PathBuf;
///
/// let base_dirs = BaseDirs::new().unwrap();
/// let my_file = base_dirs.config_home()
/// .write("hello.txt", b"Hello, World!")
/// .unwrap();
/// ```
fn write(&self, name: &str, src: &[u8]) -> Result<PathBuf, std::io::Error>;
}
impl<T: AsRef<Path>> BaseDirsEx for T {
fn create(&self) -> Result<PathBuf, std::io::Error> {
std::fs::create_dir_all(self)?;
Ok(self.as_ref().to_path_buf())
}
fn copy_over(&self, name: &str, src: impl AsRef<Path>) -> Result<PathBuf, std::io::Error> {
let dir = self.create()?;
let full_path = dir.join(name);
std::fs::copy(&src, &full_path)?;
Ok(dir.join(full_path))
}
fn write_over(&self, name: &str, src: &[u8]) -> Result<PathBuf, std::io::Error> {
let dir = self.create()?;
let full_path = dir.join(name);
std::fs::write(&full_path, src)?;
Ok(full_path)
}
fn copy(&self, name: &str, src: impl AsRef<Path>) -> Result<PathBuf, std::io::Error> {
let dest = self.as_ref().join(name);
if dest.exists() {
Ok(dest)
} else {
self.copy_over(name, src)
}
}
fn write(&self, name: &str, src: &[u8]) -> Result<PathBuf, std::io::Error> {
let dest = self.as_ref().join(name);
if dest.exists() {
Ok(dest)
} else {
self.write_over(name, src)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{BaseDirs, test_helper::set_var};
use serial_test::serial;
#[test]
#[serial]
fn create_sub_dir() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let my_sub_config_dir = base_dirs.config_home().join("my_sub_dir").create().unwrap();
assert!(my_sub_config_dir.starts_with("/tmp/config/my_sub_dir"));
std::fs::remove_dir_all(my_sub_config_dir).unwrap();
}
#[test]
#[serial]
fn create_sub_dir_with_prefix() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::with_prefix("prefix").unwrap();
let my_sub_config_dir = base_dirs.config_home().create().unwrap();
assert!(my_sub_config_dir.starts_with("/tmp/config/prefix"));
std::fs::remove_dir_all(my_sub_config_dir).unwrap();
}
#[test]
#[serial]
fn copy_over_file() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let src = PathBuf::from("/tmp/src.txt");
std::fs::write(&src, b"Hello, World!").unwrap();
let my_sub_config_dir = base_dirs
.config_home()
.join("my_sub_dir")
.copy_over("hello.txt", &src)
.unwrap();
assert!(my_sub_config_dir.starts_with("/tmp/config/my_sub_dir/hello.txt"));
std::fs::remove_dir_all(my_sub_config_dir.parent().unwrap()).unwrap();
}
#[test]
#[serial]
fn copy_file_not_exists() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let src = PathBuf::from("/tmp/src.txt");
std::fs::write(&src, b"Hello, World!").unwrap();
let my_sub_config_dir = base_dirs
.config_home()
.join("my_sub_dir")
.copy("hello.txt", &src)
.unwrap();
assert!(my_sub_config_dir.starts_with("/tmp/config/my_sub_dir/hello.txt"));
std::fs::remove_dir_all(my_sub_config_dir.parent().unwrap()).unwrap();
}
#[test]
#[serial]
fn copy_file_exists() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::with_prefix("my_sub_dir").unwrap();
let my_sub_config_dir = base_dirs.config_home().create().unwrap();
let existing_file = my_sub_config_dir.join("old.txt");
std::fs::write(&existing_file, b"Old Content").unwrap();
let new_file = my_sub_config_dir.join("new.txt");
std::fs::write(&new_file, b"New Content").unwrap();
let my_sub_config_dir = base_dirs.config_home().copy("old.txt", &new_file).unwrap();
let content = std::fs::read(&existing_file).unwrap();
assert_eq!(content, b"Old Content");
std::fs::remove_dir_all(my_sub_config_dir.parent().unwrap()).unwrap();
}
#[test]
#[serial]
fn write_over_file_exists() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let my_file = base_dirs
.config_home()
.write_over("hello.txt", b"First")
.unwrap();
assert!(my_file.starts_with("/tmp/config/hello.txt"));
// Write again, should overwrite
let my_file2 = base_dirs
.config_home()
.write_over("hello.txt", b"Second")
.unwrap();
assert_eq!(my_file, my_file2);
let content = std::fs::read(&my_file).unwrap();
assert_eq!(content, b"Second");
std::fs::remove_file(my_file).unwrap();
}
#[test]
#[serial]
fn write_over_file_not_exists() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let my_file = base_dirs
.config_home()
.write_over("hello.txt", b"Hello, World!")
.unwrap();
assert!(my_file.starts_with("/tmp/config/hello.txt"));
std::fs::remove_file(my_file).unwrap();
}
#[test]
#[serial]
fn write_file_exists() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let my_file = base_dirs
.config_home()
.write_over("hello.txt", b"First")
.unwrap();
// Write using `.write`, should NOT overwrite
let my_file2 = base_dirs
.config_home()
.write("hello.txt", b"Second")
.unwrap();
assert_eq!(my_file, my_file2);
let content = std::fs::read(&my_file).unwrap();
assert_eq!(content, b"First"); // Should still be "First"
std::fs::remove_file(my_file).unwrap();
}
#[test]
#[serial]
fn write_file_nots_exists() {
set_var("XDG_CONFIG_HOME", "/tmp/config");
let base_dirs = BaseDirs::new().unwrap();
let path = base_dirs
.config_home()
.write("hello.txt", b"Hello, World!")
.unwrap();
assert!(path.starts_with("/tmp/config/hello.txt"));
let content = std::fs::read(&path).unwrap();
assert_eq!(content, b"Hello, World!");
std::fs::remove_file(path).unwrap();
}
}