Compare commits

...

2 commits

Author SHA1 Message Date
b92eaa1241 feat: Added DiscordConfig for future usage
- Added crate::error::Error::IsNotImplemented
- Added Config::full_impl_path
- Added Config::discord_impl for discord crate
2025-12-09 16:07:41 +03:00
1142fe6ad9 feat: Added impl_path field to Config struct
- Also clarified error::Error enum
2025-12-09 14:16:23 +03:00
4 changed files with 116 additions and 6 deletions

79
discord/src/config.rs Normal file
View file

@ -0,0 +1,79 @@
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<DiscordConfig, Error>;
}
impl ConfigImpl for Config {
fn discord_impl(&self) -> Result<DiscordConfig, Error> {
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<MessageId>,
}
impl SquadObject for DiscordConfig {
fn load(path: PathBuf) -> Result<Self, squad_quest::error::Error> {
match std::fs::read_to_string(path) {
Ok(string) => {
match toml::from_str::<Self>(&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(())
}
}

View file

@ -3,11 +3,15 @@ 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<dyn std::error::Error + Send + Sync>;
type Context<'a> = poise::Context<'a, Data, Error>;
@ -18,6 +22,7 @@ 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();
@ -30,7 +35,10 @@ async fn main() {
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
Ok(Data {config})
Ok(Data {
config,
discord,
})
})
})
.build();

View file

@ -24,6 +24,9 @@ pub struct Config {
/// If true, print to std{out/err}
pub verbose: bool,
/// Path to implementation config file
pub impl_path: Option<PathBuf>,
}
impl Default for Config {
@ -34,6 +37,7 @@ impl Default for Config {
accounts_path: "accounts".into(),
map: "map.toml".into(),
verbose: true,
impl_path: None,
}
}
}
@ -353,4 +357,19 @@ 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<PathBuf> {
match &self.impl_path {
Some(impl_path) => {
let mut path = self.path.clone();
path.push(impl_path.clone());
Some(path)
},
None => None,
}
}
}

View file

@ -2,7 +2,8 @@
use std::{fmt, path::PathBuf};
/// Error struct
/// Error enum, which acts as different Error structs wrapper,
/// while also handling some other errors
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
@ -14,15 +15,18 @@ 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 {
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}")
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"),
}
}
}