diff --git a/.vscode/settings.json b/.vscode/settings.json index 4d55b26..f7b8426 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,5 +18,8 @@ "telegram", "discord", "matrix", + ], + "rust-analyzer.linkedProjects": [ + "./Cargo.toml" ] } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 9187d5f..fa7eb81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -990,6 +990,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "micronfig" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09d5fa5735feed9ff4d6fc550793ef80d18b90f33f3f89af6176318f295383cd" + [[package]] name = "mime" version = "0.3.17" @@ -1157,6 +1163,7 @@ dependencies = [ "lazy_static", "log", "md5", + "micronfig", "pretty_env_logger", "rand", "regex", diff --git a/Cargo.toml b/Cargo.toml index 4668dd1..ab3733f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ hmac = { version = "0.12.1", optional = true } sha2 = { version = "0.10.6", optional = true } # exec pretty_env_logger = { version = "0.4.0", optional = true } +micronfig = { version = "0.1.2", optional = true } # data serde = { version = "1.0.140", features = ["derive"] } serde_json = { version = "1.0.82" } @@ -56,7 +57,7 @@ tokio = { version = "1.20.3", features = ["rt-multi-thread", "macros"] } [features] jpg = ["hmac", "sha2", "base64", "hex"] test = ["tokio"] -exec = ["pretty_env_logger"] +exec = ["pretty_env_logger", "micronfig"] search = ["tantivy"] telegram = ["exec", "search", "jpg", "teloxide", "tokio", "md5", "rand"] discord = ["exec", "search", "serenity", "tokio", "anyhow"] diff --git a/src/config/mod.rs b/src/config/mod.rs new file mode 100644 index 0000000..fe3f86a --- /dev/null +++ b/src/config/mod.rs @@ -0,0 +1,65 @@ +//! Module defining configurable values that can be used by the binaries. +#![allow(missing_docs)] + + +/// Configuration required by the `exec` feature. +#[cfg(feature = "exec")] +pub mod exec { + lazy_static::lazy_static! { + /// The locale that card data should be downloaded in. + /// + /// # Examples + /// + /// - `en_US` + /// - `it_IT` + /// + pub static ref DATA_DRAGON_LOCALE: String = + micronfig::required("DATA_DRAGON_LOCALE"); + + /// The set codes for which card data should be downloaded, separated by commas. + /// + /// # Examples + /// + /// - `set1,set2,set3` + /// - `set1` + /// - `set1,set2,set3,set4,set5,set6,set6cde,set7` + /// + pub static ref DATA_DRAGON_SET_CODES: Vec = + micronfig::required::("DATA_DRAGON_SET_CODES") + .split(",").map(|s: &str| s.to_string()).collect(); + } +} + +/// Configuration required by the `jpg` feature. +#[cfg(feature = "jpg")] +pub mod jpg { + lazy_static::lazy_static! { + /// Secret key configured in imgproxy. + pub static ref POROXY_KEY: Vec = micronfig::required("POROXY_KEY"); + + /// Salt configured in imgproxy. + pub static ref POROXY_SALT: String = micronfig::required("POROXY_SALT"); + + /// URL where imgproxy can be reached at. + pub static ref POROXY_HOST: String = micronfig::required("POROXY_HOST"); + } +} + +/// Configuration required by the `discord` feature. +#[cfg(feature = "discord")] +pub mod discord { + lazy_static::lazy_static! { + /// ID of the guild where commands should be registered in. + /// + /// If not defined, commands are registered globally, and then cached. + /// + /// Useful for development, since guild commands are not cached. + pub static ref SERENITY_DEV_GUILD_ID: Option = micronfig::optional("SERENITY_DEV_GUILD_ID").map(|s: u64| s.into()); + + /// The Discord bot token. + pub static ref SERENITY_TOKEN: String = micronfig::required("SERENITY_TOKEN"); + + /// The Discord bot app ID. + pub static ref SERENITY_APPID: u64 = micronfig::required("SERENITY_APPID"); + } +} \ No newline at end of file diff --git a/src/data/setbundle/art.rs b/src/data/setbundle/art.rs index a991916..b1d4292 100644 --- a/src/data/setbundle/art.rs +++ b/src/data/setbundle/art.rs @@ -53,8 +53,7 @@ impl CardArt { use hmac::Mac; use std::env; - let key = env::var("POROXY_KEY") - .expect("POROXY_KEY to be set"); + let key = crate::config::jpg::POROXY_KEY; let key = hex::decode(key) .expect("POROXY_KEY to be a valid hex code"); diff --git a/src/lib.rs b/src/lib.rs index 3b1076a..2a86f67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,6 +33,9 @@ pub mod data; +#[cfg(feature = "exec")] +pub mod config; + #[cfg(feature = "search")] pub mod search;