mcapi 0.2.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, Java Runtimes, and SLP)
Documentation
use std::collections::HashMap;
use std::fmt;

use serde::de::{MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};

/// CustomSkinLoader profile JSON returned by `GET /{player}.json` or `GET /csl/{player}.json`.
///
/// Tolerates real-world Blessing Skin `CustomSkinAPI` quirks where unused model slots
/// are serialized as `null` (`{"default": "abc", "slim": null}`) or an empty PHP
/// array (`"skins": []`).
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
pub struct CslProfile {
    pub username: String,
    #[serde(default, deserialize_with = "deserialize_csl_skins")]
    pub skins: HashMap<String, String>,
    #[serde(default)]
    pub cape: Option<String>,
    #[serde(default)]
    pub elytra: Option<String>,
}

fn deserialize_csl_skins<'de, D>(deserializer: D) -> Result<HashMap<String, String>, D::Error>
where
    D: Deserializer<'de>,
{
    struct SkinsVisitor;

    impl<'de> Visitor<'de> for SkinsVisitor {
        type Value = HashMap<String, String>;

        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("a map of model names to optional texture hashes, null, or []")
        }

        fn visit_unit<E>(self) -> Result<Self::Value, E> {
            Ok(HashMap::new())
        }

        fn visit_none<E>(self) -> Result<Self::Value, E> {
            Ok(HashMap::new())
        }

        fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
            while seq.next_element::<serde::de::IgnoredAny>()?.is_some() {}
            Ok(HashMap::new())
        }

        fn visit_map<M: MapAccess<'de>>(self, mut map: M) -> Result<Self::Value, M::Error> {
            let mut out = HashMap::new();
            while let Some((k, v)) = map.next_entry::<String, Option<String>>()? {
                if let Some(hash) = v {
                    out.insert(k, hash);
                }
            }
            Ok(out)
        }
    }

    deserializer.deserialize_any(SkinsVisitor)
}