2021-02-11 22:39:54 +00:00
|
|
|
//! Stream encoders.
|
|
|
|
|
2021-02-12 00:15:25 +00:00
|
|
|
use std::{
|
2021-05-05 18:36:02 +01:00
|
|
|
error::Error as StdError,
|
2021-02-12 00:15:25 +00:00
|
|
|
future::Future,
|
|
|
|
io::{self, Write as _},
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2019-03-26 15:14:32 -07:00
|
|
|
|
2021-01-10 00:04:19 +08:00
|
|
|
use actix_rt::task::{spawn_blocking, JoinHandle};
|
2019-04-04 13:17:55 -07:00
|
|
|
use bytes::Bytes;
|
2021-05-05 18:36:02 +01:00
|
|
|
use derive_more::Display;
|
2019-12-13 11:24:57 +06:00
|
|
|
use futures_core::ready;
|
2021-12-04 19:40:47 +00:00
|
|
|
use pin_project_lite::pin_project;
|
2021-06-19 21:21:13 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "compress-brotli")]
|
|
|
|
use brotli2::write::BrotliEncoder;
|
|
|
|
|
|
|
|
#[cfg(feature = "compress-gzip")]
|
|
|
|
use flate2::write::{GzEncoder, ZlibEncoder};
|
|
|
|
|
|
|
|
#[cfg(feature = "compress-zstd")]
|
2021-06-03 22:32:52 +02:00
|
|
|
use zstd::stream::write::Encoder as ZstdEncoder;
|
2019-03-26 15:14:32 -07:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
use super::Writer;
|
2021-02-12 00:15:25 +00:00
|
|
|
use crate::{
|
2021-12-04 19:40:47 +00:00
|
|
|
body::{BodySize, MessageBody},
|
|
|
|
error::BlockingError,
|
2021-02-12 00:15:25 +00:00
|
|
|
http::{
|
|
|
|
header::{ContentEncoding, CONTENT_ENCODING},
|
|
|
|
HeaderValue, StatusCode,
|
|
|
|
},
|
2021-08-31 04:18:54 +01:00
|
|
|
ResponseHead,
|
2021-02-12 00:15:25 +00:00
|
|
|
};
|
2019-03-26 15:14:32 -07:00
|
|
|
|
2021-02-12 00:15:25 +00:00
|
|
|
const MAX_CHUNK_SIZE_ENCODE_IN_PLACE: usize = 1024;
|
2019-04-04 13:17:55 -07:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
pin_project! {
|
|
|
|
pub struct Encoder<B> {
|
|
|
|
#[pin]
|
|
|
|
body: EncoderBody<B>,
|
|
|
|
encoder: Option<ContentEncoder>,
|
|
|
|
fut: Option<JoinHandle<Result<ContentEncoder, io::Error>>>,
|
|
|
|
eof: bool,
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<B: MessageBody> Encoder<B> {
|
2021-12-04 19:40:47 +00:00
|
|
|
fn none() -> Self {
|
|
|
|
Encoder {
|
|
|
|
body: EncoderBody::None,
|
|
|
|
encoder: None,
|
|
|
|
fut: None,
|
|
|
|
eof: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
pub fn response(
|
|
|
|
encoding: ContentEncoding,
|
|
|
|
head: &mut ResponseHead,
|
2021-12-04 19:40:47 +00:00
|
|
|
body: B,
|
|
|
|
) -> Self {
|
2019-04-06 15:02:02 -07:00
|
|
|
let can_encode = !(head.headers().contains_key(&CONTENT_ENCODING)
|
2019-04-04 13:17:55 -07:00
|
|
|
|| head.status == StatusCode::SWITCHING_PROTOCOLS
|
2019-06-16 21:54:17 +06:00
|
|
|
|| head.status == StatusCode::NO_CONTENT
|
2019-04-04 13:17:55 -07:00
|
|
|
|| encoding == ContentEncoding::Identity
|
|
|
|
|| encoding == ContentEncoding::Auto);
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
match body.size() {
|
|
|
|
// no need to compress an empty body
|
|
|
|
BodySize::None => return Self::none(),
|
|
|
|
|
|
|
|
// we cannot assume that Sized is not a stream
|
|
|
|
BodySize::Sized(_) | BodySize::Stream => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO potentially some optimisation for single-chunk responses here by trying to read the
|
|
|
|
// payload eagerly, stopping after 2 polls if the first is a chunk and the second is None
|
2019-04-04 13:17:55 -07:00
|
|
|
|
|
|
|
if can_encode {
|
2021-12-04 19:40:47 +00:00
|
|
|
// Modify response body only if encoder is set
|
2019-07-22 11:35:00 +06:00
|
|
|
if let Some(enc) = ContentEncoder::encoder(encoding) {
|
|
|
|
update_head(encoding, head);
|
|
|
|
head.no_chunking(false);
|
2021-11-16 21:41:35 +00:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
return Encoder {
|
|
|
|
body: EncoderBody::Stream { body },
|
2019-07-22 11:35:00 +06:00
|
|
|
encoder: Some(enc),
|
2021-12-04 19:40:47 +00:00
|
|
|
fut: None,
|
|
|
|
eof: false,
|
|
|
|
};
|
2019-07-22 11:35:00 +06:00
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
2021-02-15 11:24:46 +00:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
Encoder {
|
|
|
|
body: EncoderBody::Stream { body },
|
2019-07-22 11:35:00 +06:00
|
|
|
encoder: None,
|
2021-12-04 19:40:47 +00:00
|
|
|
fut: None,
|
|
|
|
eof: false,
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
pin_project! {
|
|
|
|
#[project = EncoderBodyProj]
|
|
|
|
enum EncoderBody<B> {
|
|
|
|
None,
|
|
|
|
Stream { #[pin] body: B },
|
|
|
|
}
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
2021-05-05 18:36:02 +01:00
|
|
|
impl<B> MessageBody for EncoderBody<B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
{
|
2021-12-04 19:40:47 +00:00
|
|
|
type Error = EncoderError;
|
2021-05-05 18:36:02 +01:00
|
|
|
|
2020-01-29 11:15:13 +03:00
|
|
|
fn size(&self) -> BodySize {
|
|
|
|
match self {
|
2021-12-04 19:40:47 +00:00
|
|
|
EncoderBody::None => BodySize::None,
|
|
|
|
EncoderBody::Stream { body } => body.size(),
|
2020-01-29 11:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 11:10:55 +09:00
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
2021-05-05 18:36:02 +01:00
|
|
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
2020-01-29 11:15:13 +03:00
|
|
|
match self.project() {
|
2021-12-04 19:40:47 +00:00
|
|
|
EncoderBodyProj::None => Poll::Ready(None),
|
|
|
|
|
|
|
|
EncoderBodyProj::Stream { body } => body
|
|
|
|
.poll_next(cx)
|
|
|
|
.map_err(|err| EncoderError::Body(err.into())),
|
2020-01-29 11:15:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 18:36:02 +01:00
|
|
|
impl<B> MessageBody for Encoder<B>
|
|
|
|
where
|
|
|
|
B: MessageBody,
|
|
|
|
{
|
2021-12-04 19:40:47 +00:00
|
|
|
type Error = EncoderError;
|
2021-05-05 18:36:02 +01:00
|
|
|
|
2019-04-10 12:24:17 -07:00
|
|
|
fn size(&self) -> BodySize {
|
2019-03-26 15:14:32 -07:00
|
|
|
if self.encoder.is_none() {
|
2020-01-29 11:15:13 +03:00
|
|
|
self.body.size()
|
2019-03-26 15:14:32 -07:00
|
|
|
} else {
|
2019-03-27 09:24:55 -07:00
|
|
|
BodySize::Stream
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 11:10:55 +09:00
|
|
|
|
|
|
|
fn poll_next(
|
|
|
|
self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
2021-05-05 18:36:02 +01:00
|
|
|
) -> Poll<Option<Result<Bytes, Self::Error>>> {
|
2020-01-29 11:15:13 +03:00
|
|
|
let mut this = self.project();
|
2019-03-26 15:14:32 -07:00
|
|
|
loop {
|
2020-01-29 11:15:13 +03:00
|
|
|
if *this.eof {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-04-04 15:03:40 -07:00
|
|
|
}
|
|
|
|
|
2020-01-29 11:15:13 +03:00
|
|
|
if let Some(ref mut fut) = this.fut {
|
2021-05-05 18:36:02 +01:00
|
|
|
let mut encoder = ready!(Pin::new(fut).poll(cx))
|
|
|
|
.map_err(|_| EncoderError::Blocking(BlockingError))?
|
|
|
|
.map_err(EncoderError::Io)?;
|
2021-02-12 00:15:25 +00:00
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
let chunk = encoder.take();
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.encoder = Some(encoder);
|
|
|
|
this.fut.take();
|
2021-02-12 00:15:25 +00:00
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
if !chunk.is_empty() {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-04-04 13:17:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 00:15:25 +00:00
|
|
|
let result = ready!(this.body.as_mut().poll_next(cx));
|
2020-01-29 11:15:13 +03:00
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
match result {
|
2021-02-12 00:15:25 +00:00
|
|
|
Some(Err(err)) => return Poll::Ready(Some(Err(err))),
|
|
|
|
|
|
|
|
Some(Ok(chunk)) => {
|
2020-01-29 11:15:13 +03:00
|
|
|
if let Some(mut encoder) = this.encoder.take() {
|
2021-02-12 00:15:25 +00:00
|
|
|
if chunk.len() < MAX_CHUNK_SIZE_ENCODE_IN_PLACE {
|
2021-05-05 18:36:02 +01:00
|
|
|
encoder.write(&chunk).map_err(EncoderError::Io)?;
|
2019-04-04 13:17:55 -07:00
|
|
|
let chunk = encoder.take();
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.encoder = Some(encoder);
|
2021-02-12 00:15:25 +00:00
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
if !chunk.is_empty() {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-04-04 13:17:55 -07:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-10 00:04:19 +08:00
|
|
|
*this.fut = Some(spawn_blocking(move || {
|
2019-04-04 13:17:55 -07:00
|
|
|
encoder.write(&chunk)?;
|
|
|
|
Ok(encoder)
|
|
|
|
}));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
2019-04-04 15:03:40 -07:00
|
|
|
} else {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
2021-02-12 00:15:25 +00:00
|
|
|
|
|
|
|
None => {
|
2020-01-29 11:15:13 +03:00
|
|
|
if let Some(encoder) = this.encoder.take() {
|
2021-05-05 18:36:02 +01:00
|
|
|
let chunk = encoder.finish().map_err(EncoderError::Io)?;
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
if chunk.is_empty() {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-03-26 15:14:32 -07:00
|
|
|
} else {
|
2020-01-29 11:15:13 +03:00
|
|
|
*this.eof = true;
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(Some(Ok(chunk)));
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-15 15:54:11 +06:00
|
|
|
return Poll::Ready(None);
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_head(encoding: ContentEncoding, head: &mut ResponseHead) {
|
|
|
|
head.headers_mut().insert(
|
|
|
|
CONTENT_ENCODING,
|
2019-12-05 23:35:43 +06:00
|
|
|
HeaderValue::from_static(encoding.as_str()),
|
2019-03-26 15:14:32 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ContentEncoder {
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
Deflate(ZlibEncoder<Writer>),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
Gzip(GzEncoder<Writer>),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-12-20 13:50:07 +06:00
|
|
|
Br(BrotliEncoder<Writer>),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
|
|
|
// Wwe need explicit 'static lifetime here because ZstdEncoder needs a lifetime argument and we
|
|
|
|
// use `spawn_blocking` in `Encoder::poll_next` that requires `FnOnce() -> R + Send + 'static`.
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-zstd")]
|
2021-06-03 22:32:52 +02:00
|
|
|
Zstd(ZstdEncoder<'static, Writer>),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ContentEncoder {
|
|
|
|
fn encoder(encoding: ContentEncoding) -> Option<Self> {
|
|
|
|
match encoding {
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoding::Deflate => Some(ContentEncoder::Deflate(ZlibEncoder::new(
|
|
|
|
Writer::new(),
|
|
|
|
flate2::Compression::fast(),
|
|
|
|
))),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoding::Gzip => Some(ContentEncoder::Gzip(GzEncoder::new(
|
|
|
|
Writer::new(),
|
|
|
|
flate2::Compression::fast(),
|
|
|
|
))),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-12-20 13:50:07 +06:00
|
|
|
ContentEncoding::Br => {
|
|
|
|
Some(ContentEncoder::Br(BrotliEncoder::new(Writer::new(), 3)))
|
|
|
|
}
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-zstd")]
|
2021-06-03 22:32:52 +02:00
|
|
|
ContentEncoding::Zstd => {
|
|
|
|
let encoder = ZstdEncoder::new(Writer::new(), 3).ok()?;
|
|
|
|
Some(ContentEncoder::Zstd(encoder))
|
|
|
|
}
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2019-03-26 15:14:32 -07:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn take(&mut self) -> Bytes {
|
|
|
|
match *self {
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-12-20 13:50:07 +06:00
|
|
|
ContentEncoder::Br(ref mut encoder) => encoder.get_mut().take(),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(ref mut encoder) => encoder.get_mut().take(),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(ref mut encoder) => encoder.get_mut().take(),
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-zstd")]
|
2021-06-03 22:32:52 +02:00
|
|
|
ContentEncoder::Zstd(ref mut encoder) => encoder.get_mut().take(),
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn finish(self) -> Result<Bytes, io::Error> {
|
|
|
|
match self {
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-12-20 13:50:07 +06:00
|
|
|
ContentEncoder::Br(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-zstd")]
|
2021-06-03 22:32:52 +02:00
|
|
|
ContentEncoder::Zstd(encoder) => match encoder.finish() {
|
|
|
|
Ok(writer) => Ok(writer.buf.freeze()),
|
|
|
|
Err(err) => Err(err),
|
|
|
|
},
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-04 13:17:55 -07:00
|
|
|
fn write(&mut self, data: &[u8]) -> Result<(), io::Error> {
|
2019-03-26 15:14:32 -07:00
|
|
|
match *self {
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-brotli")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Br(ref mut encoder) => match encoder.write_all(data) {
|
2019-04-04 13:17:55 -07:00
|
|
|
Ok(_) => Ok(()),
|
2019-03-26 15:14:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding br encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Gzip(ref mut encoder) => match encoder.write_all(data) {
|
2019-04-04 13:17:55 -07:00
|
|
|
Ok(_) => Ok(()),
|
2019-03-26 15:14:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding gzip encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-gzip")]
|
2019-03-26 15:14:32 -07:00
|
|
|
ContentEncoder::Deflate(ref mut encoder) => match encoder.write_all(data) {
|
2019-04-04 13:17:55 -07:00
|
|
|
Ok(_) => Ok(()),
|
2019-03-26 15:14:32 -07:00
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding deflate encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
2021-12-04 19:40:47 +00:00
|
|
|
|
2021-06-19 21:21:13 +02:00
|
|
|
#[cfg(feature = "compress-zstd")]
|
2021-06-03 22:32:52 +02:00
|
|
|
ContentEncoder::Zstd(ref mut encoder) => match encoder.write_all(data) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(err) => {
|
|
|
|
trace!("Error decoding ztsd encoding: {}", err);
|
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
},
|
2019-03-26 15:14:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-05 18:36:02 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Display)]
|
|
|
|
#[non_exhaustive]
|
2021-12-04 19:40:47 +00:00
|
|
|
pub enum EncoderError {
|
2021-05-05 18:36:02 +01:00
|
|
|
#[display(fmt = "body")]
|
2021-12-04 19:40:47 +00:00
|
|
|
Body(Box<dyn StdError>),
|
2021-05-05 18:36:02 +01:00
|
|
|
|
|
|
|
#[display(fmt = "blocking")]
|
|
|
|
Blocking(BlockingError),
|
|
|
|
|
|
|
|
#[display(fmt = "io")]
|
|
|
|
Io(io::Error),
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
impl StdError for EncoderError {
|
2021-05-05 18:36:02 +01:00
|
|
|
fn source(&self) -> Option<&(dyn StdError + 'static)> {
|
2021-06-17 17:57:58 +01:00
|
|
|
match self {
|
2021-12-04 19:40:47 +00:00
|
|
|
EncoderError::Body(err) => Some(&**err),
|
2021-06-17 17:57:58 +01:00
|
|
|
EncoderError::Blocking(err) => Some(err),
|
|
|
|
EncoderError::Io(err) => Some(err),
|
|
|
|
}
|
2021-05-05 18:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
impl From<EncoderError> for crate::Error {
|
|
|
|
fn from(err: EncoderError) -> Self {
|
2021-06-17 17:57:58 +01:00
|
|
|
crate::Error::new_encoder().with_cause(err)
|
2021-05-05 18:36:02 +01:00
|
|
|
}
|
|
|
|
}
|