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
use std::{env, path::PathBuf};

use directories::{ProjectDirs, UserDirs};
use tracing::error;

use crate::Error;

/// Return the path to the user's home directory
pub fn home_dir_path() -> Result<PathBuf, Error> {
    if let Some(user_dirs) = UserDirs::new() {
        Ok(user_dirs.home_dir().to_path_buf())
    } else {
        Err(Error::NovelApi(
            "No valid home directory path could be retrieved from the operating system".to_string(),
        ))
    }
}

/// Return the path to the project's config directory or the current directory on failure
pub fn config_dir_path(app_name: &str) -> Result<PathBuf, Error> {
    match ProjectDirs::from("", "novel-rs", app_name) {
        Some(dir) => Ok(dir.config_dir().to_path_buf()),
        None => {
            error!("Failed to get the path to the project's config directory, using the current working directory");
            Ok(env::current_dir()?)
        }
    }
}

/// Return the path to the project's local data directory or the current directory on failure
pub fn data_dir_path(app_name: &str) -> Result<PathBuf, Error> {
    match ProjectDirs::from("", "novel-rs", app_name) {
        Some(dir) => Ok(dir.data_local_dir().to_path_buf()),
        None => {
            error!("Failed to get the path to the project's local data directory, using the current working directory");
            Ok(env::current_dir()?)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn config_dir_path() -> Result<(), Error> {
        let _ = super::config_dir_path("test-app")?;
        Ok(())
    }

    #[test]
    fn data_dir_path() -> Result<(), Error> {
        let _ = super::data_dir_path("test-app")?;
        Ok(())
    }
}