|
| 1 | +//! IMAP COMPRESS extension specified in [RFC4978](https://www.rfc-editor.org/rfc/rfc4978.html). |
| 2 | +
|
| 3 | +use std::fmt; |
| 4 | +use std::pin::Pin; |
| 5 | +use std::task::{Context, Poll}; |
| 6 | + |
| 7 | +use pin_project::pin_project; |
| 8 | + |
| 9 | +use crate::client::Session; |
| 10 | +use crate::error::Result; |
| 11 | +use crate::imap_stream::ImapStream; |
| 12 | +use crate::types::IdGenerator; |
| 13 | +use crate::Connection; |
| 14 | + |
| 15 | +#[cfg(feature = "runtime-async-std")] |
| 16 | +use async_std::io::{IoSlice, IoSliceMut, Read, Write}; |
| 17 | +#[cfg(feature = "runtime-async-std")] |
| 18 | +use futures::io::BufReader; |
| 19 | +#[cfg(feature = "runtime-tokio")] |
| 20 | +use tokio::io::{AsyncRead as Read, AsyncWrite as Write, BufReader, ReadBuf}; |
| 21 | + |
| 22 | +#[cfg(feature = "runtime-tokio")] |
| 23 | +use async_compression::tokio::bufread::DeflateDecoder; |
| 24 | +#[cfg(feature = "runtime-tokio")] |
| 25 | +use async_compression::tokio::write::DeflateEncoder; |
| 26 | + |
| 27 | +#[cfg(feature = "runtime-async-std")] |
| 28 | +use async_compression::futures::bufread::DeflateDecoder; |
| 29 | +#[cfg(feature = "runtime-async-std")] |
| 30 | +use async_compression::futures::write::DeflateEncoder; |
| 31 | + |
| 32 | +/// Network stream compressed with DEFLATE. |
| 33 | +#[derive(Debug)] |
| 34 | +#[pin_project] |
| 35 | +pub struct DeflateStream<T: Read + Write + Unpin + fmt::Debug> { |
| 36 | + #[pin] |
| 37 | + inner: DeflateDecoder<BufReader<DeflateEncoder<T>>>, |
| 38 | +} |
| 39 | + |
| 40 | +impl<T: Read + Write + Unpin + fmt::Debug> DeflateStream<T> { |
| 41 | + pub(crate) fn new(stream: T) -> Self { |
| 42 | + let stream = DeflateEncoder::new(stream); |
| 43 | + let stream = BufReader::new(stream); |
| 44 | + let stream = DeflateDecoder::new(stream); |
| 45 | + Self { inner: stream } |
| 46 | + } |
| 47 | + |
| 48 | + /// Gets a reference to the underlying stream. |
| 49 | + pub fn get_ref(&self) -> &T { |
| 50 | + self.inner.get_ref().get_ref().get_ref() |
| 51 | + } |
| 52 | + |
| 53 | + /// Gets a mutable reference to the underlying stream. |
| 54 | + pub fn get_mut(&mut self) -> &mut T { |
| 55 | + self.inner.get_mut().get_mut().get_mut() |
| 56 | + } |
| 57 | + |
| 58 | + /// Consumes `DeflateStream` and returns underlying stream. |
| 59 | + pub fn into_inner(self) -> T { |
| 60 | + self.inner.into_inner().into_inner().into_inner() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(feature = "runtime-tokio")] |
| 65 | +impl<T: Read + Write + Unpin + fmt::Debug> Read for DeflateStream<T> { |
| 66 | + fn poll_read( |
| 67 | + self: Pin<&mut Self>, |
| 68 | + cx: &mut Context<'_>, |
| 69 | + buf: &mut ReadBuf<'_>, |
| 70 | + ) -> Poll<std::io::Result<()>> { |
| 71 | + self.project().inner.poll_read(cx, buf) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(feature = "runtime-async-std")] |
| 76 | +impl<T: Read + Write + Unpin + fmt::Debug> Read for DeflateStream<T> { |
| 77 | + fn poll_read( |
| 78 | + self: Pin<&mut Self>, |
| 79 | + cx: &mut Context<'_>, |
| 80 | + buf: &mut [u8], |
| 81 | + ) -> Poll<async_std::io::Result<usize>> { |
| 82 | + self.project().inner.poll_read(cx, buf) |
| 83 | + } |
| 84 | + |
| 85 | + fn poll_read_vectored( |
| 86 | + self: Pin<&mut Self>, |
| 87 | + cx: &mut Context<'_>, |
| 88 | + bufs: &mut [IoSliceMut<'_>], |
| 89 | + ) -> Poll<async_std::io::Result<usize>> { |
| 90 | + self.project().inner.poll_read_vectored(cx, bufs) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +#[cfg(feature = "runtime-tokio")] |
| 95 | +impl<T: Read + Write + Unpin + fmt::Debug> Write for DeflateStream<T> { |
| 96 | + fn poll_write( |
| 97 | + self: Pin<&mut Self>, |
| 98 | + cx: &mut std::task::Context<'_>, |
| 99 | + buf: &[u8], |
| 100 | + ) -> Poll<std::io::Result<usize>> { |
| 101 | + self.project().inner.get_pin_mut().poll_write(cx, buf) |
| 102 | + } |
| 103 | + |
| 104 | + fn poll_flush( |
| 105 | + self: Pin<&mut Self>, |
| 106 | + cx: &mut std::task::Context<'_>, |
| 107 | + ) -> Poll<std::io::Result<()>> { |
| 108 | + self.project().inner.poll_flush(cx) |
| 109 | + } |
| 110 | + |
| 111 | + fn poll_shutdown( |
| 112 | + self: Pin<&mut Self>, |
| 113 | + cx: &mut std::task::Context<'_>, |
| 114 | + ) -> Poll<std::io::Result<()>> { |
| 115 | + self.project().inner.poll_shutdown(cx) |
| 116 | + } |
| 117 | + |
| 118 | + fn poll_write_vectored( |
| 119 | + self: Pin<&mut Self>, |
| 120 | + cx: &mut Context<'_>, |
| 121 | + bufs: &[std::io::IoSlice<'_>], |
| 122 | + ) -> Poll<std::io::Result<usize>> { |
| 123 | + self.project().inner.poll_write_vectored(cx, bufs) |
| 124 | + } |
| 125 | + |
| 126 | + fn is_write_vectored(&self) -> bool { |
| 127 | + self.inner.is_write_vectored() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +#[cfg(feature = "runtime-async-std")] |
| 132 | +impl<T: Read + Write + Unpin + fmt::Debug> Write for DeflateStream<T> { |
| 133 | + fn poll_write( |
| 134 | + self: Pin<&mut Self>, |
| 135 | + cx: &mut std::task::Context<'_>, |
| 136 | + buf: &[u8], |
| 137 | + ) -> Poll<async_std::io::Result<usize>> { |
| 138 | + self.project().inner.as_mut().poll_write(cx, buf) |
| 139 | + } |
| 140 | + |
| 141 | + fn poll_flush( |
| 142 | + self: Pin<&mut Self>, |
| 143 | + cx: &mut std::task::Context<'_>, |
| 144 | + ) -> Poll<async_std::io::Result<()>> { |
| 145 | + self.project().inner.poll_flush(cx) |
| 146 | + } |
| 147 | + |
| 148 | + fn poll_close( |
| 149 | + self: Pin<&mut Self>, |
| 150 | + cx: &mut std::task::Context<'_>, |
| 151 | + ) -> Poll<async_std::io::Result<()>> { |
| 152 | + self.project().inner.poll_close(cx) |
| 153 | + } |
| 154 | + |
| 155 | + fn poll_write_vectored( |
| 156 | + self: Pin<&mut Self>, |
| 157 | + cx: &mut Context<'_>, |
| 158 | + bufs: &[IoSlice<'_>], |
| 159 | + ) -> Poll<async_std::io::Result<usize>> { |
| 160 | + self.project().inner.poll_write_vectored(cx, bufs) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +impl<T: Read + Write + Unpin + fmt::Debug + Send> Session<T> { |
| 165 | + /// Runs `COMPRESS DEFLATE` command. |
| 166 | + pub async fn compress<F, S>(self, f: F) -> Result<Session<S>> |
| 167 | + where |
| 168 | + S: Read + Write + Unpin + fmt::Debug, |
| 169 | + F: FnOnce(DeflateStream<T>) -> S, |
| 170 | + { |
| 171 | + let Self { |
| 172 | + mut conn, |
| 173 | + unsolicited_responses_tx, |
| 174 | + unsolicited_responses, |
| 175 | + } = self; |
| 176 | + conn.run_command_and_check_ok("COMPRESS DEFLATE", Some(unsolicited_responses_tx.clone())) |
| 177 | + .await?; |
| 178 | + |
| 179 | + let stream = conn.into_inner(); |
| 180 | + let deflate_stream = DeflateStream::new(stream); |
| 181 | + let stream = ImapStream::new(f(deflate_stream)); |
| 182 | + let conn = Connection { |
| 183 | + stream, |
| 184 | + request_ids: IdGenerator::new(), |
| 185 | + }; |
| 186 | + let session = Session { |
| 187 | + conn, |
| 188 | + unsolicited_responses_tx, |
| 189 | + unsolicited_responses, |
| 190 | + }; |
| 191 | + Ok(session) |
| 192 | + } |
| 193 | +} |
0 commit comments