1use core::fmt;
42
43use alloc::string::{String, ToString};
44
45use imap_codec::{
46 CommandCodec,
47 fragmentizer::Fragmentizer,
48 imap_types::{
49 command::{Command, CommandBody},
50 core::TagGenerator,
51 response::{StatusKind, Tagged},
52 },
53};
54use log::trace;
55use thiserror::Error;
56
57use crate::{coroutine::*, imap_try, send::*};
58
59#[derive(Clone, Debug, Error)]
61pub enum ImapMailboxCloseError {
62 #[error("IMAP CLOSE failed: NO {0}")]
63 No(String),
64 #[error("IMAP CLOSE failed: BAD {0}")]
65 Bad(String),
66 #[error("IMAP CLOSE failed: BYE {0}")]
67 Bye(String),
68
69 #[error("IMAP CLOSE failed: server did not return a tagged response")]
70 MissingTagged,
71
72 #[error("IMAP CLOSE failed: {0}")]
73 Send(#[from] SendImapCommandError),
74}
75
76pub struct ImapMailboxClose {
78 state: State,
79}
80
81impl ImapMailboxClose {
82 pub fn new() -> Self {
83 let command = Command {
84 tag: TagGenerator::new().generate(),
85 body: CommandBody::Close,
86 };
87
88 trace!("send IMAP command {command:?}");
89
90 let state = State::Send(SendImapCommand::new(CommandCodec::new(), command));
91
92 Self { state }
93 }
94}
95
96impl Default for ImapMailboxClose {
97 fn default() -> Self {
98 Self::new()
99 }
100}
101
102impl ImapCoroutine for ImapMailboxClose {
103 type Yield = ImapYield;
104 type Return = Result<(), ImapMailboxCloseError>;
105
106 fn resume(
107 &mut self,
108 fragmentizer: &mut Fragmentizer,
109 arg: Option<&[u8]>,
110 ) -> ImapCoroutineState<Self::Yield, Self::Return> {
111 loop {
112 trace!("close: {}", self.state);
113
114 match &mut self.state {
115 State::Send(send) => {
116 let out = imap_try!(send, fragmentizer, arg);
117
118 if let Some(bye) = out.bye {
119 let err = ImapMailboxCloseError::Bye(bye.text.to_string());
120 return ImapCoroutineState::Complete(Err(err));
121 }
122
123 let Some(Tagged { body, .. }) = out.tagged else {
124 let err = ImapMailboxCloseError::MissingTagged;
125 return ImapCoroutineState::Complete(Err(err));
126 };
127
128 return match body.kind {
129 StatusKind::Ok => ImapCoroutineState::Complete(Ok(())),
130 StatusKind::No => {
131 let err = ImapMailboxCloseError::No(body.text.to_string());
132 ImapCoroutineState::Complete(Err(err))
133 }
134 StatusKind::Bad => {
135 let err = ImapMailboxCloseError::Bad(body.text.to_string());
136 ImapCoroutineState::Complete(Err(err))
137 }
138 };
139 }
140 }
141 }
142 }
143}
144
145enum State {
146 Send(SendImapCommand<CommandCodec>),
147}
148
149impl fmt::Display for State {
150 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151 match self {
152 Self::Send(_) => f.write_str("send close"),
153 }
154 }
155}
156
157#[cfg(test)]
158mod tests {
159 use core::str;
160
161 use alloc::{borrow::ToOwned, vec::Vec};
162
163 use super::*;
164
165 #[test]
166 fn success_returns_ok() {
167 let mut close = ImapMailboxClose::new();
168 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
169
170 let bytes = expect_wants_write(&mut close, &mut frag, None);
171 let line = str::from_utf8(&bytes).expect("utf8 command");
172 let tag = first_word(line).to_owned();
173 assert!(line.trim_end().ends_with("CLOSE"));
174
175 expect_wants_read(&mut close, &mut frag);
176
177 let reply = format!("{tag} OK CLOSE completed\r\n");
178 expect_complete_ok(&mut close, &mut frag, reply.as_bytes());
179 }
180
181 #[test]
182 fn tagged_no_returns_no_error() {
183 let mut close = ImapMailboxClose::new();
184 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
185
186 let bytes = expect_wants_write(&mut close, &mut frag, None);
187 let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
188
189 expect_wants_read(&mut close, &mut frag);
190
191 let reply = format!("{tag} NO no mailbox selected\r\n");
192 let err = expect_complete_err(&mut close, &mut frag, reply.as_bytes());
193 let ImapMailboxCloseError::No(text) = err else {
194 panic!("expected ImapMailboxCloseError::No, got {err:?}");
195 };
196 assert_eq!(text, "no mailbox selected");
197 }
198
199 #[test]
200 fn tagged_bad_returns_bad_error() {
201 let mut close = ImapMailboxClose::new();
202 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
203
204 let bytes = expect_wants_write(&mut close, &mut frag, None);
205 let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
206
207 expect_wants_read(&mut close, &mut frag);
208
209 let reply = format!("{tag} BAD CLOSE syntax error\r\n");
210 let err = expect_complete_err(&mut close, &mut frag, reply.as_bytes());
211 let ImapMailboxCloseError::Bad(text) = err else {
212 panic!("expected ImapMailboxCloseError::Bad, got {err:?}");
213 };
214 assert_eq!(text, "CLOSE syntax error");
215 }
216
217 #[test]
218 fn bye_returns_bye_error() {
219 let mut close = ImapMailboxClose::new();
220 let mut frag = Fragmentizer::new(50 * 1024 * 1024);
221
222 let _ = expect_wants_write(&mut close, &mut frag, None);
223 expect_wants_read(&mut close, &mut frag);
224
225 let err = expect_complete_err(&mut close, &mut frag, b"* BYE going down\r\n");
226 let ImapMailboxCloseError::Bye(text) = err else {
227 panic!("expected ImapMailboxCloseError::Bye, got {err:?}");
228 };
229 assert_eq!(text, "going down");
230 }
231
232 fn expect_wants_write(
235 cor: &mut ImapMailboxClose,
236 frag: &mut Fragmentizer,
237 arg: Option<&[u8]>,
238 ) -> Vec<u8> {
239 match cor.resume(frag, arg) {
240 ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => bytes,
241 state => panic!("expected WantsWrite, got {state:?}"),
242 }
243 }
244
245 fn expect_wants_read(cor: &mut ImapMailboxClose, frag: &mut Fragmentizer) {
246 match cor.resume(frag, None) {
247 ImapCoroutineState::Yielded(ImapYield::WantsRead) => {}
248 state => panic!("expected WantsRead, got {state:?}"),
249 }
250 }
251
252 fn expect_complete_ok(cor: &mut ImapMailboxClose, frag: &mut Fragmentizer, reply: &[u8]) {
253 match cor.resume(frag, Some(reply)) {
254 ImapCoroutineState::Complete(Ok(())) => {}
255 state => panic!("expected Complete(Ok), got {state:?}"),
256 }
257 }
258
259 fn expect_complete_err(
260 cor: &mut ImapMailboxClose,
261 frag: &mut Fragmentizer,
262 reply: &[u8],
263 ) -> ImapMailboxCloseError {
264 match cor.resume(frag, Some(reply)) {
265 ImapCoroutineState::Complete(Err(err)) => err,
266 state => panic!("expected Complete(Err), got {state:?}"),
267 }
268 }
269
270 fn first_word(line: &str) -> &str {
271 line.split_whitespace()
272 .next()
273 .expect("first whitespace-separated token")
274 }
275}