Skip to content

Commit 5376f25

Browse files
committed
docs: add documentation for config module
1 parent a2f2cab commit 5376f25

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

src/config/database.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Database Configuration
2+
13
use std::{fs, path::PathBuf};
24

35
use anyhow::{anyhow, Result};
@@ -10,11 +12,14 @@ use crate::config::Node;
1012

1113
#[non_exhaustive]
1214
pub(super) struct DatabaseConfig {
15+
/// Database pool.
1316
pool: SqlitePool,
17+
/// Database URL/location.
1418
url: String,
1519
}
1620

1721
impl DatabaseConfig {
22+
/// Generate a new `DatabaseConfig` from the config file.
1823
pub fn generate(config_file: &Node, default_path: PathBuf) -> Result<Self> {
1924
// TODO: make cmd argument priority over config file one
2025
let path = get_value!(config_file, PathBuf, "BOT_DATABASE_LOCATION", "database"=>"location", default_path)?;
@@ -38,12 +43,15 @@ impl DatabaseConfig {
3843
Ok(Self { pool, url })
3944
}
4045

46+
/// Run database migrations.
4147
pub async fn run_migrations(&self) -> Result<()> {
4248
sqlx::migrate!().run(&self.pool).await?;
4349
Ok(())
4450
}
4551

52+
/// Returns the database pool.
4653
pub const fn pool(&self) -> &SqlitePool { &self.pool }
4754

55+
/// Returns the database URL.
4856
pub const fn url(&self) -> &String { &self.url }
4957
}

src/config/defaults.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
//! Default values for the config.
2+
//!
3+
//! This file contains the default values for the config. These values are used
4+
//! when the config file is not present or when a value is missing from the
5+
//! config file.
6+
17
pub(super) const PREFIX: &str = "-";
28
pub(super) const AUTO_REGISTER_COMMANDS: bool = true;
39
#[cfg(feature = "music")]

src/config/general.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! General Configuration
2+
13
use anyhow::Result;
24
#[cfg(feature = "config_file")]
35
use taplo::dom::Node;
@@ -10,14 +12,20 @@ use crate::config::Node;
1012

1113
#[non_exhaustive]
1214
pub(super) struct GeneralConfig {
15+
/// Discord token.
1316
token: String,
17+
/// Prefix for commands.
1418
prefix: String,
19+
/// Whether to auto register commands.
1520
auto_register_commands: bool,
21+
/// Whether to auto change voice channel when user sended the command from
22+
/// another voice cahnnel.
1623
#[cfg(feature = "music")]
1724
vc_auto_change: bool,
1825
}
1926

2027
impl GeneralConfig {
28+
/// Generate a new `GeneralConfig` from the config file.
2129
pub fn generate(config_file: &Node) -> Result<Self> {
2230
let token = get_value!(config_file, String, "BOT_TOKEN", "general"=>"token", "Discord token couldn't found.")?;
2331
let prefix = get_value!(config_file, String, "BOT_PREFIX", "general"=>"prefix", PREFIX)?;
@@ -34,12 +42,17 @@ impl GeneralConfig {
3442
})
3543
}
3644

45+
/// Returns the Discord token.
3746
pub const fn token(&self) -> &String { &self.token }
3847

48+
/// Returns the prefix for commands.
3949
pub const fn prefix(&self) -> &String { &self.prefix }
4050

51+
/// Returns whether to auto register commands.
4152
pub const fn auto_register_commands(&self) -> bool { self.auto_register_commands }
4253

54+
/// Returns whether to auto change voice channel when user sended the
55+
/// command from another voice cahnnel.
4356
#[cfg(feature = "music")]
4457
pub const fn vc_auto_change(&self) -> bool { self.vc_auto_change }
4558
}

src/config/message.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! Message Configuration.
2+
13
use anyhow::Result;
24
#[cfg(feature = "config_file")]
35
use taplo::dom::Node;
@@ -15,15 +17,22 @@ use crate::config::Node;
1517

1618
#[non_exhaustive]
1719
pub(super) struct MessageConfig {
20+
/// Whether to always use embed messages.
1821
always_embed: bool,
22+
/// Whether to use random colors for embed messages.
1923
random_embed_colors: bool,
24+
/// Color to use in embed message when the given command successes.
2025
success_color: u32,
26+
/// Color to use in embed message when there is no success/error.
2127
normal_color: u32,
28+
/// Color to use in embed message when the given command errors.
2229
error_color: u32,
30+
/// Time limit for interaction messages.
2331
interaction_time_limit: u64,
2432
}
2533

2634
impl MessageConfig {
35+
/// Generate a new `MessageConfig` from the config file.
2736
pub fn generate(config_file: &Node) -> Result<Self> {
2837
let always_embed = get_value!(config_file, bool, "BOT_MSG_ALWAYS_EMBED", "message"=>"always_embed", ALWAYS_EMBED)?;
2938
let random_embed_colors = get_value!(config_file, bool, "BOT_MSG_RANDOM_EMBED_COLORS", "message"=>"random_embed_colors", RANDOM_EMBED_COLORS)?;
@@ -42,15 +51,23 @@ impl MessageConfig {
4251
})
4352
}
4453

54+
/// Returns whether to always use embed messages.
4555
pub const fn always_embed(&self) -> bool { self.always_embed }
4656

57+
/// Returns whether to use random colors for embed messages.
4758
pub const fn random_embed_colors(&self) -> bool { self.random_embed_colors }
4859

60+
/// Returns the color to use in embed message when the given command
61+
/// successes.
4962
pub const fn success_color(&self) -> u32 { self.success_color }
5063

64+
/// Returns the color to use in embed message when there is no
65+
/// success/error.
5166
pub const fn normal_color(&self) -> u32 { self.normal_color }
5267

68+
/// Returns the color to use in embed message when the given command errors.
5369
pub const fn error_color(&self) -> u32 { self.error_color }
5470

71+
/// Returns the time limit for interaction messages.
5572
pub const fn interaction_time_limit(&self) -> u64 { self.interaction_time_limit }
5673
}

src/config/mod.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
//! This module keeps state of the bot
2+
//!
3+
//! Even though it started as storing configuration about bot, now it is
4+
//! responsible to store everything from configuration to music player state and
5+
//! probably the most confusing file/module in this project yet it is
6+
//! surprisingly straightforward.
7+
//!
8+
//! Main [`Config`] struct is divided into substructs/submodules. Each submodule
9+
//! is responsible for storing configuration about a specific part of the bot.
10+
//! Every struct is its own file to make it easier to find them.
11+
//!
12+
//! Also to not make every struct public their functions are re-exported in the
13+
//! main [`Config`] struct.
14+
//!
15+
//! ## Why constructer is named `generate()`?
16+
//! Every struct in config module and its submodules use `generate()` function
17+
//! to generate their struct. This is because this structs only created once and
18+
//! third party resources such as config files and environmental variables used
19+
//! in their creation. Using different name for the constructor other than
20+
//! `new()` is to make it clear that it is not a normal constructor.
21+
122
#[cfg(feature = "cmd")]
223
mod cmd_arguments;
324
mod defaults;
@@ -47,9 +68,13 @@ use crate::{
4768
server::Server,
4869
};
4970

71+
/// Dummy type to use when `config_file` feature is not enabled. When
72+
/// `config_file` feature is disabled, [`taplo`] crate is disabled. therefore,
73+
/// there is no Node type, and this creates a compilation error.
5074
#[cfg(not(feature = "config_file"))]
5175
struct Node;
5276

77+
/// Main struct to store everything
5378
#[non_exhaustive]
5479
pub struct Config {
5580
general: GeneralConfig,
@@ -66,6 +91,7 @@ pub struct Config {
6691
}
6792

6893
impl Config {
94+
/// Generate [`Config`] from config sources
6995
pub fn generate() -> Result<Self> {
7096
#[cfg(feature = "cmd")]
7197
let cmd_arguments = CMDArguments::parse();
@@ -172,48 +198,95 @@ impl Config {
172198
})
173199
}
174200

201+
/// Get `Discord` token for bot
175202
pub const fn token(&self) -> &String { self.general.token() }
176203

204+
/// Get prefix for `prefix commands`
177205
pub const fn prefix(&self) -> &String { self.general.prefix() }
178206

207+
/// Register `slash commands` to `Discord`
179208
pub const fn auto_register_commands(&self) -> bool { self.general.auto_register_commands() }
180209

210+
/// Get `vc_auto_change` setting
181211
#[cfg(feature = "music")]
182212
pub const fn vc_auto_change(&self) -> bool { self.general.vc_auto_change() }
183213

214+
/// Get `message_always_embed` setting
215+
///
216+
/// WARNING: You do not need to use this setting. [`message!()`] macro
217+
/// already obeys this setting.
218+
///
219+
/// [`message!()`]: crate::messager::message!()
184220
pub const fn message_always_embed(&self) -> bool { self.message.always_embed() }
185221

222+
/// Get `message_randowm_colors` setting
223+
///
224+
/// WARNING: You do not need to use this setting. [`message!()`] macro
225+
/// already obeys this setting.
226+
///
227+
/// [`message!()`]: crate::messager::message!()
186228
pub const fn message_random_embed_colors(&self) -> bool { self.message.random_embed_colors() }
187229

230+
/// Get `message_success_color` setting
231+
///
232+
/// WARNING: You do not need to use this setting. [`message!()`] macro
233+
/// already obeys this setting.
234+
///
235+
/// [`message!()`]: crate::messager::message!()
188236
pub const fn message_success_color(&self) -> u32 { self.message.success_color() }
189237

238+
/// Get `message_normal_color` setting
239+
///
240+
/// WARNING: You do not need to use this setting. [`message!()`] macro
241+
/// already obeys this setting.
242+
///
243+
/// [`message!()`]: crate::messager::message!()
190244
pub const fn message_normal_color(&self) -> u32 { self.message.normal_color() }
191245

246+
/// Get `message_error_color` setting
247+
///
248+
/// WARNING: You do not need to use this setting. [`message!()`] macro
249+
/// already obeys this setting.
250+
///
251+
/// [`message!()`]: crate::messager::message!()
192252
pub const fn message_error_color(&self) -> u32 { self.message.error_color() }
193253

254+
/// Get `message_interaction_time_limit` setting
194255
pub const fn message_interaction_time_limit(&self) -> u64 {
195256
self.message.interaction_time_limit()
196257
}
197258

259+
/// Get `youtube_search_count` setting
198260
#[cfg(feature = "music")]
199261
pub const fn youtube_search_count(&self) -> u8 { self.youtube.search_count() }
200262

263+
/// Get `youtube_age_resricted` setting
201264
#[cfg(feature = "music")]
202265
pub const fn youtube_age_restricted(&self) -> bool { self.youtube.age_restricted() }
203266

267+
/// Chack if `Spotify` enabled
204268
#[cfg(feature = "spotify")]
205269
pub const fn is_spotify_initialized(&self) -> bool { self.spotify.is_some() }
206270

271+
/// Get `Spotify` client credentials
207272
#[cfg(feature = "spotify")]
208273
pub fn spotify_client(&self) -> Option<(&String, &String)> {
209274
Some(self.spotify.as_ref()?.client())
210275
}
211276

277+
/// Get `Spotify` token
212278
#[cfg(feature = "spotify")]
213279
pub async fn spotify_token(&self) -> Option<String> {
214280
Some(self.spotify.as_ref()?.token().await)
215281
}
216282

283+
/// Get databse pool to interact with database
284+
///
285+
/// WARNING: use [`bot::commands::macros::db_connection!()`] instead of this
286+
/// if you want database connection in [bot command].
287+
///
288+
/// [`bot::commands::macros::db_connection!()`]: crate::bot::commands::macros::db_connection!()
289+
/// [bot_command]: crate::bot::commands
217290
#[cfg(feature = "database")]
218291
pub const fn database_pool(&self) -> Option<&SqlitePool> {
219292
if let Some(db) = &self.database {
@@ -223,6 +296,7 @@ impl Config {
223296
}
224297
}
225298

299+
/// Get database URL
226300
#[cfg(feature = "database")]
227301
pub const fn database_url(&self) -> Option<&String> {
228302
if let Some(db) = &self.database {
@@ -232,6 +306,7 @@ impl Config {
232306
}
233307
}
234308

309+
/// Run database migrations to setup database
235310
#[cfg(feature = "database")]
236311
pub async fn run_database_migrations(&self) -> Result<()> {
237312
if let Some(db) = &self.database {
@@ -241,8 +316,10 @@ impl Config {
241316
Ok(())
242317
}
243318

319+
/// Get connected servers
244320
pub const fn servers(&self) -> &RwLock<HashMap<GuildId, Arc<Server>>> { &self.servers }
245321

322+
/// Get main [`Songbird`] strcut
246323
#[cfg(feature = "music")]
247324
pub fn songbird(&self) -> Arc<Songbird> { Arc::clone(&self.songbird) }
248325
}

src/config/spotify.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! `Spotify` Configuration.
2+
13
use std::time::Instant;
24

35
use anyhow::Result;
@@ -10,15 +12,20 @@ use crate::config::Node;
1012

1113
#[non_exhaustive]
1214
pub(super) struct SpotifyConfig {
15+
/// Client ID for Spotify API.
1316
client_id: String,
17+
/// Client secret for Spotify API.
1418
client_secret: String,
19+
/// Token for Spotify API.
1520
token: RwLock<Option<String>>,
21+
/// Last time token was refreshed.
1622
last_refresh: RwLock<Option<Instant>>,
1723
}
1824

1925
impl SpotifyConfig {
2026
const REFRESH_TIME: u64 = 3500;
2127

28+
/// Generate a new `SpotifyConfig` from the config file.
2229
pub fn generate(config_file: &Node) -> Result<Self> {
2330
let client_id = get_value!(config_file, String, "BOT_SP_CLIENT_ID", "spotify"=>"client_id",
2431
"For Spotify support client ID is requared. Either set your client ID on the config file or disable Spotify support")?;
@@ -35,8 +42,11 @@ impl SpotifyConfig {
3542
})
3643
}
3744

45+
/// Returns the client ID and client secret.
3846
pub const fn client(&self) -> (&String, &String) { (&self.client_id, &self.client_secret) }
3947

48+
/// Returns the current token if it is not expired, otherwise it requests a
49+
/// new one and return that.
4050
pub async fn token(&self) -> String {
4151
if self.token.read().await.is_none()
4252
|| self
@@ -60,6 +70,7 @@ impl SpotifyConfig {
6070
.to_string()
6171
}
6272

73+
/// Request a new token from Spotify API.
6374
#[allow(clippy::significant_drop_tightening)]
6475
async fn refresh_token(&self) {
6576
let mut write_lock_token = self.token.write().await;

src/config/youtube.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! `YouTube` Configuration.
2+
13
use anyhow::Result;
24
#[cfg(feature = "config_file")]
35
use taplo::dom::Node;
@@ -10,19 +12,24 @@ use crate::config::Node;
1012
// TODO: implement this configs
1113
#[non_exhaustive]
1214
pub(super) struct YouTubeConfig {
15+
/// Number of results to show when searching for a song.
1316
search_count: u8,
17+
/// Whether to allow age restricted videos.
1418
age_restricted: bool,
1519
}
1620

1721
impl YouTubeConfig {
22+
/// Generate a new `YouTubeConfig` from the config file.
1823
pub fn generate(config_file: &Node) -> Result<Self> {
1924
let search_count = get_value!(config_file, u8, "BOT_YT_SEARCH_COUNT", "youtube"=>"search_count", YT_SEARCH_COUNT)?;
2025
let age_restricted = get_value!(config_file, bool, "BOT_YT_AGE_RESTRICTED", "youtube"=>"age_restricted", YT_AGE_RESTRICTED)?;
2126

2227
Ok(Self { search_count, age_restricted })
2328
}
2429

30+
/// Returns the number of results to show when searching for a song.
2531
pub const fn search_count(&self) -> u8 { self.search_count }
2632

33+
/// Returns whether to allow age restricted videos.
2734
pub const fn age_restricted(&self) -> bool { self.age_restricted }
2835
}

0 commit comments

Comments
 (0)