From 01ee8c872b15e2aecb4c0ef5c2dd3aa6cc3c4d3c Mon Sep 17 00:00:00 2001 From: KokaKiwi Date: Mon, 27 Jun 2022 14:50:32 +0200 Subject: [PATCH] proto: Add untagged status in response body values Fixes #9 --- src/proto/body.rs | 32 -------------- src/proto/mod.rs | 1 - src/proto/res/body.rs | 74 ++++++++++++++++++++++++++++++++ src/proto/{res.rs => res/mod.rs} | 4 +- src/server/pipeline.rs | 32 ++++++++++++++ 5 files changed, 109 insertions(+), 34 deletions(-) delete mode 100644 src/proto/body.rs create mode 100644 src/proto/res/body.rs rename src/proto/{res.rs => res/mod.rs} (98%) diff --git a/src/proto/body.rs b/src/proto/body.rs deleted file mode 100644 index a4e9b58..0000000 --- a/src/proto/body.rs +++ /dev/null @@ -1,32 +0,0 @@ -use imap_codec::types::response::Data as ImapData; - -#[derive(Debug)] -pub enum Body { - Once(Vec), -} - -impl Body { - pub(crate) fn into_data(self) -> Vec { - match self { - Body::Once(data) => data, - } - } -} - -impl FromIterator for Body { - fn from_iter>(iter: T) -> Self { - Body::Once(Vec::from_iter(iter)) - } -} - -impl From> for Body { - fn from(data: Vec) -> Self { - Body::from_iter(data) - } -} - -impl From for Body { - fn from(data: ImapData) -> Self { - Body::from_iter([data]) - } -} diff --git a/src/proto/mod.rs b/src/proto/mod.rs index c59663b..852952c 100644 --- a/src/proto/mod.rs +++ b/src/proto/mod.rs @@ -1,6 +1,5 @@ pub use self::req::Request; pub use self::res::Response; -pub mod body; pub mod req; pub mod res; diff --git a/src/proto/res/body.rs b/src/proto/res/body.rs new file mode 100644 index 0000000..6a3c589 --- /dev/null +++ b/src/proto/res/body.rs @@ -0,0 +1,74 @@ +use std::io; + +use imap_codec::codec::Encode; +use imap_codec::types::response::{Data as ImapData, Status as ImapStatus}; + +#[derive(Debug)] +pub enum Body { + Once(Vec), +} + +impl Body { + pub(crate) fn into_data(self) -> Vec { + match self { + Body::Once(data) => data, + } + } +} + +impl FromIterator for Body { + fn from_iter>(iter: T) -> Self { + Body::Once(Vec::from_iter(iter)) + } +} + +impl FromIterator for Body { + fn from_iter>(iter: T) -> Self { + Body::from_iter(iter.into_iter().map(Data::Data)) + } +} + +impl From> for Body { + fn from(body: Vec) -> Self { + Body::from_iter(body) + } +} + +impl From> for Body { + fn from(data: Vec) -> Self { + Body::from_iter(data) + } +} + +impl From for Body { + fn from(data: ImapData) -> Self { + Body::from_iter([data]) + } +} + +#[derive(Debug, Clone)] +pub enum Data { + Data(ImapData), + Status(ImapStatus), +} + +impl Encode for Data { + fn encode(&self, writer: &mut impl io::Write) -> std::io::Result<()> { + match self { + Data::Data(ref data) => data.encode(writer), + Data::Status(ref status) => status.encode(writer), + } + } +} + +impl From for Data { + fn from(data: ImapData) -> Self { + Data::Data(data) + } +} + +impl From for Data { + fn from(status: ImapStatus) -> Self { + Data::Status(status) + } +} diff --git a/src/proto/res.rs b/src/proto/res/mod.rs similarity index 98% rename from src/proto/res.rs rename to src/proto/res/mod.rs index c704c48..96b92ee 100644 --- a/src/proto/res.rs +++ b/src/proto/res/mod.rs @@ -3,9 +3,11 @@ use imap_codec::types::{ response::{Code as ImapCode, Status as ImapStatus}, }; -use super::body::Body; +use self::body::Body; use crate::errors::{Error, Result}; +pub mod body; + #[derive(Debug)] pub struct Response { pub(crate) status: Status, diff --git a/src/server/pipeline.rs b/src/server/pipeline.rs index 0bb4579..3942bce 100644 --- a/src/server/pipeline.rs +++ b/src/server/pipeline.rs @@ -146,3 +146,35 @@ where Poll::Ready(Ok(())) } } + +impl Sink for Connection +where + Self: Sink<(Option, Response)>, +{ + type Error = as Sink<(Option, Response)>>::Error; + + fn poll_ready( + self: Pin<&mut Self>, + cx: &mut task::Context<'_>, + ) -> Poll> { + , Response)>>::poll_ready(self, cx) + } + + fn start_send(self: Pin<&mut Self>, item: Response) -> Result<(), Self::Error> { + , Response)>>::start_send(self, (None, item)) + } + + fn poll_flush( + self: Pin<&mut Self>, + cx: &mut task::Context<'_>, + ) -> Poll> { + , Response)>>::poll_flush(self, cx) + } + + fn poll_close( + self: Pin<&mut Self>, + cx: &mut task::Context<'_>, + ) -> Poll> { + , Response)>>::poll_close(self, cx) + } +}