diff --git a/discord/src/config.rs b/discord/src/config.rs deleted file mode 100644 index 8323e3d..0000000 --- a/discord/src/config.rs +++ /dev/null @@ -1,79 +0,0 @@ -use std::{io::Write, path::PathBuf}; - -use poise::serenity_prelude::{ChannelId, GuildId, MessageId}; -use serde::{Serialize, Deserialize}; -use squad_quest::{SquadObject, config::Config, error::Error}; - -pub trait ConfigImpl { - fn discord_impl(&self) -> Result; -} - -impl ConfigImpl for Config { - fn discord_impl(&self) -> Result { - let Some(path) = &self.impl_path else { - return Err(Error::IsNotImplemented); - }; - DiscordConfig::load(path.clone()) - } -} - -#[derive(Serialize, Deserialize)] -pub struct DiscordConfig { - pub guild: GuildId, - pub quests_channel: ChannelId, - pub answers_channel: ChannelId, - pub quests_messages: Vec, -} - -impl SquadObject for DiscordConfig { - fn load(path: PathBuf) -> Result { - match std::fs::read_to_string(path) { - Ok(string) => { - match toml::from_str::(&string) { - Ok(object) => Ok(object), - Err(error) => Err(Error::TomlDeserializeError(error)) - } - }, - Err(error) => Err(Error::IoError(error)) - } - } - - fn delete(path: PathBuf) -> Result<(), Error> { - match Self::load(path.clone()) { - Ok(_) => { - if let Err(error) = std::fs::remove_file(path) { - return Err(Error::IoError(error)); - } - - Ok(()) - }, - Err(error) => Err(error) - } - } - - fn save(&self, path: PathBuf) -> Result<(), Error> { - let filename = "discord.toml".to_string(); - let mut full_path = path; - full_path.push(filename); - - let str = match toml::to_string_pretty(&self) { - Ok(string) => string, - Err(error) => { - return Err(Error::TomlSerializeError(error)); - } - }; - - let mut file = match std::fs::File::create(full_path) { - Ok(f) => f, - Err(error) => { - return Err(Error::IoError(error)); - } - }; - - if let Err(error) = file.write_all(str.as_bytes()) { - return Err(Error::IoError(error)); - } - - Ok(()) - } -} diff --git a/discord/src/main.rs b/discord/src/main.rs index deae174..cbbdc3b 100644 --- a/discord/src/main.rs +++ b/discord/src/main.rs @@ -3,15 +3,11 @@ use dotenvy::dotenv; use poise::serenity_prelude as serenity; use squad_quest::config::Config; -use crate::config::{ConfigImpl, DiscordConfig}; - mod commands; mod cli; -mod config; struct Data { pub config: Config, - pub discord: DiscordConfig, } type Error = Box; type Context<'a> = poise::Context<'a, Data, Error>; @@ -22,7 +18,6 @@ async fn main() { let cli = cli::Cli::parse(); let config = Config::load(cli.config.clone()); - let discord = config.discord_impl().expect("config does not define impl_path"); let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"); let intents = serenity::GatewayIntents::non_privileged(); @@ -35,10 +30,7 @@ async fn main() { .setup(|ctx, _ready, framework| { Box::pin(async move { poise::builtins::register_globally(ctx, &framework.options().commands).await?; - Ok(Data { - config, - discord, - }) + Ok(Data {config}) }) }) .build(); diff --git a/src/config/mod.rs b/src/config/mod.rs index 9544042..f0b2f94 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -24,9 +24,6 @@ pub struct Config { /// If true, print to std{out/err} pub verbose: bool, - - /// Path to implementation config file - pub impl_path: Option, } impl Default for Config { @@ -37,7 +34,6 @@ impl Default for Config { accounts_path: "accounts".into(), map: "map.toml".into(), verbose: true, - impl_path: None, } } } @@ -357,19 +353,4 @@ impl Config { path.push(self.map.clone()); path } - - /// Returns full path to implementation config TOML, if defined - /// This path will be relative to working directory - /// Only makes sense if using inside binary crate, - /// which provides implementation config - pub fn full_impl_path(&self) -> Option { - match &self.impl_path { - Some(impl_path) => { - let mut path = self.path.clone(); - path.push(impl_path.clone()); - Some(path) - }, - None => None, - } - } } diff --git a/src/error.rs b/src/error.rs index abb475c..0804b58 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,8 +2,7 @@ use std::{fmt, path::PathBuf}; -/// Error enum, which acts as different Error structs wrapper, -/// while also handling some other errors +/// Error struct #[derive(Debug)] #[non_exhaustive] pub enum Error { @@ -15,18 +14,15 @@ pub enum Error { TomlSerializeError(toml::ser::Error), /// toml::de::Error happened when loading TomlDeserializeError(toml::de::Error), - /// Implementation config is None, so crate binary is not implemented. - IsNotImplemented, } impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::IsNotAFile(path) => write!(f, "{path:?} is not a file"), - Self::IoError(error) => write!(f, "{error}"), - Self::TomlSerializeError(error) => write!(f, "{error}"), - Self::TomlDeserializeError(error) => write!(f, "{error}"), - Self::IsNotImplemented => write!(f, "implementation not found"), + Error::IsNotAFile(path) => write!(f, "{:?} is not a file", path), + Error::IoError(error) => write!(f, "io error: {error}"), + Error::TomlSerializeError(error) => write!(f, "serialize error: {error}"), + Error::TomlDeserializeError(error) => write!(f, "parse error: {error}") } } }