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
use std::path::PathBuf;

use crate::{AppName, Error};

/// Name of the file used to store credentials.
pub const CREDENTIALS_FILE_NAME: &str = "credentials";

/// Returns the path to the credentials file.
#[derive(Debug)]
pub struct CredentialsFile;

impl CredentialsFile {
    /// Returns the path to the credentials in the user's configuration
    /// directory.
    ///
    /// The file's existence is not checked -- that is the responsibility of the
    /// caller.
    ///
    /// The path differs depending on the user's operating system:
    ///
    /// * `Windows`: `C:\Users\%USER%\AppData\Roaming\<app>\credentials`
    /// * `Linux`: `$XDG_CONFIG_HOME` or `$HOME/.config/<app>/credentials`
    /// * `OS X`: `$HOME/Library/Application Support/<app>/credentials`
    pub fn path(app_name: AppName<'_>) -> Result<PathBuf, Error> {
        dirs::config_dir()
            .map(|config_dir| config_dir.join(*app_name).join(CREDENTIALS_FILE_NAME))
            .ok_or(Error::UserConfigDirNotFound)
    }
}