use base64::prelude::*;
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{Decode, Encode};
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Encode, Decode)]
pub struct Property<S = String> {
pub name: S,
pub value: S,
pub signature: Option<S>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct PlayerTextures {
pub skin: Url,
pub cape: Option<Url>,
}
impl PlayerTextures {
pub fn try_from_textures(textures: &str) -> anyhow::Result<Self> {
#[derive(Debug, Deserialize)]
struct Textures {
textures: PlayerTexturesPayload,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
struct PlayerTexturesPayload {
skin: TextureUrl,
#[serde(default)]
cape: Option<TextureUrl>,
}
#[derive(Debug, Deserialize)]
struct TextureUrl {
url: Url,
}
let decoded = BASE64_STANDARD.decode(textures.as_bytes())?;
let Textures { textures } = serde_json::from_slice(&decoded)?;
Ok(Self {
skin: textures.skin.url,
cape: textures.cape.map(|t| t.url),
})
}
}