Skip to main content

arama_env/
dir.rs

1use std::{
2    env,
3    fs::create_dir_all,
4    io::{Error, ErrorKind, Result},
5    path::{Path, PathBuf},
6};
7
8const LOCAL_DIR: &str = ".arama-local";
9const BIN_DIR: &str = "bin";
10const CACHE_DIR: &str = ".arama-cache";
11
12pub fn local_dir() -> Result<PathBuf> {
13    let current_exe = env::current_exe()?;
14    let path = current_exe
15        .parent()
16        .expect("failed to get exe parent directory")
17        .join(LOCAL_DIR);
18    Ok(path.to_path_buf())
19}
20
21pub fn local_bin_dir() -> Result<PathBuf> {
22    let local_dir = local_dir()?;
23    let path = local_dir.join(BIN_DIR);
24    Ok(path.to_path_buf())
25}
26
27pub fn cache_dir() -> Result<PathBuf> {
28    let current_exe = env::current_exe()?;
29    let path = current_exe
30        .parent()
31        .expect("failed to get exe parent directory")
32        .join(CACHE_DIR);
33    Ok(path.to_path_buf())
34}
35
36pub fn validate_dir(path: &Path) -> Result<()> {
37    if !path.exists() {
38        return create_dir_all(path);
39    }
40
41    if !path.is_dir() {
42        return Err(Error::new(
43            ErrorKind::NotADirectory,
44            format!(
45                "Can't treat cache directory, bacause invalid file is found: {}",
46                path.to_string_lossy(),
47            )
48            .as_str(),
49        ));
50    }
51
52    Ok(())
53}