Add BodySize enum

This commit is contained in:
Deluvi
2020-06-26 15:35:37 +02:00
committed by Martin Algesten
parent e224a6d126
commit 7de192d3f1
3 changed files with 36 additions and 17 deletions

View File

@@ -43,11 +43,21 @@ impl Default for Payload {
}
}
/// The size of the body.
///
/// *Internal API*
#[derive(Debug)]
pub(crate) enum BodySize {
Empty,
Unknown,
Known(u64),
}
/// Payloads are turned into this type where we can hold both a size and the reader.
///
/// *Internal API*
pub(crate) struct SizedReader {
pub size: Option<usize>,
pub size: BodySize,
pub reader: Box<dyn Read + 'static>,
}
@@ -58,7 +68,7 @@ impl fmt::Debug for SizedReader {
}
impl SizedReader {
fn new(size: Option<usize>, reader: Box<dyn Read + 'static>) -> Self {
fn new(size: BodySize, reader: Box<dyn Read + 'static>) -> Self {
SizedReader { size, reader }
}
}
@@ -66,7 +76,7 @@ impl SizedReader {
impl Payload {
pub fn into_read(self) -> SizedReader {
match self {
Payload::Empty => SizedReader::new(Some(0), Box::new(empty())),
Payload::Empty => SizedReader::new(BodySize::Empty, Box::new(empty())),
Payload::Text(text, _charset) => {
#[cfg(feature = "charset")]
let bytes = {
@@ -79,20 +89,20 @@ impl Payload {
let bytes = text.into_bytes();
let len = bytes.len();
let cursor = Cursor::new(bytes);
SizedReader::new(Some(len), Box::new(cursor))
SizedReader::new(BodySize::Known(len as u64), Box::new(cursor))
}
#[cfg(feature = "json")]
Payload::JSON(v) => {
let bytes = serde_json::to_vec(&v).expect("Bad JSON in payload");
let len = bytes.len();
let cursor = Cursor::new(bytes);
SizedReader::new(Some(len), Box::new(cursor))
SizedReader::new(BodySize::Known(len as u64), Box::new(cursor))
}
Payload::Reader(read) => SizedReader::new(None, read),
Payload::Reader(read) => SizedReader::new(BodySize::Unknown, read),
Payload::Bytes(bytes) => {
let len = bytes.len();
let cursor = Cursor::new(bytes);
SizedReader::new(Some(len), Box::new(cursor))
SizedReader::new(BodySize::Known(len as u64), Box::new(cursor))
}
}
}