summaryrefslogtreecommitdiff
path: root/src/bot_config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bot_config.rs')
-rw-r--r--src/bot_config.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/bot_config.rs b/src/bot_config.rs
new file mode 100644
index 0000000..2438c40
--- /dev/null
+++ b/src/bot_config.rs
@@ -0,0 +1,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
+{
+ user: 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 {
+ user: config.get_str("user")?,
+ password: config.get_str("password")?,
+ homeserver_url: config.get_str("homeserver_url")?,
+ })
+ }
+
+ /// Get the username that should be used by the bot
+ pub fn user(&self) -> &String { &self.user }
+ /// 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 }
+}