aerogramme/src/imap/response.rs

124 lines
2.7 KiB
Rust
Raw Normal View History

2024-01-17 15:56:05 +00:00
use std::sync::Arc;
use anyhow::Result;
2024-01-17 15:56:05 +00:00
use tokio::sync::Notify;
use imap_codec::imap_types::command::Command;
use imap_codec::imap_types::core::Tag;
2024-01-02 14:35:23 +00:00
use imap_codec::imap_types::response::{Code, Data, Status};
2024-01-17 07:22:15 +00:00
#[derive(Debug)]
2024-01-02 14:35:23 +00:00
pub enum Body<'a> {
Data(Data<'a>),
Status(Status<'a>),
}
2024-01-02 14:35:23 +00:00
pub struct ResponseBuilder<'a> {
tag: Option<Tag<'a>>,
code: Option<Code<'a>>,
text: String,
body: Vec<Body<'a>>,
}
2024-01-02 14:35:23 +00:00
impl<'a> ResponseBuilder<'a> {
pub fn to_req(mut self, cmd: &Command<'a>) -> Self {
self.tag = Some(cmd.tag.clone());
self
}
2024-01-02 14:35:23 +00:00
pub fn tag(mut self, tag: Tag<'a>) -> Self {
self.tag = Some(tag);
self
}
pub fn message(mut self, txt: impl Into<String>) -> Self {
self.text = txt.into();
self
}
2024-01-02 14:35:23 +00:00
pub fn code(mut self, code: Code<'a>) -> Self {
self.code = Some(code);
self
}
2024-01-02 14:35:23 +00:00
pub fn data(mut self, data: Data<'a>) -> Self {
self.body.push(Body::Data(data));
self
}
pub fn many_data(mut self, data: Vec<Data<'a>>) -> Self {
for d in data.into_iter() {
self = self.data(d);
}
self
}
2024-01-02 19:23:33 +00:00
#[allow(dead_code)]
2024-01-02 14:35:23 +00:00
pub fn info(mut self, status: Status<'a>) -> Self {
self.body.push(Body::Status(status));
self
}
2024-01-02 19:23:33 +00:00
#[allow(dead_code)]
2024-01-02 14:35:23 +00:00
pub fn many_info(mut self, status: Vec<Status<'a>>) -> Self {
for d in status.into_iter() {
self = self.info(d);
}
self
}
2024-01-02 14:35:23 +00:00
pub fn set_body(mut self, body: Vec<Body<'a>>) -> Self {
self.body = body;
self
}
2024-01-02 14:35:23 +00:00
pub fn ok(self) -> Result<Response<'a>> {
Ok(Response {
2024-01-02 14:35:23 +00:00
completion: Status::ok(self.tag, self.code, self.text)?,
body: self.body,
})
}
2024-01-02 14:35:23 +00:00
pub fn no(self) -> Result<Response<'a>> {
Ok(Response {
completion: Status::no(self.tag, self.code, self.text)?,
body: self.body,
})
}
2024-01-02 14:35:23 +00:00
pub fn bad(self) -> Result<Response<'a>> {
Ok(Response {
completion: Status::bad(self.tag, self.code, self.text)?,
body: self.body,
})
}
2024-01-02 14:35:23 +00:00
}
2024-01-17 07:22:15 +00:00
#[derive(Debug)]
2024-01-02 14:35:23 +00:00
pub struct Response<'a> {
2024-01-02 19:23:33 +00:00
pub body: Vec<Body<'a>>,
pub completion: Status<'a>,
2024-01-02 14:35:23 +00:00
}
impl<'a> Response<'a> {
pub fn build() -> ResponseBuilder<'a> {
ResponseBuilder {
2024-01-02 14:35:23 +00:00
tag: None,
code: None,
text: "".to_string(),
body: vec![],
}
}
2024-01-02 14:35:23 +00:00
pub fn bye() -> Result<Response<'a>> {
Ok(Response {
2024-01-02 14:35:23 +00:00
completion: Status::bye(None, "bye")?,
body: vec![],
})
}
}
2024-01-17 07:22:15 +00:00
#[derive(Debug)]
pub enum ResponseOrIdle {
Response(Response<'static>),
2024-01-17 15:56:05 +00:00
StartIdle(Arc<Notify>),
2024-01-17 09:14:48 +00:00
IdleEvent(Vec<Body<'static>>),
2024-01-17 07:22:15 +00:00
}