steel_protocol/packets/game/
s_spectator_action.rs1use std::io::{Cursor, Result};
2
3use steel_macros::ServerPacket;
4use steel_utils::codec::VarInt;
5use steel_utils::serial::ReadFrom;
6
7#[derive(ServerPacket, Clone, Debug)]
8pub struct SSpectatorAction {
9 pub spectate_entity_id: Option<i32>,
10}
11
12impl ReadFrom for SSpectatorAction {
13 fn read(data: &mut Cursor<&[u8]>) -> Result<Self> {
14 let spectate_entity_id = Option::<VarInt>::read(data)?.map(|id| id.0);
15 Ok(Self { spectate_entity_id })
16 }
17}
18
19#[cfg(test)]
20mod tests {
21 use super::*;
22
23 #[test]
24 fn reads_absent_entity_id() {
25 let mut data = Cursor::new([0].as_slice());
26 let packet = SSpectatorAction::read(&mut data).expect("packet should parse");
27
28 assert_eq!(packet.spectate_entity_id, None);
29 }
30
31 #[test]
32 fn reads_present_entity_id_as_varint() {
33 let mut data = Cursor::new([1, 62].as_slice());
34 let packet = SSpectatorAction::read(&mut data).expect("packet should parse");
35
36 assert_eq!(packet.spectate_entity_id, Some(62));
37 }
38}