tf_demo_parser/demo/message/
setconvar.rs1use bitbuffer::{BitRead, BitReadStream, BitWrite, BitWriteStream, Endianness};
2use serde::{Deserialize, Serialize};
3
4use crate::ReadResult;
5
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, BitWrite, PartialEq, Serialize, Deserialize, Clone)]
8pub struct ConVar {
9 pub key: String,
10 pub value: String,
11}
12
13impl<E: Endianness> BitRead<'_, E> for ConVar {
14 fn read(stream: &mut BitReadStream<'_, E>) -> ReadResult<Self> {
15 let key = stream
16 .read()
17 .unwrap_or_else(|_| "Malformed cvar name".to_string());
18 let value = stream
19 .read()
20 .unwrap_or_else(|_| "Malformed cvar value".to_string());
21 Ok(ConVar { key, value })
22 }
23}
24
25#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
26#[derive(Debug, BitRead, PartialEq, Serialize, Deserialize, Clone)]
27pub struct SetConVarMessage {
28 pub length: u8,
29 #[size = "length"]
30 pub vars: Vec<ConVar>,
31}
32
33impl<E: Endianness> BitWrite<E> for SetConVarMessage {
34 fn write(&self, stream: &mut BitWriteStream<E>) -> ReadResult<()> {
35 self.length.write(stream)?;
36 self.vars.write(stream)
37 }
38}
39#[test]
40fn test_set_con_var_roundtrip() {
41 crate::test_roundtrip_write(SetConVarMessage {
42 length: 0,
43 vars: Vec::new(),
44 });
45 crate::test_roundtrip_write(SetConVarMessage {
46 length: 2,
47 vars: vec![
48 ConVar {
49 key: "foo1".to_string(),
50 value: "bar1".to_string(),
51 },
52 ConVar {
53 key: "foo2".to_string(),
54 value: "bar2".to_string(),
55 },
56 ],
57 });
58}