squad-quest/discord/src/error.rs
2ndbeam cc916c06ce feat: Implemented daily timer
- Bump version to 0.10.0
- Added /timer command
2025-12-18 15:58:18 +03:00

94 lines
3.3 KiB
Rust

use std::fmt::Display;
use poise::serenity_prelude as serenity;
use squad_quest::error::MapError;
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
QuestNotFound(u16),
QuestIsPublic(u16),
QuestIsCompleted(u16),
NoContent,
NoChannelOrUser,
BothChannelAndUser,
SerenityError(serenity::Error),
SquadQuestError(squad_quest::error::Error),
AccountNotFound,
AccountIsSelf,
InsufficientFunds(u32),
RoomNotFound(u16),
RoomAlreadyUnlocked(u16),
CannotReach(u16),
TimerSet,
}
impl From<serenity::Error> for Error {
fn from(value: serenity::Error) -> Self {
Self::SerenityError(value)
}
}
impl From<squad_quest::error::Error> for Error {
fn from(value: squad_quest::error::Error) -> Self {
Self::SquadQuestError(value)
}
}
impl From<squad_quest::error::MapError> for Error {
fn from(value: squad_quest::error::MapError) -> Self {
match value {
MapError::RoomNotFound(id) => Self::RoomNotFound(id),
MapError::RoomAlreadyUnlocked(id, _) => Self::RoomAlreadyUnlocked(id),
MapError::InsufficientFunds(_, _, amount) => Self::InsufficientFunds(amount),
MapError::CannotReach(id, _) => Self::CannotReach(id),
_ => unreachable!(),
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::QuestNotFound(u16) => write!(f, "quest #{u16} not found"),
Self::QuestIsPublic(u16) => write!(f, "quest #{u16} is already public"),
Self::QuestIsCompleted(u16) => write!(f, "quest #{u16} is already completed for this user"),
Self::NoContent => write!(f, "no text or attachment was specified"),
Self::NoChannelOrUser => write!(f, "no channel or user was specified"),
Self::BothChannelAndUser => write!(f, "both channel and user was specified"),
Self::SerenityError(_) => write!(f, "discord interaction error"),
Self::SquadQuestError(_) => write!(f, "internal logic error"),
Self::AccountNotFound => write!(f, "account not found"),
Self::AccountIsSelf => write!(f, "given account is the same as command user"),
Self::InsufficientFunds(amount) => write!(f, "user does not have {amount} points"),
Self::RoomNotFound(id) => write!(f, "room #{id} not found"),
Self::RoomAlreadyUnlocked(id) => write!(f, "room #{id} is already unlocked for this user"),
Self::CannotReach(id) => write!(f, "user cannot reach room #{id}"),
Self::TimerSet => write!(f, "timer is already set"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::QuestNotFound(_) |
Self::QuestIsPublic(_) |
Self::QuestIsCompleted(_) |
Self::NoContent |
Self::NoChannelOrUser |
Self::BothChannelAndUser |
Self::AccountNotFound |
Self::AccountIsSelf |
Self::InsufficientFunds(_) |
Self::RoomNotFound(_) |
Self::RoomAlreadyUnlocked(_) |
Self::CannotReach(_) |
Self::TimerSet => None,
Self::SerenityError(error) => Some(error),
Self::SquadQuestError(error) => Some(error),
}
}
}