|
| 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::shared_stream::SharedStream; |
| 13 | +use crate::types::IdGenerator; |
| 14 | +use crate::Connection; |
| 15 | + |
| 16 | +#[cfg(feature = "runtime-async-std")] |
| 17 | +use async_std::io::{BufReader, Read, Write}; |
| 18 | +#[cfg(feature = "runtime-tokio")] |
| 19 | +use tokio::io::{AsyncRead as Read, AsyncWrite as Write, BufReader, ReadBuf}; |
| 20 | + |
| 21 | +#[cfg(feature = "runtime-tokio")] |
| 22 | +use async_compression::tokio::bufread::DeflateDecoder; |
| 23 | +#[cfg(feature = "runtime-tokio")] |
| 24 | +use async_compression::tokio::write::DeflateEncoder; |
| 25 | + |
| 26 | +#[cfg(feature = "runtime-async-std")] |
| 27 | +use async_compression::futures::bufread::DeflateDecoder; |
| 28 | +#[cfg(feature = "runtime-async-std")] |
| 29 | +use async_compression::futures::write::DeflateEncoder; |
| 30 | + |
| 31 | +/// IMAP stream |
| 32 | +#[derive(Debug)] |
| 33 | +#[pin_project] |
| 34 | +pub struct DeflateStream<T: Read + Write + Unpin + fmt::Debug> { |
| 35 | + /// Shared stream reference to allow direct access |
| 36 | + /// to the underlying stream. |
| 37 | + stream: SharedStream<T>, |
| 38 | + |
| 39 | + #[pin] |
| 40 | + decoder: DeflateDecoder<BufReader<SharedStream<T>>>, |
| 41 | + |
| 42 | + #[pin] |
| 43 | + encoder: DeflateEncoder<SharedStream<T>>, |
| 44 | +} |
| 45 | + |
| 46 | +impl<T: Read + Write + Unpin + fmt::Debug> DeflateStream<T> { |
| 47 | + pub(crate) fn new(stream: T) -> Self { |
| 48 | + let stream = SharedStream::new(stream); |
| 49 | + let decoder = DeflateDecoder::new(BufReader::new(stream.clone())); |
| 50 | + let encoder = DeflateEncoder::new(stream.clone()); |
| 51 | + Self { |
| 52 | + stream, |
| 53 | + decoder, |
| 54 | + encoder, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// Runs provided function while holding the lock on the underlying stream. |
| 59 | + /// |
| 60 | + /// This allows to access the underlying stream while ensuring |
| 61 | + /// that no data is read from the stream or written into the stream at the same time. |
| 62 | + pub fn with_lock<R>(&self, f: impl FnOnce(Pin<&mut T>) -> R) -> R { |
| 63 | + self.stream.with_lock(f) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[cfg(feature = "runtime-tokio")] |
| 68 | +impl<T: Read + Write + Unpin + fmt::Debug> Read for DeflateStream<T> { |
| 69 | + fn poll_read( |
| 70 | + self: Pin<&mut Self>, |
| 71 | + cx: &mut Context<'_>, |
| 72 | + buf: &mut ReadBuf<'_>, |
| 73 | + ) -> Poll<std::io::Result<()>> { |
| 74 | + self.project().decoder.poll_read(cx, buf) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[cfg(feature = "runtime-async-std")] |
| 79 | +impl<T: Read + Write + Unpin + fmt::Debug> Read for DeflateStream<T> { |
| 80 | + fn poll_read( |
| 81 | + self: Pin<&mut Self>, |
| 82 | + cx: &mut Context<'_>, |
| 83 | + buf: &mut [u8], |
| 84 | + ) -> Poll<async_std::io::Result<usize>> { |
| 85 | + self.project().decoder.poll_read(cx, buf) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(feature = "runtime-tokio")] |
| 90 | +impl<T: Read + Write + Unpin + fmt::Debug> Write for DeflateStream<T> { |
| 91 | + fn poll_write( |
| 92 | + self: Pin<&mut Self>, |
| 93 | + cx: &mut std::task::Context<'_>, |
| 94 | + buf: &[u8], |
| 95 | + ) -> Poll<std::io::Result<usize>> { |
| 96 | + self.project().encoder.poll_write(cx, buf) |
| 97 | + } |
| 98 | + |
| 99 | + fn poll_flush( |
| 100 | + self: Pin<&mut Self>, |
| 101 | + cx: &mut std::task::Context<'_>, |
| 102 | + ) -> Poll<std::io::Result<()>> { |
| 103 | + self.project().encoder.poll_flush(cx) |
| 104 | + } |
| 105 | + |
| 106 | + fn poll_shutdown( |
| 107 | + self: Pin<&mut Self>, |
| 108 | + cx: &mut std::task::Context<'_>, |
| 109 | + ) -> Poll<std::io::Result<()>> { |
| 110 | + self.project().encoder.poll_shutdown(cx) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#[cfg(feature = "runtime-async-std")] |
| 115 | +impl<T: Read + Write + Unpin + fmt::Debug> Write for DeflateStream<T> { |
| 116 | + fn poll_write( |
| 117 | + self: Pin<&mut Self>, |
| 118 | + cx: &mut std::task::Context<'_>, |
| 119 | + buf: &[u8], |
| 120 | + ) -> Poll<async_std::io::Result<usize>> { |
| 121 | + self.project().encoder.poll_write(cx, buf) |
| 122 | + } |
| 123 | + |
| 124 | + fn poll_flush( |
| 125 | + self: Pin<&mut Self>, |
| 126 | + cx: &mut std::task::Context<'_>, |
| 127 | + ) -> Poll<async_std::io::Result<()>> { |
| 128 | + self.project().encoder.poll_flush(cx) |
| 129 | + } |
| 130 | + |
| 131 | + fn poll_close( |
| 132 | + self: Pin<&mut Self>, |
| 133 | + cx: &mut std::task::Context<'_>, |
| 134 | + ) -> Poll<async_std::io::Result<()>> { |
| 135 | + self.project().encoder.poll_close(cx) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +impl<T: Read + Write + Unpin + fmt::Debug + Send> Session<T> { |
| 140 | + /// Runs `COMPRESS DEFLATE` command. |
| 141 | + pub async fn compress<F, S>(self, f: F) -> Result<Session<S>> |
| 142 | + where |
| 143 | + S: Read + Write + Unpin + fmt::Debug, |
| 144 | + F: FnOnce(DeflateStream<T>) -> S, |
| 145 | + { |
| 146 | + let Self { |
| 147 | + mut conn, |
| 148 | + unsolicited_responses_tx, |
| 149 | + unsolicited_responses, |
| 150 | + } = self; |
| 151 | + conn.run_command_and_check_ok("COMPRESS DEFLATE", Some(unsolicited_responses_tx.clone())) |
| 152 | + .await?; |
| 153 | + |
| 154 | + let stream = conn.into_inner(); |
| 155 | + let deflate_stream = DeflateStream::new(stream); |
| 156 | + let stream = ImapStream::new(f(deflate_stream)); |
| 157 | + let conn = Connection { |
| 158 | + stream, |
| 159 | + request_ids: IdGenerator::new(), |
| 160 | + }; |
| 161 | + let session = Session { |
| 162 | + conn, |
| 163 | + unsolicited_responses_tx, |
| 164 | + unsolicited_responses, |
| 165 | + }; |
| 166 | + Ok(session) |
| 167 | + } |
| 168 | +} |
0 commit comments