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
use std::fs::read_to_string;

use serde::Deserialize;

/// This struct represents the contents of a `~/.config/fedora.toml` file.
#[derive(Debug, Deserialize)]
pub struct FedoraConfig {
    /// This section contains information about the user's FAS account.
    #[serde(rename(deserialize = "FAS"))]
    pub fas: FASConfig,
}

/// This config file section contains information about the current user's FAS account.
#[derive(Debug, Deserialize)]
pub struct FASConfig {
    /// User name in the fedora accounts system (FAS)
    pub username: String,
}

/// This helper function reads and parses the configuration file.
pub fn get_config() -> Result<FedoraConfig, String> {
    let home = match dirs::home_dir() {
        Some(path) => path,
        None => {
            return Err(String::from("Unable to determine $HOME."));
        },
    };

    let config_path = home.join(".config/fedora.toml");

    let config_str = match read_to_string(&config_path) {
        Ok(string) => string,
        Err(_) => {
            return Err(String::from(
                "Unable to read configuration file from ~/.config/fedora.toml",
            ));
        },
    };

    let config: FedoraConfig = match toml::from_str(&config_str) {
        Ok(config) => config,
        Err(_) => {
            return Err(String::from(
                "Unable to parse configuration file from ~/.config/fedora.toml",
            ));
        },
    };

    Ok(config)
}

/// This helper function reads the username from the legacy `~/.fedora.upn` file.
pub fn get_legacy_username() -> Result<Option<String>, String> {
    let home = match dirs::home_dir() {
        Some(path) => path,
        None => {
            return Err(String::from("Unable to determine $HOME."));
        },
    };

    let file_path = home.join(".fedora.upn");

    let username = match read_to_string(&file_path) {
        Ok(string) => Some(string.trim().to_string()),
        Err(error) => {
            return if error.kind() == std::io::ErrorKind::NotFound {
                Ok(None)
            } else {
                Err(error.to_string())
            };
        },
    };

    Ok(username)
}