use crate::requests::Result;
#[cfg(feature = "fuzz")]
use arbitrary::Arbitrary;
use std::io::{Read, Write};
#[cfg_attr(feature = "fuzz", derive(Arbitrary))]
#[derive(Debug, PartialEq)]
pub struct RequestBody {
bytes: Vec<u8>,
}
impl RequestBody {
#[cfg(feature = "testing")]
pub(super) fn new() -> RequestBody {
RequestBody { bytes: Vec::new() }
}
pub(super) fn read_from_stream(mut stream: &mut impl Read, len: usize) -> Result<RequestBody> {
let bytes = get_from_stream!(stream; len);
Ok(RequestBody { bytes })
}
pub(super) fn write_to_stream(&self, stream: &mut impl Write) -> Result<()> {
stream.write_all(&self.bytes)?;
Ok(())
}
pub(crate) fn from_bytes(bytes: Vec<u8>) -> RequestBody {
RequestBody { bytes }
}
pub fn bytes(&self) -> &[u8] {
&self.bytes
}
pub fn len(&self) -> usize {
self.bytes.len()
}
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
#[cfg(feature = "testing")]
pub fn _from_bytes(bytes: Vec<u8>) -> RequestBody {
RequestBody { bytes }
}
}