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
#![allow(clippy::option_if_let_else)]
use std::path::PathBuf;

use crate::error::Error;

#[derive(Debug, Clone, Copy)]
pub enum Directories {
  /// Directories to store information related to the state of Wick.
  GlobalState,
  /// Directories to store caches.
  RelativeCache,
  /// Directories to store global caches.
  GlobalCache,
}

impl Directories {
  pub fn basedir(&self) -> Result<PathBuf, Error> {
    match self {
      Self::GlobalState => global_state_dir(),
      Self::RelativeCache => Ok(Cache::Root.basedir()),
      Self::GlobalCache => Ok(global_cache_dir()?.join(Cache::Root.basedir())),
    }
  }
}

const CACHE_BASE: &str = ".wick";

#[derive(Debug, Clone, Copy)]
/// The type of cache to use.
pub enum Cache {
  /// The root cache.
  Root,
  /// The component cache directory.
  Assets,
}

impl Cache {
  /// Get the path to the cache.
  #[must_use]
  pub fn basedir(&self) -> PathBuf {
    match self {
      Self::Root => PathBuf::from(CACHE_BASE),
      Self::Assets => PathBuf::from("remote"),
    }
  }
}

fn global_state_dir() -> Result<PathBuf, Error> {
  #[cfg(not(target_os = "windows"))]
  return Ok(match xdg::BaseDirectories::with_prefix("wick") {
    Ok(xdg) => xdg.get_state_home(),
    Err(_) => std::env::current_dir().map_err(|_| Error::Pwd)?,
  });
  #[cfg(target_os = "windows")]
  return Ok(match std::env::var("LOCALAPPDATA") {
    Ok(var) => PathBuf::from(format!("{}/wick", var)),
    Err(_) => std::env::current_dir().map_err(|_| Error::Pwd)?,
  });
}

fn global_cache_dir() -> Result<PathBuf, Error> {
  #[cfg(not(target_os = "windows"))]
  return Ok(match xdg::BaseDirectories::with_prefix("wick") {
    Ok(xdg) => xdg.get_cache_home(),
    Err(_) => std::env::temp_dir(),
  });
  #[cfg(target_os = "windows")]
  return Ok(std::env::temp_dir());
}