From 179ad3e8244ec09ee00f922b88bb530e1550c805 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Fri, 8 Jul 2022 19:21:13 +0200 Subject: [PATCH] fix: use snake case and module for configuration --- configuration.example.json | 16 +++-- configuration.schema.json | 11 ++- .../application.rs} | 71 +++++++------------ src/configuration/mod.rs | 1 + src/main.rs | 18 ++--- 5 files changed, 50 insertions(+), 67 deletions(-) rename src/{configuration.rs => configuration/application.rs} (72%) create mode 100644 src/configuration/mod.rs diff --git a/configuration.example.json b/configuration.example.json index c8ae19b..c09ade2 100644 --- a/configuration.example.json +++ b/configuration.example.json @@ -1,17 +1,19 @@ { "$schema": "./configuration.schema.json", - "discord-authorization-token": "", + "discord_authorization_token": "", "administrators": { "roles": [0], "users": [0] }, - "thread-introductions": [ + "thread_introductions": [ { "channels": [0], - "message": "" + "response": { + "message": "" + } } ], - "message-responders": [ + "message_responders": [ { "includes": { "channels": [0], @@ -23,10 +25,12 @@ }, "condition": { "user": { - "server-age": 2 + "server_age": 2 } }, - "message": "" + "response": { + "message": "" + } } ] } diff --git a/configuration.schema.json b/configuration.schema.json index 2c2f8de..e00ae84 100644 --- a/configuration.schema.json +++ b/configuration.schema.json @@ -4,9 +4,9 @@ "title": "Configuration schema", "description": "The Revanced Discord bot configuration schema.", "type": "object", - "required": ["discord-authorization-token"], + "required": ["discord_authorization_token"], "properties": { - "discord-authorization-token": { + "discord_authorization_token": { "type": "string", "description": "The authorization token for the Discord bot." }, @@ -29,7 +29,7 @@ }, "description": "The list of administrators to control the Discord bot." }, - "thread-introductions": { + "thread_introductions": { "type": "array", "items": { "type": "object", @@ -48,7 +48,7 @@ "minItems": 1, "uniqueItems": true }, - "message-responders": { + "message_responders": { "type": "array", "items": { "type": "object", @@ -81,7 +81,7 @@ "user": { "type": "object", "properties": { - "server-age": { + "server_age": { "type": "integer", "description": "The user must be at least this many days old on the server." } @@ -230,7 +230,6 @@ "$ref": "#/$defs/embed", "description": "The embed to send." } - }, } } } diff --git a/src/configuration.rs b/src/configuration/application.rs similarity index 72% rename from src/configuration.rs rename to src/configuration/application.rs index 69870c4..86b4f82 100644 --- a/src/configuration.rs +++ b/src/configuration/application.rs @@ -4,33 +4,50 @@ use std::io::{Error, Read, Write}; use serde::{Deserialize, Serialize}; #[derive(Default, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct BotConfiguration { - #[serde(rename = "discord-authorization-token")] +pub struct Configuration { pub discord_authorization_token: String, pub administrators: Administrators, - #[serde(rename = "thread-introductions")] pub thread_introductions: Vec, - #[serde(rename = "message-responders")] pub message_responders: Vec, } +impl Configuration { + fn save(&self) -> Result<(), Error> { + let mut file = File::create("configuration.json")?; + let json = serde_json::to_string_pretty(&self)?; + file.write(json.as_bytes())?; + Ok(()) + } + + pub fn load() -> Result { + let mut file = match File::open("configuration.json") { + Ok(file) => file, + Err(_) => { + let configuration = Configuration::default(); + configuration.save()?; + return Ok(configuration); + }, + }; + + let mut buf = String::new(); + file.read_to_string(&mut buf)?; + Ok(serde_json::from_str(&buf)?) + } +} + #[derive(Default, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Administrators { pub roles: Vec, pub users: Vec, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Introduction { pub channels: Vec, pub response: Response, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct MessageResponder { pub includes: Includes, pub excludes: Excludes, @@ -39,14 +56,12 @@ pub struct MessageResponder { } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Response { pub message: Option, pub embed: Option, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Embed { pub title: String, pub description: String, @@ -59,7 +74,6 @@ pub struct Embed { } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Field { pub name: String, pub value: String, @@ -67,36 +81,29 @@ pub struct Field { } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Footer { pub text: String, - #[serde(rename = "icon_url")] pub icon_url: String, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Image { pub url: String, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Thumbnail { pub url: String, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Author { pub name: String, - #[serde(rename = "icon_url")] pub icon_url: String, pub url: String, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Includes { pub channels: Vec, #[serde(rename = "match")] @@ -104,7 +111,6 @@ pub struct Includes { } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Excludes { pub roles: Vec, #[serde(rename = "match")] @@ -112,38 +118,11 @@ pub struct Excludes { } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct Condition { pub user: User, } #[derive(Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] pub struct User { - #[serde(rename = "server-age")] pub server_age: i64, } - -impl BotConfiguration { - fn save(&self) -> Result<(), Error> { - let mut file = File::create("configuration.json")?; - let json = serde_json::to_string_pretty(&self)?; - file.write(json.as_bytes())?; - Ok(()) - } - - pub fn load() -> Result { - let mut file = match File::open("configuration.json") { - Ok(file) => file, - Err(_) => { - let configuration = BotConfiguration::default(); - configuration.save()?; - return Ok(configuration); - }, - }; - - let mut buf = String::new(); - file.read_to_string(&mut buf)?; - Ok(serde_json::from_str(&buf)?) - } -} diff --git a/src/configuration/mod.rs b/src/configuration/mod.rs new file mode 100644 index 0000000..af7b1fe --- /dev/null +++ b/src/configuration/mod.rs @@ -0,0 +1 @@ +pub mod application; diff --git a/src/main.rs b/src/main.rs index 7fa13d4..e5914a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use chrono::{DateTime, Duration, NaiveDateTime, Utc}; -use configuration::BotConfiguration; +use configuration::application::Configuration; use log::{error, info, trace, LevelFilter}; use logger::logging::SimpleLogger; use regex::Regex; @@ -17,19 +17,19 @@ mod logger; static LOGGER: SimpleLogger = SimpleLogger; -struct Configuration; +struct BotConfiguration; -impl TypeMapKey for Configuration { - type Value = Arc>; +impl TypeMapKey for BotConfiguration { + type Value = Arc>; } pub struct Handler; -async fn get_configuration_lock(ctx: &Context) -> Arc> { +async fn get_configuration_lock(ctx: &Context) -> Arc> { ctx.data .read() .await - .get::() + .get::() .expect("Expected Configuration in TypeMap.") .clone() } @@ -38,8 +38,8 @@ fn contains_match(strings: &Vec, text: &String) -> bool { strings.iter().any(|regex| Regex::new(regex).unwrap().is_match(&text)) } -fn load_configuration() -> BotConfiguration { - BotConfiguration::load().expect("Failed to load configuration") +fn load_configuration() -> Configuration { + Configuration::load().expect("Failed to load configuration") } #[async_trait] @@ -232,7 +232,7 @@ async fn main() { .await .expect("Failed to create client"); - client.data.write().await.insert::(Arc::new(RwLock::new(configuration))); + client.data.write().await.insert::(Arc::new(RwLock::new(configuration))); if let Err(why) = client.start().await { error!("{:?}", why);