use std::io::{Read, Write};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct SMSG_KICK_REASON {
pub reason: u8,
pub text: String,
}
impl crate::private::Sealed for SMSG_KICK_REASON {}
impl SMSG_KICK_REASON {
fn read_inner(mut r: &mut &[u8], body_size: u32) -> Result<Self, crate::errors::ParseErrorKind> {
if !(2..=257).contains(&body_size) {
return Err(crate::errors::ParseErrorKind::InvalidSize);
}
let reason = crate::util::read_u8_le(&mut r)?;
let text = {
let text = crate::util::read_c_string_to_vec(&mut r)?;
String::from_utf8(text)?
};
Ok(Self {
reason,
text,
})
}
}
impl crate::Message for SMSG_KICK_REASON {
const OPCODE: u32 = 0x03c4;
#[cfg(feature = "print-testcase")]
fn message_name(&self) -> &'static str {
"SMSG_KICK_REASON"
}
#[cfg(feature = "print-testcase")]
fn to_test_case_string(&self) -> Option<String> {
use std::fmt::Write;
use crate::traits::Message;
let mut s = String::new();
writeln!(s, "test SMSG_KICK_REASON {{").unwrap();
writeln!(s, " reason = {};", self.reason).unwrap();
writeln!(s, " text = \"{}\";", self.text).unwrap();
writeln!(s, "}} [").unwrap();
let [a, b] = (u16::try_from(self.size() + 2).unwrap()).to_be_bytes();
writeln!(s, " {a:#04X}, {b:#04X}, /* size */").unwrap();
let [a, b] = 964_u16.to_le_bytes();
writeln!(s, " {a:#04X}, {b:#04X}, /* opcode */").unwrap();
let mut bytes: Vec<u8> = Vec::new();
self.write_into_vec(&mut bytes).unwrap();
let mut bytes = bytes.into_iter();
crate::util::write_bytes(&mut s, &mut bytes, 1, "reason", " ");
crate::util::write_bytes(&mut s, &mut bytes, self.text.len() + 1, "text", " ");
writeln!(s, "] {{").unwrap();
writeln!(s, " versions = \"{}\";", std::env::var("WOWM_TEST_CASE_WORLD_VERSION").unwrap_or("2.4.3".to_string())).unwrap();
writeln!(s, "}}\n").unwrap();
Some(s)
}
fn size_without_header(&self) -> u32 {
self.size() as u32
}
fn write_into_vec(&self, mut w: impl Write) -> Result<(), std::io::Error> {
w.write_all(&self.reason.to_le_bytes())?;
assert_ne!(self.text.as_bytes().iter().next_back(), Some(&0_u8), "String `text` must not be null-terminated.");
w.write_all(self.text.as_bytes())?;
w.write_all(&[0])?;
Ok(())
}
fn read_body<S: crate::private::Sealed>(r: &mut &[u8], body_size: u32) -> Result<Self, crate::errors::ParseError> {
Self::read_inner(r, body_size).map_err(|a| crate::errors::ParseError::new(964, "SMSG_KICK_REASON", body_size, a))
}
}
#[cfg(feature = "tbc")]
impl crate::tbc::ServerMessage for SMSG_KICK_REASON {}
impl SMSG_KICK_REASON {
pub(crate) fn size(&self) -> usize {
1 + self.text.len() + 1 }
}