1use borrowme::borrowme;
4
5use crate::cursor::{self, Cursor};
6
7#[derive(thiserror::Error, Debug)]
8#[error(transparent)]
9pub struct Error(#[from] cursor::Error);
11
12pub(crate) const REQUEST_HEADER: &[u8] = b"U";
13pub(crate) const RESPONSE_HEADER: u8 = b'D';
14
15#[borrowme]
16#[derive(Debug)]
17#[owned_attr(derive(Clone))]
18#[owned_attr(doc(hidden))]
19pub struct Player<'a> {
24 pub index: u8,
26 pub name: &'a str,
28 pub score: i32,
30 pub duration: f32
32}
33
34#[borrowme]
35#[derive(Debug)]
36#[owned_attr(derive(Clone))]
37#[owned_attr(doc(hidden))]
38pub struct Players<'a> {
44 pub reported_num: u8,
46 pub players: Vec<Player<'a>>
48}
49
50impl<'a> Player<'a> {
51 pub fn to_owned(self) -> OwnedPlayer {
53 borrowme::to_owned(self)
54 }
55
56 fn parse(cur: &mut Cursor<'a>) -> Result<Self, Error> {
57 Ok(Self {
58 index: cur.read_byte()?,
59 name: cur.read_str()?,
60 score: cur.read_long()?,
61 duration: cur.read_float()?,
62 })
63 }
64}
65
66impl<'a> Players<'a> {
67 pub(crate) fn parse(raw: &'a [u8]) -> Result<Self, Error> {
68 let mut cur = Cursor::new(raw);
69 let reported_num = cur.read_byte()?;
70 let mut players = Vec::with_capacity(reported_num as usize);
71
72 while !cur.is_end() {
73 players.push(Player::parse(&mut cur)?);
74 }
75
76 Ok(Self{reported_num, players})
77 }
78
79 pub fn to_owned(self) -> OwnedPlayers {
81 borrowme::to_owned(self)
82 }
83}