micro_web/encoding/
mod.rs

1//! Module for handling HTTP response body encoding.
2//!
3//! This module provides functionality for encoding HTTP response bodies using different compression
4//! algorithms like gzip, deflate, zstd, and brotli. It works in conjunction with the encoder
5//! module to provide a complete encoding solution.
6//!
7//! The main components are:
8//! - `Writer`: An internal buffer implementation for collecting encoded data
9//! - `encoder`: A sub-module containing the encoding logic and request handler wrapper
10//!
11//! The implementation is inspired by the actix-http crate's encoding functionality.
12
13use bytes::{Bytes, BytesMut};
14use std::io;
15
16pub mod encoder;
17
18// inspired by from actix-http
19pub(crate) struct Writer {
20    buf: BytesMut,
21}
22
23impl Writer {
24    fn new() -> Self {
25        Self { buf: BytesMut::with_capacity(4096) }
26    }
27
28    fn take(&mut self) -> Bytes {
29        self.buf.split().freeze()
30    }
31}
32
33impl io::Write for Writer {
34    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
35        self.buf.extend_from_slice(buf);
36        Ok(buf.len())
37    }
38
39    fn flush(&mut self) -> io::Result<()> {
40        Ok(())
41    }
42}