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
// Copyright 2021 - 2021, Rupansh Sekar and the Kilogramme (TBD) contributors
// SPDX-License-Identifier: MPL-2.0
use crate::consts;
use serde::Deserialize;
use std::{convert::Into, fs::File, io::Read};

/// Storage for Userbot's Config
#[derive(Deserialize)]
pub struct UserBotConfig {
    /// `optional`
    #[serde(default)]
    pub options: Options,
    /// `mandatory`
    pub telegram: TgConf,
    /// `mandatory`
    pub mongo: MongoConf,
}

#[derive(Deserialize)]
/// Storage for Userbot specific options,
pub struct Options {
    /// `optional` \
    /// Enable log file \
    /// Default: `true`
    #[serde(default = "Options::default_file_log")]
    pub file_log: bool,
}

/// Storage for telegram related configs
#[derive(Deserialize)]
pub struct TgConf {
    /// `mandatory` \
    /// Obtained from [https://my.telegram.org](https://my.telegram.org)
    pub api_id: i32,
    /// `mandatory` \
    /// Obtained from [https://my.telegram.org](https://my.telegram.org)
    pub api_hash: String,
    /// `mandatory` \
    /// `+` prefixed International phone number
    pub phone: String,
}

/// Storage for MongoDB related configs
#[derive(Deserialize)]
pub struct MongoConf {
    /// `mandatory` \
    /// Server url of mongodb server to connect to
    pub uri: String,
}

impl UserBotConfig {
    /// Parse `config.toml` file
    pub fn from_file() -> Result<UserBotConfig, std::io::Error> {
        let mut conf = File::open(consts::CONFIG_FILE)?;
        let mut confdata = String::new();
        conf.read_to_string(&mut confdata)?;
        toml::from_str(&confdata).map_err(Into::into)
    }
}

impl Default for Options {
    fn default() -> Self {
        Self {
            file_log: Self::default_file_log(),
        }
    }
}

impl Options {
    fn default_file_log() -> bool {
        true
    }
}