use std::{convert::Infallible, str::FromStr};
use api_builder::APIClientError;
use serde::Deserialize;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, thiserror::Error)]
pub enum LuarmorMessage {
#[error("API is up and working")]
APIWorking,
#[error("Invalid API key! Visit https://luarmor.net/ to get access")]
IncorrectAPIKey,
#[error("Wrong API key")]
InvalidAPIKey,
#[error("Success")]
Success,
#[error("User has been deleted")]
UserDeleted,
#[error("Key not found")]
KeyNotFound,
#[error("Project not found")]
ProjectNotFound,
#[error("Successfully reset")]
SuccessReset,
#[error("User is on cooldown")]
UserCooldown,
#[error("User key does not exist")]
UserKeyNotFound,
#[error("The key already has a Discord linked to it")]
DiscordAlreadyLinked,
#[error("nothing to see here")]
NothingToSee,
#[error("Identifier already exists")]
IdentifierAlreadyExists,
#[error("Invalid Discord ID")]
InvalidDiscordId,
#[error("Discord ID successfully linked")]
DiscordIdSuccess,
#[error("Reset HWID is disabled for this script")]
ResetHWIDDisabled,
#[error("User has been edited successfully")]
EditSuccess,
#[error("Discord ID already exists")]
DiscordAlreadyExists,
#[error("{0}")]
Other(String),
}
impl APIClientError for LuarmorMessage {}
impl FromStr for LuarmorMessage {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"API is up and working!" => Self::APIWorking,
"Invalid API key! Visit https://luarmor.net/ to get access." => Self::IncorrectAPIKey,
"Wrong API key" => Self::InvalidAPIKey,
"Success!" => Self::Success,
"User has been deleted!" => Self::UserDeleted,
"Key not found" => Self::KeyNotFound,
"Project not found!" => Self::ProjectNotFound,
"Successfully reset!" => Self::SuccessReset,
"User is on cooldown." => Self::UserCooldown,
"User key doesn't exist" => Self::UserKeyNotFound,
"This key already has a discord linked to it" => Self::DiscordAlreadyLinked,
"nothing to see here." => Self::NothingToSee,
"Identifier already exists." => Self::IdentifierAlreadyExists,
"Invalid discord_id" => Self::InvalidDiscordId,
"Discord ID successfully linked!" => Self::DiscordIdSuccess,
"Reset Hwid is disabled for this script" => Self::ResetHWIDDisabled,
"User has been edited successfully!" => Self::EditSuccess,
"Project not found" => Self::ProjectNotFound,
"Discord ID already exists" => Self::DiscordAlreadyExists,
"Discord ID already exists." => Self::DiscordAlreadyExists,
"Discord ID already exist." => Self::DiscordAlreadyExists,
s => Self::Other(s.to_string()),
})
}
}
impl<'de> Deserialize<'de> for LuarmorMessage {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(Self::from_str(s.as_str()).unwrap()) }
}
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize)]
pub struct LuarmorResponse<T> {
pub success: bool,
pub message: LuarmorMessage,
#[serde(flatten)]
pub data: Option<T>,
}