feat: Added DiscordConfig for future usage
- Added crate::error::Error::IsNotImplemented - Added Config::full_impl_path - Added Config::discord_impl for discord crate
This commit is contained in:
parent
1142fe6ad9
commit
b92eaa1241
4 changed files with 106 additions and 1 deletions
79
discord/src/config.rs
Normal file
79
discord/src/config.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -357,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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ 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 {
|
||||
|
|
@ -24,6 +26,7 @@ impl fmt::Display for Error {
|
|||
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue