1- #[ derive( Debug , Clone ) ]
1+ use serde:: { Deserialize , Serialize } ;
2+ use std:: fs;
3+ use std:: path:: Path ;
4+ use std:: error:: Error ;
5+ use std:: io:: { self , Write } ;
6+
7+ const CONFIG_FILE : & str = "config.toml" ;
8+
9+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
210pub struct Config {
3- pub discord_webhook_url : String ,
11+ pub discord : DiscordConfig ,
12+ pub monitoring : MonitoringConfig ,
13+ pub appearance : AppearanceConfig ,
14+ }
15+
16+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
17+ pub struct DiscordConfig {
18+ pub webhook_url : String ,
19+ pub bot_name : String ,
20+ pub bot_avatar_url : String ,
21+ }
22+
23+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
24+ pub struct MonitoringConfig {
425 pub commits_url : String ,
526 pub check_interval_secs : u64 ,
6- pub rust_color : u32 ,
27+ }
28+
29+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
30+ pub struct AppearanceConfig {
31+ pub embed_color : String ,
732 pub footer_icon_url : String ,
833}
934
1035impl Config {
11- pub fn new ( ) -> Self {
12- Self {
13- discord_webhook_url : "https://discord.com/api/webhooks/1377062435000680469/p8IPNirCl90kWHSpmX-YMEDxeNKdaGOUtofNY_jSX_B-w_3vMSXymIaKuvVVP71Xtlnq" . to_string ( ) ,
14- commits_url : "https://commits.facepunch.com/r/rust_reboot" . to_string ( ) ,
15- check_interval_secs : 50 ,
16- rust_color : 0xCD412B ,
17- footer_icon_url : "https://i.imgur.com/on47Qk9.png" . to_string ( ) ,
36+ pub fn load_or_create ( ) -> Result < Self , Box < dyn Error > > {
37+ if Path :: new ( CONFIG_FILE ) . exists ( ) {
38+ Self :: load_from_file ( )
39+ } else {
40+ Self :: create_default_and_prompt ( )
41+ }
42+ }
43+
44+ fn load_from_file ( ) -> Result < Self , Box < dyn Error > > {
45+ let content = fs:: read_to_string ( CONFIG_FILE ) ?;
46+ let config: Config = toml:: from_str ( & content) ?;
47+ Ok ( config)
48+ }
49+
50+ fn create_default_and_prompt ( ) -> Result < Self , Box < dyn Error > > {
51+ println ! ( "🔧 First time setup - Creating configuration file..." ) ;
52+
53+ let default_config = Self :: default ( ) ;
54+ let toml_content = toml:: to_string_pretty ( & default_config) ?;
55+
56+ fs:: write ( CONFIG_FILE , & toml_content) ?;
57+
58+ println ! ( "✅ Created '{}'" , CONFIG_FILE ) ;
59+ println ! ( ) ;
60+ println ! ( "📝 Please edit the configuration file with your settings:" ) ;
61+ println ! ( " - Discord webhook URL" ) ;
62+ println ! ( " - Bot name and avatar" ) ;
63+ println ! ( " - Monitoring settings" ) ;
64+ println ! ( ) ;
65+ print ! ( "Press Enter when you've finished editing the config file..." ) ;
66+ io:: stdout ( ) . flush ( ) ?;
67+
68+ let mut input = String :: new ( ) ;
69+ io:: stdin ( ) . read_line ( & mut input) ?;
70+
71+ // Reload the config after user edits
72+ Self :: load_from_file ( )
73+ }
74+
75+ pub fn rust_color ( & self ) -> u32 {
76+ // Parse hex color string to u32
77+ if self . appearance . embed_color . starts_with ( '#' ) {
78+ u32:: from_str_radix ( & self . appearance . embed_color [ 1 ..] , 16 )
79+ . unwrap_or ( 0xCD412B )
80+ } else {
81+ u32:: from_str_radix ( & self . appearance . embed_color , 16 )
82+ . unwrap_or ( 0xCD412B )
1883 }
1984 }
2085}
2186
2287impl Default for Config {
2388 fn default ( ) -> Self {
24- Self :: new ( )
89+ Self {
90+ discord : DiscordConfig {
91+ webhook_url : "REPLACE_WITH_YOUR_DISCORD_WEBHOOK_URL" . to_string ( ) ,
92+ bot_name : "Rust Commit Tracker" . to_string ( ) ,
93+ bot_avatar_url : "https://i.imgur.com/on47Qk9.png" . to_string ( ) ,
94+ } ,
95+ monitoring : MonitoringConfig {
96+ commits_url : "https://commits.facepunch.com/r/rust_reboot" . to_string ( ) ,
97+ check_interval_secs : 50 ,
98+ } ,
99+ appearance : AppearanceConfig {
100+ embed_color : "#CD412B" . to_string ( ) , // Rust orange
101+ footer_icon_url : "https://i.imgur.com/on47Qk9.png" . to_string ( ) ,
102+ } ,
103+ }
25104 }
26105}
0 commit comments