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
723
724
725
726
727
728
729
//! This crate helps with cache directories creation in a system-agnostic way.
//!
//! The [`CacheDirConfig`] type helps define the desired location(s) where attempts to create
//! the cache directory should be made and also helps with the creation itself.
//!
//! The [`CacheDir`] type holds the path to the created cache directory, obtained with the help of
//! [`CacheDirConfig`], if it succeeded to create the directory.
//!
//! [`CacheDir`] derefs to [`PathBuf`] and implements most of the same traits that [`PathBuf`] does.
//!
//! Code examples:
//!
//! - [`Creating an application cache with app_cache_path(custom path)`]
//!
//! - [`Creating an application cache without app_cache_path`]
//!
//! - [`Creating a user cache(the default)`]
//!
//! - [`Creating a system-wide cache`]
//!
//! - [`Creating a tmp cache`]
//!
//! - [`Creating a memory cache`]
//!
//! - [`Using all default fallbacks(without creating an application cache)`]
//!
//! - [`Using all default fallbacks(with automatic creation of the application cache)`]
//!
//! [`Jump to the list of structs`]
//!
//! [`CacheDir`]: struct.CacheDir.html
//! [`CacheDirConfig`]: struct.CacheDirConfig.html
//! [`PathBuf`]: https://doc.rust-lang.org/std/path/struct.PathBuf.html
//! [`Creating an application cache with app_cache_path(custom path)`]: index.html#creating-an-application-cache-with-app_cache_pathcustom-path
//! [`Creating an application cache without app_cache_path`]: index.html#creating-an-application-cache-without-app_cache_path
//! [`Creating a user cache(the default)`]: index.html#creating-a-user-cachethe-default
//! [`Creating a system-wide cache`]: index.html#creating-a-system-wide-cache
//! [`Creating a tmp cache`]: index.html#creating-a-tmp-cache
//! [`Creating a memory cache`]: index.html#creating-a-memory-cache
//! [`Using all default fallbacks(without creating an application cache)`]: index.html#using-all-default-fallbackswithout-creating-an-application-cache
//! [`Using all default fallbacks(with automatic creation of the application cache)`]: index.html#using-all-default-fallbackswith-automatic-creation-of-the-application-cache
//! [`Jump to the list of structs`]: index.html#structs
//! [`To the top ⤴`]: index.html
//!
//! # Examples
//!
//! ### Creating an `application cache` **with** `app_cache_path`(custom path)
//! [`To the top ⤴`]
//!
//! ```
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//! use std::env::current_dir;
//!
//! let current_dir = current_dir().unwrap();
//! let app_cache = CacheDirConfig::new("example/path")
//!                                .app_cache_path(current_dir.as_path())
//!                                .get_cache_dir().unwrap();
//!
//! assert_eq!(current_dir.join("example/path"),
//!            app_cache.into_path_buf());
//! ```
//!
//! ### Creating an `application cache` **without** `app_cache_path`
//! [`To the top ⤴`]
//!
//! ```
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//! use std::env::current_dir;
//!
//! let app_cache = CacheDirConfig::new("example")
//!                                .app_cache(true)
//!                                .get_cache_dir().unwrap();
//!
//! let current_dir = current_dir().unwrap();
//! if cfg!(not(windows)) {
//!     assert_eq!(current_dir.join(".cache").join("example"),
//!                app_cache.into_path_buf());
//! } else {
//!     assert_eq!(current_dir.join("Cache").join("example"),
//!                app_cache.into_path_buf());
//! }
//! ```
//!
//! ### Creating a `user cache`(the default)
//! [`To the top ⤴`]
//!
//! ```
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//! use std::env;
//!
//! // User cache is the default
//! let user_cache: PathBuf = CacheDirConfig::new("example")
//!                                          .get_cache_dir().unwrap()
//!                                          .into(); // `CacheDir` implements `Into<PathBuf>`
//!
//! let assert_user_cache = CacheDirConfig::new("example")
//!                                        .user_cache(true)
//!                                        .get_cache_dir().unwrap()
//!                                        .into_path_buf(); // -> PathBuf
//!
//! assert_eq!(assert_user_cache, user_cache);
//!
//! let expected_cache_dir: PathBuf;
//! #[cfg(any(unix, target_os = "redox"))]
//! {
//!     let home_dir = env::home_dir();
//!
//!     #[cfg(not(any(target_os = "emscripten", target_os = "macos")))]
//!     {
//!         expected_cache_dir = home_dir.unwrap().join(".cache").join("example");
//!     }
//!
//!     #[cfg(target_os = "emscripten")]
//!     {
//!         expected_cache_dir = if home_dir.is_none() {
//!             PathBuf::from("/var/cache").join("example")
//!         } else {
//!             home_dir.unwrap().join(".cache").join("example")
//!         };
//!     }
//!
//!     #[cfg(target_os = "macos")]
//!     {
//!         expected_cache_dir = home_dir.unwrap().join("Library/Caches").join("example");
//!     }
//! }
//!
//! #[cfg(windows)]
//! {
//!     let local_app_data = PathBuf::from(env::var_os("LOCALAPPDATA").unwrap());
//!     expected_cache_dir = local_app_data.join("example");
//! }
//!
//! assert_eq!(expected_cache_dir, user_cache);
//! ```
//!
//! ### Creating a `system-wide cache`
//! [`To the top ⤴`]
//!
//! ```no_run
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//!
//! let cache_dir = CacheDirConfig::new("example")
//!                                .sys_cache(true)
//!                                .get_cache_dir().unwrap();
//!
//! let expected_cache_dir: PathBuf;
//! #[cfg(unix)]
//! {
//!     #[cfg(not(target_os = "macos"))]
//!     { expected_cache_dir = PathBuf::from("/var/cache").join("example"); }
//!
//!     #[cfg(target_os = "macos")]
//!     { expected_cache_dir = PathBuf::from("/Library/Caches").join("example"); }
//! }
//!
//! #[cfg(windows)]
//! {
//!     use std::env;
//!     let program_data = PathBuf::from(env::var_os("ProgramData").unwrap());
//!     expected_cache_dir = program_data.join("example");
//! }
//!
//! assert_eq!(expected_cache_dir, cache_dir.into_path_buf());
//! ```
//!
//! ### Creating a `tmp cache`
//! [`To the top ⤴`]
//!
//! ```
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//!
//! let cache_dir = CacheDirConfig::new("example")
//!                                .tmp_cache(true)
//!                                .get_cache_dir().unwrap();
//!
//! let expected_cache_dir: PathBuf;
//! #[cfg(unix)]
//! {
//!     // On Unix, we try `/var/tmp` first, because it is more persistent than `/tmp`
//!     expected_cache_dir = PathBuf::from("/var/tmp").join("example");
//! }
//!
//! #[cfg(windows)]
//! {
//!     use std::env::temp_dir;
//!     expected_cache_dir = temp_dir().join("example");
//! }
//!
//! assert_eq!(expected_cache_dir, cache_dir.into_path_buf());
//! ```
//!
//! ### Creating a `memory cache`
//! [`To the top ⤴`]
//!
//! ```no_run
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//!
//! let cache_dir = CacheDirConfig::new("example/path")
//!                                .mem_cache(true)
//!                                .get_cache_dir().unwrap();
//!
//! // In-memory caches are supported only on Linux
//! if cfg!(target_os = "linux") {
//!     // We try `/dev/shm` before `/run/shm`
//!     assert_eq!(PathBuf::from("/dev/shm").join("example/path"),
//!                cache_dir.into_path_buf());
//! }
//! ```
//!
//! ### Using all default fallbacks(without creating an application cache)
//! [`To the top ⤴`]
//!
//! ```
//! use cachedir::CacheDirConfig;
//! use std::path::PathBuf;
//!
//! let short_version = CacheDirConfig::new("example/path")
//!                                    .try_all_caches()
//!                                    .get_cache_dir().unwrap();
//!
//! let verbose_version = CacheDirConfig::new("example/path")
//!                                      .user_cache(true) // Order here
//!                                      .sys_cache(true)  // does
//!                                      .tmp_cache(true)  // not
//!                                      .mem_cache(true)  // matter
//!                                      .get_cache_dir().unwrap(); // This still is the last call
//!
//! assert_eq!(short_version, verbose_version); // `CacheDir` implements `Eq` and `PartialEq`
//! ```
//! This will try to create the cache directory using these options(listed below).<br/>
//! It falls back to the next option if it fails:<br/>
//!
//! 1. User cache(**persistent** and **doesn't require** elevated rights)
//!
//! 2. System-wide cache(**persistent** and **requires** elevated rights)
//!
//! 3. Tmp cache(**somewhat persistent in /var/tmp** on Unix, **doesn't require** elevated rights)
//!
//! 4. Memory cache(**not persistent** between system restarts, **doesn't require** elevated rights)
//!
//! ### Using all default fallbacks(with automatic creation of the application cache)
//! [`To the top ⤴`]
//!
//! ```
//! use cachedir::CacheDirConfig;
//! use std::path::{ Path, PathBuf };
//! use std::env::current_dir;
//!
//! let one_way = CacheDirConfig::new("example/path")
//!                              .app_cache(true)
//!                              .try_all_caches()
//!                              .get_cache_dir().unwrap();
//!
//! let autocreated_dir = if cfg!(not(windows)) {
//!     Path::new(".cache")
//! } else {
//!     Path::new("Cache")
//! };
//!
//! let app_cache_path = current_dir().unwrap().join(autocreated_dir);
//!
//! // It's OK to use `app_cache(true)` here too
//! let another_way = CacheDirConfig::new("example/path")
//!                                  .app_cache_path(app_cache_path.as_path())
//!                                  .try_all_caches()
//!                                  .get_cache_dir().unwrap();
//!
//! assert_eq!(one_way, another_way);
//!
//! // `app_cache_path` overwrites the default that was set in `app_cache(true)`
//! let yet_another_way = CacheDirConfig::new("example/path")
//!                                      .app_cache(true)
//!                                      .app_cache_path("/tmp/other/path")
//!                                      .try_all_caches()
//!                                      .get_cache_dir().unwrap();
//!
//! assert!(another_way != yet_another_way);
//!
//! // `app_cache(true)` does not overwrite the path of `app_cache_path` if it was set
//! let or_this_way = CacheDirConfig::new("example/path")
//!                                  .app_cache_path("/tmp/other/path")
//!                                  .app_cache(true)
//!                                  .try_all_caches()
//!                                  .get_cache_dir().unwrap();
//!
//! assert_eq!(yet_another_way, or_this_way);
//! ```
//! This will try to create the cache directory using these options(listed below).<br/>
//! It falls back to the next option if it fails:<br/>
//!
//! 1. Application cache
//!
//! 2. User cache(**persistent** and **doesn't require** elevated rights)
//!
//! 3. System-wide cache(**persistent** and **requires** elevated rights)
//!
//! 4. Tmp cache(**somewhat persistent in /var/tmp** on Unix, **doesn't require** elevated rights)
//!
//! 5. Memory cache(**not persistent** between system restarts, **doesn't require** elevated rights)
//!
//! [`To the top ⤴`]

use std::path;
use std::io;
use std::ffi::OsStr;

// Contains the os-agnostic `create_cache_dir` function
mod sys_cache;

// Traits implementations for `CacheDir`
mod traits_impls;

/// This structure holds the [`PathBuf`] returned from [`CacheDirConfig`].
///
/// It derefs to [`PathBuf`] and implements most of the same traits as [`PathBuf`].
///
/// # Examples
/// ```
/// # use cachedir::{ CacheDir, CacheDirConfig };
/// # use std::path::{ Path, PathBuf };
/// # use std::ffi::OsStr;
/// let cache_dir: CacheDir = CacheDirConfig::new("example").get_cache_dir().unwrap();
///
/// fn as_path(path: &Path) {}
///
/// fn into_path_buf<P: Into<PathBuf>>(path: P) {
///     let _path_buf: PathBuf = path.into();
/// }
///
/// fn as_ref<P: AsRef<OsStr>>(path: P) {
///     let _os_str: &OsStr = path.as_ref();
/// }
///
/// as_path(cache_dir.as_path());
/// into_path_buf(cache_dir.clone());
/// as_ref(cache_dir.clone());
///
/// println!("{}", cache_dir.display());
/// ```
///
/// [`CacheDirConfig`]: struct.CacheDirConfig.html
/// [`PathBuf`]: https://doc.rust-lang.org/std/path/struct.PathBuf.html
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CacheDir {
    path: path::PathBuf
}

impl CacheDir {
    /// ```
    /// # use cachedir::CacheDirConfig;
    /// # use std::path::PathBuf;
    /// let path: PathBuf = CacheDirConfig::new("example")
    ///                                    .get_cache_dir().unwrap()
    ///                                    .into_path_buf();
    /// ```
    pub fn into_path_buf(self) -> path::PathBuf {
        self.path
    }
}

/// This structure helps configure the desired behavior when attempting to create
/// a cache directory and also creates the directory based on that behavior.
///
/// `CacheDirConfig` prioritizes the most persistent destinations first and then the locations
/// that require the least user rights, when attempting to create a cache directory.<br/><br/>
///
/// The default behavior is to create a `user cache`(won't attempt other cache options).
///
/// If another cache option is passed, then the `user cache` default won't be used any more.
///
/// If only [`app_cache_path`] and/or [`app_cache(true)`] is passed, then only the application cache
/// will be created(there won't be attempts to fallback to other cache options).
///
/// Multiple fallbacks can be used by using the [`app_cache_path`], [`app_cache`], [`user_cache`],
/// [`sys_cache`], [`tmp_cache`], [`mem_cache`] and [`try_all_caches`] functions to configure
/// the behavior.
///
/// The order of attempts looks like this(note that unset cache options are skipped):
///
/// 1. Application cache
///
/// 2. User cache(**persistent** and **doesn't require** elevated rights)
///
/// 3. System-wide cache(**persistent** and **requires** elevated rights)
///
/// 4. Tmp cache(**somewhat persistent in /var/tmp** on Unix, **doesn't require** elevated rights)
///
/// 5. Memory cache(**not persistent** between system restarts, **doesn't require** elevated rights)
///
/// Example: If a user sets [`user_cache(true)`], [`sys_cache(true)`] and [`mem_cache(true)`]
/// than `CaheDirConfig` will attempt to create a cache directory in the `user_cache`, than, if it
/// fails(ex: cache directory not found, missing rights, a file with the same name exists, etc...),
/// it will fall-back to the `sys_cache`, in which case, if this also fails, it will attempt as a
/// last resort to create a cache directory in the `mem_cache`.
///
/// [`app_cache_path`]: struct.CacheDirConfig.html#method.app_cache_path
/// [`app_cache(true)`]: struct.CacheDirConfig.html#method.app_cache
/// [`app_cache`]: struct.CacheDirConfig.html#method.app_cache
/// [`user_cache`]: struct.CacheDirConfig.html#method.user_cache
/// [`user_cache(true)`]: struct.CacheDirConfig.html#method.user_cache
/// [`sys_cache`]: struct.CacheDirConfig.html#method.sys_cache
/// [`sys_cache(true)`]: struct.CacheDirConfig.html#method.sys_cache
/// [`tmp_cache`]: struct.CacheDirConfig.html#method.tmp_cache
/// [`mem_cache`]: struct.CacheDirConfig.html#method.mem_cache
/// [`mem_cache(true)`]: struct.CacheDirConfig.html#method.mem_cache
/// [`try_all_caches`]: struct.CacheDirConfig.html#method.try_all_caches
///
/// # Examples
/// ```
/// # use cachedir::CacheDirConfig;
/// let cache_dir = CacheDirConfig::new("example").get_cache_dir().unwrap();
/// ```
/// This will attempt to create the cache directory `example` in the `User Cache`.<br/>
/// Read [`user_cache`] documentation if you want to find more about the paths used for `User Cache`.
pub struct CacheDirConfig<'a, 'b> {
    cache_name:     &'a path::Path,
    app_cache_path: Option<&'b path::Path>,
    app_cache:      bool,
    // wasted an hour on this, obsessing about "user" not being 3 characters aligned,
    // but "usr" is not very clear(for non-Unix users) and does not sound as well when pronouncing it
    user_cache:     bool,
    sys_cache:      bool,
    tmp_cache:      bool,
    mem_cache:      bool
}

impl<'a, 'b> CacheDirConfig<'a, 'b> {
    /// `cache_name` accepts a path - used to create the cache directory.
    ///
    /// If it *does not exist* at the desired location, `CacheDirConfig` will create it when
    /// calling `get_cache_dir()`, before returning the path to the final cache destination.
    ///
    /// If it *already exists* and it is a directory, `get_cache_dir()` will
    /// return the path to the final cache destination(**note:** in this situation, `CacheDirConfig`
    /// cannot guarantee that you have access to write in the final destination, it just confirms
    /// that the cache directory already exists).
    ///
    /// # Examples
    /// ```
    /// use cachedir::CacheDirConfig;
    /// let cache_config = CacheDirConfig::new("some/path");
    /// ```
    pub fn new<S: AsRef<OsStr> + ?Sized>(cache_name: &'a S) -> CacheDirConfig<'a, 'b> {
        CacheDirConfig {
            cache_name:     path::Path::new(cache_name),
            app_cache_path: None,
            app_cache:      false,
            user_cache:     false,
            sys_cache:      false,
            tmp_cache:      false,
            mem_cache:      false
        }
    }

    /// This function allows to choose a custom path where the cache directory should be created.
    ///
    /// If it *does not exist*, `CacheDirConfig` will attempt to create it.
    ///
    /// If none of the other cache options are selected, this will be the only directory
    /// where `CacheDirConfig` will attempt to create the cache directory.
    ///
    /// Using this function automatically switches `app_cache` to `true`. It can manually be
    /// disabled later by calling `app_cache(false)` on this `CacheDirConfig` object.
    ///
    /// # Examples
    /// ```no_run
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .app_cache_path("/application/cache")
    ///                                .get_cache_dir();
    /// ```
    pub fn app_cache_path<S: AsRef<OsStr> + ?Sized>(&mut self,
                                                    path: &'b S) -> &mut CacheDirConfig<'a, 'b> {
        self.app_cache_path = Some(path::Path::new(path));
        self.app_cache      = true;
        self
    }

    /// This function tells `CacheDirConfig` if it should attempt to create
    /// an application cache directory.
    ///
    /// ### Non-Windows
    /// If `app_cache_path` is not passed, `CacheDirConfig` will attempt to create the
    /// application cache based on the *current directory + ".cache"*
    ///
    /// If the directory *does not exist*, `CacheDirConfig` will attempt to create it.
    ///
    /// ### Windows
    /// If `app_cache_path` is not passed, `CacheDirConfig` will attempt to create the
    /// application cache based on the *current directory + "Cache"*
    ///
    /// If the directory *does not exist*, `CacheDirConfig` will attempt to create it.
    ///
    /// # Examples
    /// ```
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .app_cache(true)
    ///                                .get_cache_dir();
    /// ```
    pub fn app_cache(&mut self, value: bool)  -> &mut CacheDirConfig<'a, 'b> {
        self.app_cache = value;
        if self.app_cache_path.is_none() && self.app_cache {
            self.app_cache_path = if cfg!(not(windows)) {
                Some(path::Path::new(".cache"))
            } else {
                Some(path::Path::new("Cache"))
            };
        }
        self
    }

    /// This function tells `CacheDirConfig` if it should attempt to create
    /// a user cache directory.
    ///
    /// Note that if none of the cache options are passed, than the default
    /// is to use the `User Cache`.
    ///
    /// ### Unix
    /// `CacheDirConfig` will attempt to obtain the `HOME` directory of the user.
    /// If it fails, it will try the next fallback. If a fallback was not configured, it will
    /// return an `std::io::Error` when calling `get_cache_dir`.<br/>
    /// An exception is made for `Emscripten` - in this case it will attempt to create `/var/cache`
    /// as a parent directory for the final cache destination.
    ///
    /// If `HOME` was found, than `CacheDirConfig` will attempt to return or
    /// create(if it is missing) the `.cache` directory from inside the `HOME` directory
    /// on `non-macOS` systems and `Library/Caches` on `macOS`.
    ///
    /// ### Windows
    /// `CacheDirConfig` will attempt to create the cache directory inside these paths(and
    /// fallback to the next if it fails):
    ///
    /// 1. Windows environment variable: `%LOCALAPPDATA%`
    ///
    /// 2. `%APPDATA%`
    ///
    /// 3. Rust's `home_dir` which returns `%HOME%` --ifndef--> `%USERPROFILE%`
    /// --ifndef--> `OS syscall`.<br/>
    /// Since the user's home directory is not a dedicated cache directory, `CacheDirConfig`
    /// will attempt to create the `Cache` directory inside it.
    ///
    /// If it fails, it will try the next fallback. If a fallback was not configured, it will
    /// return an `std::io::Error` when calling `get_cache_dir`.
    ///
    /// ### Redox
    /// `CacheDirConfig` will attempt to obtain the `HOME` directory of the user.
    /// If it fails, it will try the next fallback. If a fallback was not configured, it will
    /// return an `std::io::Error` when calling `get_cache_dir`.
    ///
    /// If `HOME` was found, than `CacheDirConfig` will attempt to return or
    /// create(if it is missing) the `.cache` directory from inside the `HOME` directory.
    ///
    /// # Examples
    /// ```
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .user_cache(true)
    ///                                .get_cache_dir();
    /// ```
    pub fn user_cache(&mut self, value: bool) -> &mut CacheDirConfig<'a, 'b> {
        self.user_cache = value;
        self
    }

    /// This function tells `CacheDirConfig` if it should attempt to create
    /// a system-wide cache directory.<br/>
    /// **Note:** This might require elevated rights(ex: `superuser`, `Admin`...) in the system.
    ///
    /// ### Unix(non-macOS)
    /// `CacheDirConfig` will attempt to create the cache directory inside `/var/cache`
    /// when calling `get_cache_dir`.
    ///
    /// If it fails, it will try the next fallback. If a fallback was not configured, it will
    /// return an `std::io::Error` when calling `get_cache_dir`.
    ///
    /// ### Unix(macOS)
    /// `CacheDirConfig` will attempt to create the cache directory inside `/Library/Caches`
    /// when calling `get_cache_dir`.
    ///
    /// If it fails, it will try the next fallback. If a fallback was not configured, it will
    /// return an `std::io::Error` when calling `get_cache_dir`.
    ///
    /// ### Windows
    /// `CacheDirConfig` will attempt to create the cache directory inside `%ProgramData%`
    /// when calling `get_cache_dir`.
    ///
    /// If it fails, it will try the next fallback. If a fallback was not configured, it will
    /// return an `std::io::Error` when calling `get_cache_dir`.
    ///
    /// ### Redox
    /// Currently not supported
    ///
    /// # Examples
    /// ```no_run
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .sys_cache(true)
    ///                                .get_cache_dir();
    /// ```
    pub fn sys_cache(&mut self, value: bool)  -> &mut CacheDirConfig<'a, 'b> {
        self.sys_cache = value;
        self
    }

    /// This function tells `CacheDirConfig` if it should attempt to create
    /// a cache directory inside one of the system's temporary directories.
    ///
    /// ### Unix
    /// `CacheDirConfig` will attempt to create the cache directory in
    /// 2 locations(will use the second location only if the first one fails):
    ///
    /// 1. `/var/tmp`
    ///
    /// 2. Rust's `temp_dir` which is usually(not on Android) `/tmp`
    ///
    /// `/var/tmp` is preferred because it is more persistent between system restarts.<br/>
    /// It is usually automatically cleaned every 30 days by the system.
    ///
    /// If these fail, `CacheDirConfig` will try the next fallback.
    /// If a fallback was not configured, it will return an `std::io::Error` when calling
    /// `get_cache_dir`.
    ///
    /// ### Windows and Redox
    /// `CacheDirConfig` will attempt to create the cache directory inside the `TMP/TEMP` directory.
    ///
    /// The `TMP/TEMP` directory is obtained internally by calling Rust's `temp_dir`.
    ///
    /// If it fails, it will try the next fallback. If a fallback was not configured, it
    /// will return `std::io::Error` when calling `get_cache_dir`.
    ///
    /// # Examples
    /// ```
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .tmp_cache(true)
    ///                                .get_cache_dir();
    /// ```
    pub fn tmp_cache(&mut self, value: bool)  -> &mut CacheDirConfig<'a, 'b> {
        self.tmp_cache = value;
        self
    }

    /// **Linux and Emscripten only(although, it is OK to call the function on any system)**
    ///
    /// This function tells `CacheDirConfig` if it should attempt to create a cache directory
    /// inside one of these paths:
    ///
    /// 1. `/dev/shm`
    ///
    /// 2. `/run/shm`
    ///
    /// If these fail, when calling `get_cache_dir`, it will return an `std::io::Error`.
    ///
    /// # Examples
    /// ```no_run
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .mem_cache(true)
    ///                                .get_cache_dir();
    /// ```
    pub fn mem_cache(&mut self, value: bool)  -> &mut CacheDirConfig<'a, 'b> {
        self.mem_cache = value;
        self
    }

    /// This function tells `CacheDirConfig` if it should try to use all the cache fallbacks
    /// when attempting to create the cache directory.
    ///
    /// This does not activate `app_cache` if a path for it was not set.
    ///
    /// # Examples
    /// ```no_run
    /// use cachedir::CacheDirConfig;
    ///
    /// // This will not activate the `app_cache`
    /// let cache_dir1 = CacheDirConfig::new("some/path")
    ///                                .try_all_caches()
    ///                                .get_cache_dir();
    ///
    /// // This will activate `app_cache`
    /// let cache_dir2 = CacheDirConfig::new("some/path")
    ///                                 .app_cache_path("/path/to/app/cache")
    ///                                 .app_cache(false) // try_all_caches will activate it back
    ///                                 .try_all_caches()
    ///                                 .get_cache_dir();
    /// ```
    pub fn try_all_caches(&mut self) -> &mut CacheDirConfig<'a, 'b> {
        // We don't use the `app_cache`(with its fallback to ".cache" and "Cache") function
        // for this one because we assume that the user prefers system-wide directories and because
        // he might not expect to use an application cache by default, if he didn't ask for it
        if self.app_cache_path.is_some() { self.app_cache = true };
        self.user_cache = true;
        self.sys_cache  = true;
        self.tmp_cache  = true;
        self.mem_cache  = true;
        self
    }

    /// This creates the cache directory based on the `CacheDirConfig` configurations.
    ///
    /// The returned `CacheDir` contains the path to the cache directory.
    ///
    /// # Errors
    /// If the directory could not be created, `std::io::Error` is returned.</br>
    /// Calling `.kind()` on it will return the last `std::io::ErrorKind`.<br/>
    /// Calling `.description()` will return the description of all the `CacheDirConfig`
    /// attempts that failed.
    ///
    /// # Examples
    /// ```no_run
    /// use cachedir::CacheDirConfig;
    /// let cache_dir = CacheDirConfig::new("some/path")
    ///                                .get_cache_dir();
    /// ```
    pub fn get_cache_dir(&self) -> io::Result<CacheDir> {
        match sys_cache::create_cache_dir(&self) {
            Ok(path_buf) => Ok( CacheDir { path: path_buf } ),
            Err(err)     => Err(err)
        }
    }
}