1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::fmt;

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

use self::fancy_string::FancyText;

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct JavaServerInfo {
    pub version: Option<ServerVersion>,
    pub players: Option<ServerPlayers>,
    #[serde(deserialize_with = "de_description")]
    pub description: ServerDescription,
    pub favicon: Option<String>,
    #[serde(rename = "deserialize_description")]
    pub mod_info: Option<ServerModInfo>,
}

fn de_description<'de, D>(deserializer: D) -> Result<ServerDescription, D::Error>
where
    D: Deserializer<'de>,
{
    struct DeDescription;

    impl<'de> Visitor<'de> for DeDescription {
        type Value = ServerDescription;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("string or map")
        }

        fn visit_str<E>(self, value: &str) -> Result<ServerDescription, E>
        where
            E: de::Error,
        {
            Ok(ServerDescription {
                text: value.to_owned(),
                extra: None,
            })
        }

        fn visit_map<M>(self, map: M) -> Result<ServerDescription, M::Error>
        where
            M: MapAccess<'de>,
        {
            Deserialize::deserialize(de::value::MapAccessDeserializer::new(map))
        }
    }

    deserializer.deserialize_any(DeDescription)
}

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerVersion {
    pub name: String,
    pub protocol: u32,
}

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerPlayers {
    pub max: u32,
    pub online: u32,
    pub sample: Option<Vec<ServerPlayersSample>>,
}

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerPlayersSample {
    pub name: Option<String>,
    pub id: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerDescription {
    pub text: String,
    pub extra: Option<FancyText>,
}

#[derive(Serialize, Deserialize, Debug, Hash, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ServerModInfo {
    #[serde(rename = "type")]
    pub loader_type: String,
    // pub mod_list: Vec<ServerModInfoMod>,
}

impl std::str::FromStr for JavaServerInfo {
    type Err = serde_json::Error;
    fn from_str(json: &str) -> Result<Self, Self::Err> {
        serde_json::from_str(json)
    }
}

pub mod fancy_string {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq, Eq)]
    pub struct FancyText(pub Vec<FancyTextComponent>);

    impl FancyText {
        pub fn to_markdown(&self) -> String {
            let mut builder = String::with_capacity(10);
            for component in &self.0 {
                builder += &component.to_markdown();
            }
            builder
        }
    }

    #[derive(Debug, Serialize, Deserialize, Hash, Clone, PartialEq, Eq)]
    #[serde(untagged)]
    pub enum FancyTextComponent {
        ColorText {
            color: String,
            text: String,
        },
        #[serde(deserialize_with = "de_plain_text")]
        PlainText {
            text: String,
        },
        NestedText {
            #[serde(default)]
            bold: bool,
            #[serde(default)]
            italic: bool,
            #[serde(default)]
            underlined: bool,
            #[serde(default)]
            strikethrough: bool,
            #[serde(default)]
            obfuscated: bool,
            extra: FancyText,
        },
    }

    impl FancyTextComponent {
        pub fn to_markdown(&self) -> String {
            match self {
                FancyTextComponent::ColorText { color: _, text } => text.clone(),
                FancyTextComponent::PlainText { text } => text.clone(),
                FancyTextComponent::NestedText {
                    bold,
                    italic,
                    underlined,
                    strikethrough,
                    obfuscated: _,
                    extra,
                } => {
                    let mut text = extra.to_markdown();
                    if *bold {
                        text = format!("**{text}**");
                    }
                    if *italic {
                        text = format!("*{text}*");
                    }
                    if *underlined {
                        text = format!("__{text}__");
                    }
                    if *strikethrough {
                        text = format!("~~{text}~~");
                    }
                    text
                }
            }
        }
    }

    fn de_plain_text<'de, D>(deserializer: D) -> Result<String, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct DePlainText;

        impl<'de> serde::de::Visitor<'de> for DePlainText {
            type Value = String;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("string or plain text object")
            }

            fn visit_str<E>(self, value: &str) -> Result<String, E>
            where
                E: serde::de::Error,
            {
                Ok(value.to_owned())
            }

            fn visit_map<M>(self, map: M) -> Result<String, M::Error>
            where
                M: serde::de::MapAccess<'de>,
            {
                serde::Deserialize::deserialize(serde::de::value::MapAccessDeserializer::new(map))
            }
        }

        deserializer.deserialize_any(DePlainText)
    }
}