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
//! Utilities.
//!
//! <div class="warning">
//! The functions, in this module, are <a href="../index.html#functions">wrapped</a>
//! to provide better context in errors.
//! </div>
//!
use crate;
use ;
use OsStr;
use PathBuf;
/// Convert the given path into a [`Utf8PathBuf`](module@camino)
///
/// # Notes
/// * It is better to use
/// [`komichi::utf8_path_buf`](function@crate::utf8_path_buf) because it
/// uses [`KomichiError`](enum@crate::error::KomichiError)
///
/// * This is a convenience function that wraps
/// [`Utf8PathBuf::try_from`](function@camino::Utf8PathBuf::try_from)
///
/// # Arguments
/// * `path` - the string-like path to be converted
///
/// # Errors
///
/// * [`PathConvertError`] - will be returned if unable to convert the given
/// path. The given path must safely convert to UTF-8.
///
/// Retrieve and return the home directory for the current user.
///
/// # Notes
/// * It is better to use [`komichi::get_home`](function@crate::get_home)
/// because it uses [`KomichiError`](enum@crate::error::KomichiError) and
/// no `func` argument is needed.
///
/// # Arguments
/// * `func` - the function to use to retrieve the home directory.
///
/// # Errors
///
/// * [`HomeError`] - will be returned if:
/// * unable to locate the user's home directory; or
/// * the retrieved home directory contains non UTF-8 encoded characters; or
/// * the retrieved home directory is not an absolute path.
///
/// Return the given path or use the user's home directory if it's absolute.
///
/// # Notes
/// * It is better to use [`komichi::use_home`](fn@crate::use_home)
/// because it uses [`KomichiError`](enum@crate::error::KomichiError) and
/// no `func` argument is needed.
///
/// # Arguments
/// * `path` - the path to use
/// * `func` - the function to use to retrieve the home directory.
///
/// # Errors
///
/// * [`HomeError`] - will be returned if:
/// * unable to locate the user's home directory; or
/// * the retrieved home directory contains non UTF-8 encoded characters; or
/// * the retrieved, or given, home directory is not an absolute path.
///
/// Retrieve and return the current working directory ("CWD") for the current
/// user.
///
/// # Notes
/// * It is better to use [`komichi::get_cwd`](fn@crate::get_cwd)
/// because it uses [`KomichiError`](enum@crate::error::KomichiError) and
/// no `func` argument is needed.
///
/// # Arguments
/// * `func` - the function to use to retrieve the CWD.
///
/// # Errors
///
/// * [`CwdError`] - will be returned if:
/// * unable to locate the current user's CWD; or
/// * the retrieved CWD has a file-error e.g. Does not exist, permissions,
/// etc., or
/// * the retrieved CWD contains non UTF-8 encoded characters
///
/// Return the given path or use the current user's current working directory
/// ("CWD") if it's an absolute path.
///
/// # Notes
/// * It is better to use [`komichi::use_cwd`](fn@crate::use_cwd) because it
/// uses [`KomichiError`](enum@crate::error::KomichiError) and no `func`
/// argument is needed.
///
/// # Arguments
/// * `path` - the path to use
/// * `func` - the function to use to retrieve the CWD.
///
/// # Errors
///
/// * [`CwdError`] - will be returned if:
/// * unable to locate the current user's CWD; or
/// * the retrieved CWD has a file-error e.g. Does not exist, permissions,
/// etc., or
/// * the retrieved CWD contains non UTF-8 encoded characters; or
/// * the retrieved, or given, CWD is not an absolute path.
///
/// Replace the user's home directory, in the given path, with a tilde.
///
/// The general use case is to clean up paths in error messages so that
/// the user's path information will not make it to the screen.
///
/// # Arguments
/// * `path` - A string-like reference to a [`str`] containing the path
/// to be scrubbed.
/// * `home` - An [`Option`] containing a string-like reference to a [`str`]
/// containing the home directory. The home directory will be queried
/// from the system if the value is [`None`].
///
/// # Example
/// ```
/// use komichi::scrub_path;
///
/// let path = "/a/b/c/d/e/f/g";
/// let home = "/a/b";
/// let expect = "~/c/d/e/f/g".to_string();
/// let result = scrub_path(&path, Some(&home));
/// assert_eq!(expect, result);
/// ```
/// Used to replace a user's home directory, in the given path, with a tilde.
///
/// The general use case is to clean up paths in error messages so that
/// the user's path information will not make it to the screen.
///
/// # Example
///
/// This will default to using the current-user's home directory. If the
/// user's home directory doesn't match the full path will be returned.
///
/// ```
/// use komichi::Scrub;
/// let out = Scrub::new().path("/home/i-mziB!6ct/.local/bin");
/// assert_eq!(out, "/home/i-mziB!6ct/.local/bin".to_string());
/// ```
/// However this can be set to use the home directory of a user other
/// that the current-user:
///
/// ```
/// use komichi::Scrub;
/// let mut scrub = Scrub::new();
/// scrub.set_home("/home/i-mziB!6ct");
/// let out = scrub.path("/home/i-mziB!6ct/.local/bin");
/// assert_eq!(out, "~/.local/bin".to_string());
///
/// ```