transcode/
transcode.rs

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
use byte_transcoder::{reader::ByteReader, reader_error::ByteReaderResult};
use uuid::Uuid;

#[allow(dead_code)]
#[derive(Debug)]
pub struct Payload {
    game_id: Uuid,
    join_code: String,
    lobby: Vec<Account>,
}

#[derive(Debug)]
pub struct Account {
    pub account_id: Uuid,
    pub username: String,
}

fn main() -> ByteReaderResult<()> {
    #[rustfmt::skip]
    let bytes: Vec<u8> = vec![
        // Game ID (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,

        // Join Code (String)
        4, 84, 111, 100, 100,

        // Lobby size (u8)
        2,

        // Account 1
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,

        // Account 2
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,
    ];

    let mut byte_reader: ByteReader = ByteReader::new(&bytes);

    let game_id: Uuid = byte_reader.read_uuid()?;
    let join_code: String = byte_reader.read_string()?;

    let mut lobby: Vec<Account> = Vec::new();
    let lobby_len: u8 = byte_reader.read_u8()?;
    for _ in 0..lobby_len {
        lobby.push(Account {
            account_id: byte_reader.read_uuid()?,
            username: byte_reader.read_string()?,
        });
    }

    println!(
        "{:?}",
        Payload {
            game_id,
            join_code,
            lobby,
        }
    );
    Ok(())
}