Compare commits
No commits in common. "b92eaa1241cb5f975570c408b8d8af86ccb4b1d0" and "2cf251009987ef21b0fa86d3f930e0ef74dfe2f1" have entirely different histories.
b92eaa1241
...
2cf2510099
4 changed files with 6 additions and 116 deletions
|
|
@ -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<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,15 +3,11 @@ use dotenvy::dotenv;
|
||||||
use poise::serenity_prelude as serenity;
|
use poise::serenity_prelude as serenity;
|
||||||
use squad_quest::config::Config;
|
use squad_quest::config::Config;
|
||||||
|
|
||||||
use crate::config::{ConfigImpl, DiscordConfig};
|
|
||||||
|
|
||||||
mod commands;
|
mod commands;
|
||||||
mod cli;
|
mod cli;
|
||||||
mod config;
|
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub discord: DiscordConfig,
|
|
||||||
}
|
}
|
||||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||||
type Context<'a> = poise::Context<'a, Data, Error>;
|
type Context<'a> = poise::Context<'a, Data, Error>;
|
||||||
|
|
@ -22,7 +18,6 @@ async fn main() {
|
||||||
|
|
||||||
let cli = cli::Cli::parse();
|
let cli = cli::Cli::parse();
|
||||||
let config = Config::load(cli.config.clone());
|
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 token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
|
||||||
let intents = serenity::GatewayIntents::non_privileged();
|
let intents = serenity::GatewayIntents::non_privileged();
|
||||||
|
|
@ -35,10 +30,7 @@ async fn main() {
|
||||||
.setup(|ctx, _ready, framework| {
|
.setup(|ctx, _ready, framework| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||||
Ok(Data {
|
Ok(Data {config})
|
||||||
config,
|
|
||||||
discord,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,6 @@ pub struct Config {
|
||||||
|
|
||||||
/// If true, print to std{out/err}
|
/// If true, print to std{out/err}
|
||||||
pub verbose: bool,
|
pub verbose: bool,
|
||||||
|
|
||||||
/// Path to implementation config file
|
|
||||||
pub impl_path: Option<PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
|
@ -37,7 +34,6 @@ impl Default for Config {
|
||||||
accounts_path: "accounts".into(),
|
accounts_path: "accounts".into(),
|
||||||
map: "map.toml".into(),
|
map: "map.toml".into(),
|
||||||
verbose: true,
|
verbose: true,
|
||||||
impl_path: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -357,19 +353,4 @@ impl Config {
|
||||||
path.push(self.map.clone());
|
path.push(self.map.clone());
|
||||||
path
|
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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
14
src/error.rs
14
src/error.rs
|
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
use std::{fmt, path::PathBuf};
|
use std::{fmt, path::PathBuf};
|
||||||
|
|
||||||
/// Error enum, which acts as different Error structs wrapper,
|
/// Error struct
|
||||||
/// while also handling some other errors
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
|
@ -15,18 +14,15 @@ pub enum Error {
|
||||||
TomlSerializeError(toml::ser::Error),
|
TomlSerializeError(toml::ser::Error),
|
||||||
/// toml::de::Error happened when loading
|
/// toml::de::Error happened when loading
|
||||||
TomlDeserializeError(toml::de::Error),
|
TomlDeserializeError(toml::de::Error),
|
||||||
/// Implementation config is None, so crate binary is not implemented.
|
|
||||||
IsNotImplemented,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::IsNotAFile(path) => write!(f, "{path:?} is not a file"),
|
Error::IsNotAFile(path) => write!(f, "{:?} is not a file", path),
|
||||||
Self::IoError(error) => write!(f, "{error}"),
|
Error::IoError(error) => write!(f, "io error: {error}"),
|
||||||
Self::TomlSerializeError(error) => write!(f, "{error}"),
|
Error::TomlSerializeError(error) => write!(f, "serialize error: {error}"),
|
||||||
Self::TomlDeserializeError(error) => write!(f, "{error}"),
|
Error::TomlDeserializeError(error) => write!(f, "parse error: {error}")
|
||||||
Self::IsNotImplemented => write!(f, "implementation not found"),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue