#[cfg(feature = "compression")]
mod inflater;
use super::r#impl::ReceivingEventError;
#[cfg(feature = "compression")]
use inflater::Inflater;
#[derive(Debug)]
pub struct Compression {
#[cfg(feature = "compression")]
inner: Inflater,
#[cfg(not(feature = "compression"))]
inner: Vec<u8>,
}
impl Compression {
#[cfg_attr(
not(feature = "compression"),
allow(clippy::missing_const_for_fn, unused_variables)
)]
pub fn new(shard_id: [u64; 2]) -> Self {
Self {
#[cfg(feature = "compression")]
inner: Inflater::new(shard_id),
#[cfg(not(feature = "compression"))]
inner: Vec::new(),
}
}
pub fn buffer_slice_mut(&mut self) -> &mut [u8] {
#[cfg(feature = "compression")]
{
self.inner.buffer_mut()
}
#[cfg(not(feature = "compression"))]
self.inner.as_mut_slice()
}
pub fn clear(&mut self) {
self.inner.clear();
}
#[cfg_attr(
not(feature = "compression"),
allow(clippy::unused_self, unused_variables)
)]
pub fn extend_binary(&mut self, bytes: &[u8]) -> bool {
#[cfg(feature = "compression")]
{
self.inner.extend(bytes);
true
}
#[cfg(not(feature = "compression"))]
false
}
#[cfg_attr(feature = "compression", allow(clippy::unused_self, unused_variables))]
pub fn extend_text(&mut self, bytes: &[u8]) -> bool {
#[cfg(not(feature = "compression"))]
{
self.inner.extend_from_slice(bytes);
true
}
#[cfg(feature = "compression")]
false
}
#[cfg_attr(
not(feature = "compression"),
allow(clippy::unnecessary_wraps, clippy::unused_self)
)]
pub fn message_mut(&mut self) -> Result<Option<&mut [u8]>, ReceivingEventError> {
#[cfg(feature = "compression")]
{
use super::r#impl::ReceivingEventErrorType;
self.inner.msg().map_err(|source| ReceivingEventError {
kind: ReceivingEventErrorType::Decompressing,
source: Some(Box::new(source)),
})
}
#[cfg(not(feature = "compression"))]
Ok(None)
}
pub fn reset(&mut self) {
#[cfg(feature = "compression")]
self.inner.reset();
#[cfg(not(feature = "compression"))]
self.clear();
}
}
#[cfg_attr(not(feature = "compression"), allow(unused_variables))]
pub fn add_url_feature(buf: &mut String) {
#[cfg(feature = "compression")]
buf.push_str("&compress=zlib-stream");
}
#[cfg(test)]
mod tests {
#[test]
fn test_add_url_features() {
let mut buf = String::new();
super::add_url_feature(&mut buf);
#[cfg(feature = "compression")]
{
assert_eq!("&compress=zlib-stream", buf);
}
#[cfg(not(feature = "compression"))]
assert!(buf.is_empty());
}
}