transcode/
transcode.rs

1use byte_transcoder::{endian::Endian, reader::ByteReader, reader_error::ByteReaderResult};
2use uuid::Uuid;
3
4#[allow(dead_code)]
5#[derive(Debug)]
6pub struct Payload {
7    game_id: Uuid,
8    join_code: String,
9    lobby: Vec<Account>,
10}
11
12#[derive(Debug)]
13pub struct Account {
14    pub account_id: Uuid,
15    pub username: String,
16}
17
18fn main() -> ByteReaderResult<()> {
19    #[rustfmt::skip]
20    let bytes: Vec<u8> = vec![
21        // Game ID (UUIDv4)
22        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
23
24        // Join Code (String)
25        4, 84, 111, 100, 100,
26
27        // Lobby size (u8)
28        2,
29
30        // Account 1
31        // (UUIDv4)
32        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
33        // (String)
34        4, 84, 111, 100, 100,
35
36        // Account 2
37        // (UUIDv4)
38        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
39        // (String)
40        4, 84, 111, 100, 100,
41    ];
42
43    let mut byte_reader: ByteReader = ByteReader::new(&bytes, Endian::Little);
44
45    let game_id: Uuid = byte_reader.read_uuid()?;
46    let join_code: String = byte_reader.read_string()?;
47
48    let mut lobby: Vec<Account> = Vec::new();
49    let lobby_len: u8 = byte_reader.read_u8()?;
50    for _ in 0..lobby_len {
51        lobby.push(Account {
52            account_id: byte_reader.read_uuid()?,
53            username: byte_reader.read_string()?,
54        });
55    }
56
57    println!(
58        "{:?}",
59        Payload {
60            game_id,
61            join_code,
62            lobby,
63        }
64    );
65    Ok(())
66}