From 245b6b9926033acd750503ab4947c9514c5e1aa6 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 3 May 2023 08:01:51 +0200 Subject: fuck serde, plus maybe added quilt --- src/lib.rs | 96 +++++++------------------------------------------------------- 1 file changed, 11 insertions(+), 85 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 73b5697..b48bf61 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,12 +12,16 @@ pub use apis::*; use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; pub use commands::*; use error::{ErrorType, MLError, MLE}; -use serde::{Deserialize, Serialize, de::Visitor}; +use serde::{Deserialize, Serialize}; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub enum Modloader { + #[serde(rename(serialize = "fabric", deserialize = "fabric"))] Fabric, + #[serde(rename(serialize = "forge", deserialize = "forge"))] Forge, + #[serde(rename(serialize = "quilt", deserialize = "quilt"))] + Quilt, } impl Modloader { @@ -35,53 +39,16 @@ impl Display for Modloader { match self { Modloader::Fabric => write!(f, "fabric"), Modloader::Forge => write!(f, "forge"), + Modloader::Quilt => write!(f, "quilt"), } } } -impl Serialize for Modloader { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer { - match self { - Modloader::Fabric => serializer.serialize_str("fabric"), - Modloader::Forge => serializer.serialize_str("forge"), - } - } -} - -impl<'de> Deserialize<'de> for Modloader { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct FieldVisitor; - - impl<'de> Visitor<'de> for FieldVisitor { - type Value = Modloader; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("`fabric`, `forge` or `quilt`") - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, { - match v { - "fabric" => Ok(Modloader::Fabric), - "forge" => Ok(Modloader::Forge), - _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"])) - } - } - } - - deserializer.deserialize_identifier(FieldVisitor) - } -} - -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Deserialize, Serialize)] pub enum VersionLevel { + #[serde(rename(serialize = "release", deserialize = "release"))] Release, + #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))] Snapshot, Version(String) } @@ -101,7 +68,7 @@ pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { Ok(()) } -/// Loads game versions +/// Loads game versions from file pub fn load_game_versions(path: &str) -> MLE> { let mut file = File::open(path)?; let mut data = String::new(); @@ -145,44 +112,3 @@ impl VersionLevel { } } } - -impl Serialize for VersionLevel { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer { - match self { - VersionLevel::Release => serializer.serialize_str("release"), - VersionLevel::Snapshot => serializer.serialize_str("snapshot"), - VersionLevel::Version(v) => serializer.serialize_str(v), - } - } -} - -impl<'de> Deserialize<'de> for VersionLevel { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - struct FieldVisitor; - - impl<'de> Visitor<'de> for FieldVisitor { - type Value = VersionLevel; - - fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("`fabric`, `forge` or `quilt`") - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, { - match v { - "release" => Ok(VersionLevel::Release), - "snapshot" => Ok(VersionLevel::Snapshot), - _ => Ok(VersionLevel::Version(String::from(v))) - } - } - } - - deserializer.deserialize_identifier(FieldVisitor) - } -} -- cgit v1.2.3