proto: Add untagged status in response body values

Fixes #9
This commit is contained in:
Jill 2022-06-27 14:50:32 +02:00
parent 2af6a7d781
commit 01ee8c872b
Signed by: KokaKiwi
GPG key ID: 09A5A2688F13FAC1
5 changed files with 109 additions and 34 deletions

View file

@ -1,32 +0,0 @@
use imap_codec::types::response::Data as ImapData;
#[derive(Debug)]
pub enum Body {
Once(Vec<ImapData>),
}
impl Body {
pub(crate) fn into_data(self) -> Vec<ImapData> {
match self {
Body::Once(data) => data,
}
}
}
impl FromIterator<ImapData> for Body {
fn from_iter<T: IntoIterator<Item = ImapData>>(iter: T) -> Self {
Body::Once(Vec::from_iter(iter))
}
}
impl From<Vec<ImapData>> for Body {
fn from(data: Vec<ImapData>) -> Self {
Body::from_iter(data)
}
}
impl From<ImapData> for Body {
fn from(data: ImapData) -> Self {
Body::from_iter([data])
}
}

View file

@ -1,6 +1,5 @@
pub use self::req::Request; pub use self::req::Request;
pub use self::res::Response; pub use self::res::Response;
pub mod body;
pub mod req; pub mod req;
pub mod res; pub mod res;

74
src/proto/res/body.rs Normal file
View file

@ -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<Data>),
}
impl Body {
pub(crate) fn into_data(self) -> Vec<Data> {
match self {
Body::Once(data) => data,
}
}
}
impl FromIterator<Data> for Body {
fn from_iter<T: IntoIterator<Item = Data>>(iter: T) -> Self {
Body::Once(Vec::from_iter(iter))
}
}
impl FromIterator<ImapData> for Body {
fn from_iter<T: IntoIterator<Item = ImapData>>(iter: T) -> Self {
Body::from_iter(iter.into_iter().map(Data::Data))
}
}
impl From<Vec<Data>> for Body {
fn from(body: Vec<Data>) -> Self {
Body::from_iter(body)
}
}
impl From<Vec<ImapData>> for Body {
fn from(data: Vec<ImapData>) -> Self {
Body::from_iter(data)
}
}
impl From<ImapData> 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<ImapData> for Data {
fn from(data: ImapData) -> Self {
Data::Data(data)
}
}
impl From<ImapStatus> for Data {
fn from(status: ImapStatus) -> Self {
Data::Status(status)
}
}

View file

@ -3,9 +3,11 @@ use imap_codec::types::{
response::{Code as ImapCode, Status as ImapStatus}, response::{Code as ImapCode, Status as ImapStatus},
}; };
use super::body::Body; use self::body::Body;
use crate::errors::{Error, Result}; use crate::errors::{Error, Result};
pub mod body;
#[derive(Debug)] #[derive(Debug)]
pub struct Response { pub struct Response {
pub(crate) status: Status, pub(crate) status: Status,

View file

@ -146,3 +146,35 @@ where
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
} }
impl<C> Sink<Response> for Connection<C>
where
Self: Sink<(Option<Tag>, Response)>,
{
type Error = <Connection<C> as Sink<(Option<Tag>, Response)>>::Error;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
<Self as Sink<(Option<Tag>, Response)>>::poll_ready(self, cx)
}
fn start_send(self: Pin<&mut Self>, item: Response) -> Result<(), Self::Error> {
<Self as Sink<(Option<Tag>, Response)>>::start_send(self, (None, item))
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
<Self as Sink<(Option<Tag>, Response)>>::poll_flush(self, cx)
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> Poll<Result<(), Self::Error>> {
<Self as Sink<(Option<Tag>, Response)>>::poll_close(self, cx)
}
}