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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! # 小道 Komichi
//!
//! `komichi` is a collection of tools to make working with file-system paths
//! more convenient.
//!
//! ## Features
//! Some notable features of `komichi`:
//! * Uses [`camino`](module@camino) paths so that an application,
//! using this crate, can treat paths like normal Rust string-like types.
//! * [`EnvVal`] provides the ability to retrieve environment variable
//! values and use a default value if the environment variable does NOT
//! exist or have a value.
//! * [`ExpandPath`] provides a relatively-fast ability to expand unicode-paths
//! that:
//! * may contain BASH-like variables; and
//! * may start with a tilde; and
//! * may not be absolute.
//! * [`ExpandText`] provides a relatively-fast ability to expand
//! given text or text from a file (using a callback function) that:
//! * may contain BASH-like curly-bracket variables (aka identifiers)
//! * [`ExpandTextWith`] provides a relatively-fast ability to expand
//! given text or text from a file (using the [`Fetcher`](expand::text::Fetcher) trait) that:
//! * may contain BASH-like curly-bracket variables (aka identifiers)
//! * [`LocalDirectories`] can provide application local (`$HOME`) path directory
//! locations for:
//! * cache files
//! * config files
//! * data files
//! * log files
//! * state files
//! * [`SystemDirectories`] can provide application system path directories
//! locations for:
//! * system application cache files
//! * system application config files
//! * system application data files
//! * system application log files
//! * system application state files
//! * system application install files
//!
//!
use ;
use home_dir;
use OsStr;
pub use EnvVal;
pub use ExpandPath;
pub use ExpandText;
pub use ExpandTextWith;
pub use Fetcher;
pub use LocalDirectories;
pub use Scrub;
use KomichiError;
pub use SystemDirectories;
pub use scrub_path;
/// Return the current user's home directory as a
/// [`Utf8PathBuf`](struct@camino::Utf8PathBuf).
///
/// # Errors
///
/// * [`KomichiError`] - 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.
///
/// # Example
///
/// ```
/// use komichi::get_home;
/// let value = get_home().unwrap();
/// assert!(!value.to_string().is_empty());
/// ```
///
///
/// Return the given home directory or the current user's home directory
/// as a [`Utf8PathBuf`](struct@camino::Utf8PathBuf).
///
/// # Arguments
///
/// * `path` - A string-like object that can be referenced as a
/// [`Utf8Path`](struct@camino::Utf8PathBuf).
///
/// # Errors
///
/// * [`KomichiError`] - 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.
///
/// # Example
///
/// ```
/// use camino::Utf8PathBuf;
/// use komichi::use_home;
/// let value = use_home(Some("/a/b/c")).unwrap();
/// let expect = Utf8PathBuf::from("/a/b/c");
/// assert_eq!(expect, value);
/// ```
///
/// Return the current user's current working directory ("CWD") as a
/// [`Utf8PathBuf`](struct@camino::Utf8PathBuf).
///
/// # Errors
///
/// * [`KomichiError`] - 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.
///
/// # Example
///
/// ```
/// use komichi::get_cwd;
/// let value = get_cwd().unwrap();
/// assert!(!value.to_string().is_empty());
/// ```
/// Return the given path or the current user's current working directory
/// ("CWD") as a [`Utf8PathBuf`](struct@camino::Utf8PathBuf).
///
/// # Arguments
///
/// * `path` - A string-like object that can be referenced as a
/// [`Utf8Path`](struct@camino::Utf8Path).
///
/// # Errors
///
/// * [`KomichiError`] - 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.
///
/// # Example
///
/// ```
/// use camino::Utf8PathBuf;
/// use komichi::use_cwd;
/// let value = use_cwd(Some("/a/b/c")).unwrap();
/// let expect = Utf8PathBuf::from("/a/b/c");
/// assert_eq!(expect, value);
/// ```
/// Convert the given path into a [`Utf8PathBuf`](struct@camino::Utf8PathBuf).
///
/// # Arguments
///
/// * `path` - Any string-like object that can be referenced as a
/// [`std::ffi::OsStr`].
///
/// # Notes
/// * This is a convenience function that wraps
/// [`Utf8PathBuf::try_from`](struct@camino::Utf8PathBuf)
///
/// # Errors
///
/// * [`KomichiError`] - will be returned if unable to convert the given
/// path. The given path must safely convert to UTF-8.
///
/// # Example
///
/// ```
/// use camino::Utf8PathBuf;
/// use komichi::utf8_path_buf;
/// let value = utf8_path_buf("a/b/c").unwrap();
/// let expect = Utf8PathBuf::from("a/b/c");
/// assert_eq!(expect, value);
/// ```
/// Expand the given `path` with the given function/callback.
///
/// # Notes
/// * Any identifiers contained in the given path which cannot be expanded.
/// * This function will NOT expand a tilde or prepend the current working
/// directory if the given `path` is not absolute.
///
/// # Arguments
///
/// * `path` - Any string-like object that can be referenced as a
/// [`str`].
/// * `fetch` - The function/callback that will retrieve a value for a
/// given identifier key.
///
/// # Errors
/// [`KomichiError`] will be returned if:
/// * if the given `path` is empty; or,
/// * the given `path` contains an open curly bracket `{` with no preceding
/// dollar sign `$`; or,
/// * the given `path` contains an identifier that has an invalid start
/// character; or,
/// * the given `path` contains an identifier that is missing a closing
/// curly bracket `}`; or,
/// * the given `path` contains an invalid character after a starting
/// tilde `~`.
///
/// # Example
/// ```
/// use komichi::expand_path_with;
/// use camino::Utf8PathBuf;
///
/// fn fetcher(key: &str) -> Option<String> {
/// match key {
/// "ONE" => Some(String::from("1")),
/// "TWO" => Some(String::from("2")),
/// _ => None
/// }
/// }
///
/// let path = "/1${ONE}/${TWO}/${THREE}";
/// let result = expand_path_with(&path, fetcher).unwrap();
/// let expect = Utf8PathBuf::from("/11/2/${THREE}");
/// assert_eq!(expect, result);
/// ```
///
/// Strictly expand the given `path` with the given function/callback.
///
/// # Notes
/// * This differs from [`expand_path_with`] in that an
/// [`KomichiError`] will be returned on any identifiers that cannot be expanded
/// by the given function/callback.
/// * This function will NOT expand a tilde or prepend the current working
/// directory if the given `path` is not absolute.
///
/// # Arguments
///
/// * `path` - Any string-like object that can be referenced as a
/// [`str`].
/// * `fetch` - The function/callback that will retrieve a value for a
/// given identifier key.
///
/// # Errors
/// [`KomichiError`] will be returned if:
/// * if the given `path` is empty; or,
/// * the given `path` contains an open curly bracket `{` with no preceding
/// dollar sign `$`; or,
/// * the given `path` contains an identifier that has an invalid start
/// character; or,
/// * the given `path` contains an identifier that is missing a closing
/// curly bracket `}`; or,
/// * the given `path` contains an invalid character after a starting
/// tilde `~`; or,
/// * if the given `path` contains an identifier which cannot be
/// expanded by the given `fetch` callback/function.
///
/// # Example
/// ```
/// use komichi::expand_path_strict_with;
/// use camino::Utf8PathBuf;
///
/// fn fetcher(key: &str) -> Option<String> {
/// match key {
/// "ONE" => Some(String::from("1")),
/// "TWO" => Some(String::from("2")),
/// _ => None
/// }
/// }
///
/// let path = "/1${ONE}/${TWO}";
/// let result = expand_path_strict_with(&path, fetcher).unwrap();
/// let expect = Utf8PathBuf::from("/11/2");
/// assert_eq!(expect, result);
/// ```
///
/// Expand a starting tilde in the given `path` to the home directory.
///
/// If the given `path` does not contain a starting tilde, then it will
/// be returned.
/// .
/// Arguments:
/// * `path` - a string-like reference to a [`str`] containing the
/// path that will be expanded.
/// * `home` - a string-like reference to a
/// [`Utf8Path`](struct@camino::Utf8Path) containing the desired path
/// to the home directory. If `None` is given
/// then the current user's home directory will be used.
///
/// # Errors
///
/// [`KomichiError`] will be returned if:
///
/// * the given `home` is not an absolute path; or,
/// * the current user's home directory could not be found; or,
/// * if the given `path` is empty; or,
/// * the given `path` contains an open curly bracket `{` with no preceding
/// dollar sign `$`; or,
/// * the given `path` contains an identifier that has an invalid start
/// character; or,
/// * the given `path` contains an identifier that is missing a closing
/// curly bracket `}`; or,
/// * the given `path` contains an invalid character after a starting
/// tilde `~`.
///
/// Prepend the current working directory ("CWD") to the given `path`.
///
/// If the given `path` is absolute the it will be returned.
///
/// Arguments:
/// * `path` - a string-like reference to a [`str`] containing the
/// path that will be expanded.
/// * `cwd` - a string-like reference to a
/// [`Utf8Path`](struct@camino::Utf8Path) containing the desired CWD.
/// If `None` is given then the current
/// user's current-working-directory will be used.
///
/// # Errors
///
/// [`KomichiError`] will be returned if:
///
/// * the given `cwd` is not an absolute path; or,
/// * the current user's CWD could not be found; or,
/// * the current user's CWD has permission issues; or,
/// * if the given `path` is empty; or,
/// * the given `path` contains an open curly bracket `{` with no preceding
/// dollar sign `$`; or,
/// * the given `path` contains an identifier that has an invalid start
/// character; or,
/// * the given `path` contains an identifier that is missing a closing
/// curly bracket `}`; or,
/// * the given `path` contains an invalid character after a starting
/// tilde `~`.
///
/// Return the given path with all its intermediate components normalized,
/// without performing I/O.
///
/// This is modified, to use [`camino`], from the crate
/// [`normalize_path`](https://docs.rs/normalize-path/latest/normalize_path/)
///
/// # Arguments
///
/// * `path` - A string-like reference to a [`str`] containing the path
/// to be normalized
///
/// # Example
///
/// ```
/// use komichi::normalize_path;
/// use camino::Utf8PathBuf;
/// let exp = Utf8PathBuf::from("/a/b/c/d");
/// let val = normalize_path("/a//b///c/d");
/// assert_eq!(exp, val);
/// ```
///
/// Expand any environment variables in the given `path`
///
/// # Arguments
///
/// * `path` - A string-like reference to a [`str`] containing the path
/// to be expanded.
///
/// # Errors
/// [`KomichiError`] will be returned if:
/// * if the given `path` is empty; or,
/// * the given `path` contains an open curly bracket `{` with no preceding
/// dollar sign `$`; or,
/// * the given `path` contains an identifier that has an invalid start
/// character; or,
/// * the given `path` contains an identifier that is missing a closing
/// curly bracket `}`; or,
/// * the given `path` contains an invalid character after a starting
/// tilde `~`; or,
/// * if the given `path` contains an identifier which cannot be
/// expanded by the given `fetch` callback/function.
///
/// # Example
///
/// ```
/// use komichi::expand_path_with_environ;
/// use std::env::set_var;
///
/// // Only using unsafe for the example. Most likely the environment-variables
/// // will be set via the shell.
/// unsafe {
/// set_var("AVAR", "test");
/// }
/// let path = "/a/${AVAR}/b";
/// let result = expand_path_with_environ(&path).unwrap();
/// assert!(result.as_str().len() > 0);
/// ```
///
/// Return a [`LocalDirectories`] struct that has functions
/// to get various local application paths for the given application name.
///
/// # Arguments
/// * `app_name` - The name of the application used when creating the paths.
/// It is recommended that this value contain no spaces.
/// * `home` - An [`Option`] containing string-like reference to a
/// [`Utf8Path`](struct@camino::Utf8Path). This will be the path to the
/// user's home directory. This function will search for, and use, the
/// current user's home directory when given a value of `None`.
///
/// # Errors
///
/// * [`KomichiError`] - 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.
///
/// # Example
///
/// ```
/// use komichi::get_local_application_paths;
///
/// match get_local_application_paths::<&str>("myapp", None) {
/// Ok(local) => {
/// println!("{}", local.get_cache_home());
/// println!("{}", local.get_config_home());
/// println!("{}", local.get_data_home());
/// println!("{}", local.get_log_home());
/// println!("{}", local.get_state_home());
/// },
/// Err(_) => {}
/// }
///
/// ```
/// Return a [`SystemDirectories`](struct@crate::system::SystemDirectories)
/// struct which can be used to get system directories for a given
/// application-name as if the application has been installed on the system
/// but not installed locally (in the user's home directory)
///
/// # Arguments
/// * `app_name` - The name of the application used when generating the path
/// locations. It is recommended that this value contain no spaces.
///
/// # Example
///
/// ```
/// use komichi::get_system_application_paths;
/// let application_paths = get_system_application_paths("myapp");
/// println!("cache home: {}", application_paths.get_cache_home());
/// println!("config home: {}", application_paths.get_config_home());
/// println!("data home: {}", application_paths.get_data_home());
/// println!("log home: {}", application_paths.get_log_home());
/// println!("state home: {}", application_paths.get_state_home());
/// println!("opt home: {}", application_paths.get_opt_home());
///
/// ````
/// Return the given text, containing bash-like-curly variables that are
/// expanded with values from the given callback function.
///
/// The text can contain BASH-like variables with each variable using
/// the curly syntax (e.g. `A dog named ${DOG_NAME}`). BASH-like variables
/// that do not contain curly brackets (e.g. `$DOG_NAME`) will become
/// identifier errors.
///
/// The `$` character can be escaped by using two `$` characters e.g. `$$`.
///
/// Any identifier that does not have a value, as given by the callback
/// function will be skipped. Meaning that the BASH-like variable will be
/// put back into the output.
///
/// # Arguments
///
/// * `fetch` - The function/callback that will retrieve a value for a
/// given identifier key.
/// * `text` - A string-slice containing the text-to-be-expanded
///
/// # Errors
/// A [`KomichiError`] - will be returned if:
/// * there are any syntactical errors with any BASH-like variables:
/// * Non-ASCII characters between the curly-brackets,
/// * Non-alpha-numeric-underscore characters between the curly-brackets,
/// * No characters/empty between the curly-brackets,
/// * Missing an opening curly-bracket, after the `$`,
/// * Missing a closing curly-bracket, after the identifier,
/// * Identifier starts with a number. Can only start with an underscore
/// or an ASCII-letter,
/// * Invalid identifier name when the identifier name contains spaces
/// between parts of the identifier name (e.g. `${dog name}`).
///
/// # Example
/// ```
/// use komichi::expand_text_with;
///
/// fn fetcher(key: &str) -> Option<String> {
/// match key {
/// "speed" => Some(String::from("quick")),
/// "color" => Some(String::from("brown")),
/// "animal" => Some(String::from("dog")),
/// _ => None
/// }
/// }
/// let expect = "“The quick brown fox jumps over the lazy dog”";
/// let arg = "“The ${speed} ${color} fox jumps over the lazy ${animal}”";
/// let result = expand_text_with(arg, fetcher).unwrap();
/// assert_eq!(result, expect);
/// ```
///
/// Return the given text, containing bash-like-curly variables that are
/// expanded with values from the given callback-function. And return
/// an [`KomichiError`] when a value cannot be found by the given
/// callback-function
///
/// The text can contain BASH-like variables with each variable using
/// the curly syntax (e.g. `A dog named ${DOG_NAME}`). BASH-like variables
/// that do not contain curly brackets (e.g. `$DOG_NAME`) will become
/// identifier errors.
///
/// The `$` character can be escaped by using two `$` characters e.g. `$$`.
///
/// # Arguments
///
/// * `fetch` - The function/callback that will retrieve a value for a
/// given identifier key.
/// * `text` - A string-slice containing the text-to-be-expanded
///
/// # Errors
/// A [`KomichiError`] - will be returned if:
/// * there are any syntactical errors with any BASH-like variables:
/// * Non-ASCII characters between the curly-brackets,
/// * Non-alpha-numeric-underscore characters between the curly-brackets,
/// * No characters/empty between the curly-brackets,
/// * Missing an opening curly-bracket, after the `$`,
/// * Missing a closing curly-bracket, after the identifier,
/// * Identifier starts with a number. Can only start with an underscore
/// or an ASCII-letter,
/// * Invalid identifier name when the identifier name contains spaces
/// between parts of the identifier name (e.g. `${dog name}`),
/// * if the value for any identifier cannot be returned by the given
/// callback-function.
///
/// # Example
/// ```
/// use komichi::expand_text_strict_with;
///
/// fn fetcher(key: &str) -> Option<String> {
/// match key {
/// "speed" => Some(String::from("quick")),
/// "color" => Some(String::from("brown")),
/// "animal" => Some(String::from("dog")),
/// _ => None
/// }
/// }
/// let expect = "“The quick brown fox jumps over the lazy dog”";
/// let arg = "“The ${speed} ${color} fox jumps over the lazy ${animal}”";
/// let result = expand_text_strict_with(arg, fetcher).unwrap();
/// assert_eq!(result, expect);
/// ```
///