Skip to main content

vex_integrations/
config.rs

1use std::fmt;
2use std::path::Path;
3
4use serde::Deserialize;
5
6#[derive(Debug, Clone, Deserialize, Default)]
7pub struct IntegrationsConfig {
8    #[serde(default)]
9    pub discord: DiscordConfig,
10    #[serde(default)]
11    pub telegram: TelegramConfig,
12}
13
14#[derive(Clone, Deserialize, Default)]
15pub struct DiscordConfig {
16    #[serde(default)]
17    pub token: String,
18    #[serde(default)]
19    pub channel_id: String,
20    #[serde(default)]
21    pub enabled: bool,
22}
23
24impl fmt::Debug for DiscordConfig {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.debug_struct("DiscordConfig")
27            .field("token", &"[redacted]")
28            .field("channel_id", &self.channel_id)
29            .field("enabled", &self.enabled)
30            .finish()
31    }
32}
33
34#[derive(Clone, Deserialize, Default)]
35pub struct TelegramConfig {
36    #[serde(default)]
37    pub token: String,
38    #[serde(default)]
39    pub chat_id: String,
40    #[serde(default)]
41    pub enabled: bool,
42}
43
44impl fmt::Debug for TelegramConfig {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        f.debug_struct("TelegramConfig")
47            .field("token", &"[redacted]")
48            .field("chat_id", &self.chat_id)
49            .field("enabled", &self.enabled)
50            .finish()
51    }
52}
53
54/// Full config file structure — extends the existing VexConfig with integration fields.
55#[derive(Clone, Deserialize, Default)]
56struct FullConfig {
57    #[serde(default)]
58    discord: DiscordConfig,
59    #[serde(default)]
60    telegram: TelegramConfig,
61}
62
63impl IntegrationsConfig {
64    pub fn load(vex_dir: &Path) -> Self {
65        let path = vex_dir.join("config.yml");
66        std::fs::read_to_string(&path)
67            .ok()
68            .and_then(|data| serde_yaml::from_str::<FullConfig>(&data).ok())
69            .map(|c| Self {
70                discord: c.discord,
71                telegram: c.telegram,
72            })
73            .unwrap_or_default()
74    }
75}