summaryrefslogtreecommitdiff
path: root/src/bot_config.rs
blob: 5fd2482725dfced0a1a42dbb333e9ca254b66728 (plain) (blame)
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
//! Configuration module of the telegram bot. Here it gets its information to
//! login to its homeserver.

use config::{Config, ConfigError, File as ConfigFile};

/// Configuration struct of a bot
pub struct BotConfig
{
    username:       String,
    password:       String,
    homeserver_url: String,
}

impl BotConfig
{
    /// Read the default bot configuration file
    ///
    /// # Return
    /// The configuration needed for the bot to function, or, if the
    /// configuration file cannot be read, the error that occured while
    /// trying to load the configuration file.
    pub fn from_config_file() -> Result<BotConfig, ConfigError>
    {
        let mut config = Config::default();
        config.merge(ConfigFile::with_name("botconfig"))?;

        Ok(Self {
            username:       config.get_str("username")?,
            password:       config.get_str("password")?,
            homeserver_url: config.get_str("homeserver_url")?,
        })
    }

    /// Get the username that should be used by the bot
    pub fn username(&self) -> &String { &self.username }
    /// Get the password the bot should use to authenticate its user
    pub fn password(&self) -> &String { &self.password }
    /// Get the homeserver of the bot where it should send the login data to
    pub fn homeserver_url(&self) -> &String { &self.homeserver_url }
}