use std::collections::HashMap;
use base64::{Engine as _, engine::general_purpose::STANDARD};
use serde::{Deserialize, Serialize};
use url::Url;
use crate::{Error, McUuid, Result};
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ProfileProperty {
pub name: String,
pub value: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TexturesPayload {
pub timestamp: u64,
pub profile_id: McUuid,
pub profile_name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signature_required: Option<bool>,
#[serde(default)]
pub textures: HashMap<TextureType, TextureEntry>,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum TextureType {
Skin,
Cape,
}
impl TextureType {
#[must_use]
pub const fn as_path_segment(self) -> &'static str {
match self {
TextureType::Skin => "skin",
TextureType::Cape => "cape",
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextureEntry {
pub url: Url,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<TextureMetadata>,
}
#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextureMetadata {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
}
pub fn decode_textures_value(value: &str) -> Result<TexturesPayload> {
let decoded = STANDARD.decode(value.as_bytes()).map_err(Error::base64)?;
Ok(serde_json::from_slice(&decoded)?)
}
pub fn decode_textures_property(properties: &[ProfileProperty]) -> Result<Option<TexturesPayload>> {
let Some(prop) = properties.iter().find(|p| p.name == "textures") else {
return Ok(None);
};
decode_textures_value(&prop.value).map(Some)
}
#[cfg(any(feature = "mojang", feature = "yggdrasil"))]
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct JoinServerBody<'a> {
pub access_token: &'a str,
pub selected_profile: McUuid,
pub server_id: &'a str,
}