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
use binary_utils::{DataWriter, Result, DataReader, read_utf8_char, write_bytes};
use serde::Serialize;
use tokio::io::{AsyncWrite, AsyncRead};
use super::*;
use crate::ImportantFunctions;

impl DataWriter for Identifier {
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        self.0.write(writer).await
    }
}
impl DataReader for Identifier {
    async fn read(reader: &mut (impl AsyncRead + Unpin)) -> Result<Self> {
        let data = String::read(reader).await?;
        Ok(Self(data))
    }
}
impl<T> From<T> for JSONTextComponent where T: Serialize {
    fn from(value: T) -> Self {
        Self(String::new(serde_json::to_string(&value).unwrap_or(std::string::String::new())))
    }
}
impl DataWriter for JSONTextComponent {
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        self.0.write(writer).await?;
        Ok(())
    }
}
impl DataReader for String {
    async fn read(reader: &mut (impl AsyncRead + Unpin)) -> Result<Self> {
        use crate::VarInt;
        let length = VarInt::read(reader).await?;
        let mut chars = Vec::new();
        for _ in 0..length.get_value() {
            chars.push(read_utf8_char(reader, line!(), file!()).await?);
        }
        let data: std::string::String = chars.iter().collect();
        Ok(Self(data))
    }
}
impl DataWriter for String {
    async fn write(&self, writer: &mut (impl AsyncWrite + Unpin)) -> Result<()> {
        use crate::VarInt;
        let bytes = self.0.as_bytes();
        let length = VarInt::new(bytes.len() as i32);
        length.write(writer).await?;
        write_bytes(writer, bytes).await
    }
}