azalea_auth/
game_profile.rs

1use std::collections::HashMap;
2
3use azalea_buf::AzBuf;
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6
7#[derive(AzBuf, Debug, Clone, Default, Eq, PartialEq)]
8pub struct GameProfile {
9    /// The UUID of the player.
10    pub uuid: Uuid,
11    /// The username of the player.
12    pub name: String,
13    pub properties: HashMap<String, ProfilePropertyValue>,
14}
15
16impl GameProfile {
17    pub fn new(uuid: Uuid, name: String) -> Self {
18        GameProfile {
19            uuid,
20            name,
21            properties: HashMap::new(),
22        }
23    }
24}
25
26impl From<SerializableGameProfile> for GameProfile {
27    fn from(value: SerializableGameProfile) -> Self {
28        let mut properties = HashMap::new();
29        for value in value.properties {
30            properties.insert(
31                value.name,
32                ProfilePropertyValue {
33                    value: value.value,
34                    signature: value.signature,
35                },
36            );
37        }
38        Self {
39            uuid: value.id,
40            name: value.name,
41            properties,
42        }
43    }
44}
45
46#[derive(AzBuf, Debug, Clone, Eq, PartialEq)]
47pub struct ProfilePropertyValue {
48    pub value: String,
49    pub signature: Option<String>,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct SerializableGameProfile {
54    pub id: Uuid,
55    pub name: String,
56    pub properties: Vec<SerializableProfilePropertyValue>,
57}
58
59impl From<GameProfile> for SerializableGameProfile {
60    fn from(value: GameProfile) -> Self {
61        let mut properties = Vec::new();
62        for (key, value) in value.properties {
63            properties.push(SerializableProfilePropertyValue {
64                name: key,
65                value: value.value,
66                signature: value.signature,
67            });
68        }
69        Self {
70            id: value.uuid,
71            name: value.name,
72            properties,
73        }
74    }
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct SerializableProfilePropertyValue {
79    pub name: String,
80    pub value: String,
81    pub signature: Option<String>,
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    #[test]
89    fn test_deserialize_game_profile() {
90        let json = r#"{
91            "id": "f1a2b3c4-d5e6-f7a8-b9c0-d1e2f3a4b5c6",
92            "name": "Notch",
93            "properties": [
94                {
95                    "name": "qwer",
96                    "value": "asdf",
97                    "signature": "zxcv"
98                }
99            ]
100        }"#;
101        let profile =
102            GameProfile::from(serde_json::from_str::<SerializableGameProfile>(json).unwrap());
103        assert_eq!(
104            profile,
105            GameProfile {
106                uuid: Uuid::parse_str("f1a2b3c4-d5e6-f7a8-b9c0-d1e2f3a4b5c6").unwrap(),
107                name: "Notch".to_string(),
108                properties: {
109                    let mut map = HashMap::new();
110                    map.insert(
111                        "qwer".to_string(),
112                        ProfilePropertyValue {
113                            value: "asdf".to_string(),
114                            signature: Some("zxcv".to_string()),
115                        },
116                    );
117                    map
118                },
119            }
120        );
121    }
122}