use std::fmt::Display;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default, Hash)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum Loader {
#[default]
Vanilla,
Forge,
NeoForged,
Fabric,
Quilt,
LiteLoader,
Risugamis,
Rift,
Paper,
Sponge,
SpongeForge,
CraftBukkit,
Spigot,
Glowstone,
Pufferfish,
Purpur,
Folia,
Any,
#[cfg_attr(not(feature = "schema"), serde(untagged))]
Unknown(String),
}
impl Loader {
pub fn parse_from_str(string: &str) -> Self {
match string {
"vanilla" => Self::Vanilla,
"forge" => Self::Forge,
"neoforged" => Self::NeoForged,
"fabric" => Self::Fabric,
"quilt" => Self::Quilt,
"liteloader" => Self::LiteLoader,
"risugamis" => Self::Risugamis,
"rift" => Self::Rift,
"paper" => Self::Paper,
"sponge" => Self::Sponge,
"craftbukkit" => Self::CraftBukkit,
"spigot" => Self::Spigot,
"glowstone" => Self::Glowstone,
"pufferfish" => Self::Pufferfish,
"purpur" => Self::Purpur,
"folia" => Self::Folia,
"any" => Self::Any,
other => Self::Unknown(other.to_string()),
}
}
pub fn is_client(&self) -> bool {
match self {
Self::Vanilla
| Self::Forge
| Self::NeoForged
| Self::Fabric
| Self::Quilt
| Self::LiteLoader
| Self::Risugamis
| Self::Rift
| Self::Any
| Self::Unknown(..) => true,
_ => false,
}
}
pub fn is_server(&self) -> bool {
match self {
Self::Vanilla
| Self::Forge
| Self::NeoForged
| Self::Fabric
| Self::Quilt
| Self::Paper
| Self::Sponge
| Self::SpongeForge
| Self::CraftBukkit
| Self::Spigot
| Self::Glowstone
| Self::Pufferfish
| Self::Purpur
| Self::Folia
| Self::Any
| Self::Unknown(..) => true,
_ => false,
}
}
}
impl Display for Loader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Vanilla => write!(f, "Vanilla"),
Self::Forge => write!(f, "Forge"),
Self::NeoForged => write!(f, "NeoForged"),
Self::Fabric => write!(f, "Fabric"),
Self::Quilt => write!(f, "Quilt"),
Self::LiteLoader => write!(f, "LiteLoader"),
Self::Risugamis => write!(f, "Risugami's"),
Self::Rift => write!(f, "Rift"),
Self::Paper => write!(f, "Paper"),
Self::Sponge => write!(f, "Sponge"),
Self::SpongeForge => write!(f, "SpongeForge"),
Self::CraftBukkit => write!(f, "CraftBukkit"),
Self::Spigot => write!(f, "Spigot"),
Self::Glowstone => write!(f, "Glowstone"),
Self::Pufferfish => write!(f, "Pufferfish"),
Self::Purpur => write!(f, "Purpur"),
Self::Folia => write!(f, "Folia"),
Self::Any => write!(f, "Any"),
Self::Unknown(other) => write!(f, "{other}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, Hash)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum LoaderMatch {
FabricLike,
ForgeLike,
Bukkit,
#[serde(untagged)]
Loader(Loader),
}
impl LoaderMatch {
pub fn parse_from_str(string: &str) -> Self {
match string {
"fabriclike" => Self::FabricLike,
"forgelike" => Self::ForgeLike,
"bukkit" => Self::Bukkit,
other => LoaderMatch::Loader(Loader::parse_from_str(other)),
}
}
pub fn matches(&self, other: &Loader) -> bool {
if other == &Loader::Any {
return true;
}
match self {
Self::FabricLike => matches!(other, Loader::Fabric | Loader::Quilt),
Self::ForgeLike => matches!(other, Loader::Forge | Loader::SpongeForge),
Self::Bukkit => matches!(
other,
Loader::Paper
| Loader::CraftBukkit
| Loader::Spigot
| Loader::Glowstone
| Loader::Pufferfish
| Loader::Purpur
),
Self::Loader(loader) => loader == other,
}
}
}
impl Display for LoaderMatch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FabricLike => write!(f, "Fabric-like"),
Self::ForgeLike => write!(f, "Forge-like"),
Self::Bukkit => write!(f, "Bukkit"),
Self::Loader(loader) => loader.fmt(f),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum Proxy {
BungeeCord,
Waterfall,
Velocity,
#[cfg_attr(not(feature = "schema"), serde(untagged))]
Unknown(String),
}
impl Proxy {
pub fn parse_from_str(string: &str) -> Self {
match string {
"bungeecord" => Self::BungeeCord,
"waterfall" => Self::Waterfall,
"velocity" => Self::Velocity,
other => Self::Unknown(other.into()),
}
}
}
impl Display for Proxy {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::BungeeCord => write!(f, "BungeeCord"),
Self::Waterfall => write!(f, "Waterfall"),
Self::Velocity => write!(f, "Velocity"),
Self::Unknown(other) => write!(f, "{other}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "lowercase")]
pub enum ProxyMatch {
BungeeCordLike,
#[serde(untagged)]
Proxy(Proxy),
}
impl ProxyMatch {
pub fn parse_from_str(string: &str) -> Self {
match string {
"bungeecordlike" => Self::BungeeCordLike,
other => Self::Proxy(Proxy::parse_from_str(other)),
}
}
pub fn matches(&self, other: &Proxy) -> bool {
match self {
Self::BungeeCordLike => matches!(other, Proxy::BungeeCord | Proxy::Waterfall),
Self::Proxy(proxy) => proxy == other,
}
}
}