1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-11-30 10:40:50 +01:00

make SetConVarMessage use Cow instead of String

This commit is contained in:
Robin Appelman 2026-11-07 16:54:25 +01:00
commit 1f7a80468e
Signed by: robin
SSH key fingerprint: SHA256:kifqgrqnut7q1YUIrI0AdMNC23qiKAS/T9uGFmIYvbE
3 changed files with 21 additions and 15 deletions

View file

@ -1,3 +1,7 @@
## 0.7.3
- `SetConVar` message now uses `Cow` instead of `String`
## 0.7.2 ## 0.7.2
- Compress string tables, this fixes playback of encoded demos with large string - Compress string tables, this fixes playback of encoded demos with large string

View file

@ -79,7 +79,7 @@ pub enum Message<'a> {
File(FileMessage), File(FileMessage),
NetTick(NetTickMessage), NetTick(NetTickMessage),
StringCmd(StringCmdMessage), StringCmd(StringCmdMessage),
SetConVar(SetConVarMessage), SetConVar(SetConVarMessage<'a>),
SignOnState(SignOnStateMessage), SignOnState(SignOnStateMessage),
Print(PrintMessage), Print(PrintMessage),
ServerInfo(Box<ServerInfoMessage>), ServerInfo(Box<ServerInfoMessage>),

View file

@ -1,3 +1,5 @@
use std::borrow::Cow;
use bitbuffer::{BitRead, BitReadStream, Endianness}; use bitbuffer::{BitRead, BitReadStream, Endianness};
#[cfg(feature = "write")] #[cfg(feature = "write")]
use bitbuffer::{BitWrite, BitWriteStream}; use bitbuffer::{BitWrite, BitWriteStream};
@ -8,33 +10,33 @@ use crate::ReadResult;
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "write", derive(BitWrite))] #[cfg_attr(feature = "write", derive(BitWrite))]
pub struct ConVar { pub struct ConVar<'a> {
pub key: String, pub key: Cow<'a, str>,
pub value: String, pub value: Cow<'a, str>,
} }
impl<E: Endianness> BitRead<'_, E> for ConVar { impl<'a, E: Endianness> BitRead<'a, E> for ConVar<'a> {
fn read(stream: &mut BitReadStream<'_, E>) -> ReadResult<Self> { fn read(stream: &mut BitReadStream<'a, E>) -> ReadResult<Self> {
let key = stream let key = stream
.read() .read()
.unwrap_or_else(|_| "Malformed cvar name".to_string()); .unwrap_or_else(|_| "Malformed cvar name".into());
let value = stream let value = stream
.read() .read()
.unwrap_or_else(|_| "Malformed cvar value".to_string()); .unwrap_or_else(|_| "Malformed cvar value".into());
Ok(ConVar { key, value }) Ok(ConVar { key, value })
} }
} }
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[derive(Debug, BitRead, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, BitRead, PartialEq, Serialize, Deserialize, Clone)]
pub struct SetConVarMessage { pub struct SetConVarMessage<'a> {
pub length: u8, pub length: u8,
#[size = "length"] #[size = "length"]
pub vars: Vec<ConVar>, pub vars: Vec<ConVar<'a>>,
} }
#[cfg(feature = "write")] #[cfg(feature = "write")]
impl<E: Endianness> BitWrite<E> for SetConVarMessage { impl<E: Endianness> BitWrite<E> for SetConVarMessage<'_> {
fn write(&self, stream: &mut BitWriteStream<E>) -> ReadResult<()> { fn write(&self, stream: &mut BitWriteStream<E>) -> ReadResult<()> {
self.length.write(stream)?; self.length.write(stream)?;
self.vars.write(stream) self.vars.write(stream)
@ -51,12 +53,12 @@ fn test_set_con_var_roundtrip() {
length: 2, length: 2,
vars: vec![ vars: vec![
ConVar { ConVar {
key: "foo1".to_string(), key: "foo1".into(),
value: "bar1".to_string(), value: "bar1".into(),
}, },
ConVar { ConVar {
key: "foo2".to_string(), key: "foo2".into(),
value: "bar2".to_string(), value: "bar2".into(),
}, },
], ],
}); });