Skip to main content

io_imap/rfc3501/
rename.rs

1//! IMAP RENAME coroutine renaming a mailbox.
2//!
3//! # Example
4//!
5//! ```rust,no_run
6//! use std::{
7//!     io::{Read, Write},
8//!     net::TcpStream,
9//! };
10//!
11//! use io_imap::{
12//!     codec::fragmentizer::Fragmentizer,
13//!     coroutine::{ImapCoroutine, ImapCoroutineState, ImapYield},
14//!     rfc3501::rename::ImapMailboxRename,
15//! };
16//!
17//! // Ready stream needed (TCP-connected, TLS-negociated, IMAP-authenticated)
18//! let mut stream = TcpStream::connect("localhost:143").unwrap();
19//!
20//! let mut fragmentizer = Fragmentizer::new(50 * 1024 * 1024);
21//! let mut buf = [0u8; 4096];
22//!
23//! let from = "Old".try_into().unwrap();
24//! let to = "New".try_into().unwrap();
25//! let mut coroutine = ImapMailboxRename::new(from, to);
26//! let mut arg = None;
27//!
28//! loop {
29//!     match coroutine.resume(&mut fragmentizer, arg.take()) {
30//!         ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => {
31//!             stream.write_all(&bytes).unwrap();
32//!         }
33//!         ImapCoroutineState::Yielded(ImapYield::WantsRead) => {
34//!             let n = stream.read(&mut buf).unwrap();
35//!             arg = Some(&buf[..n]);
36//!         }
37//!         ImapCoroutineState::Complete(Ok(())) => break,
38//!         ImapCoroutineState::Complete(Err(err)) => panic!("{err}"),
39//!     }
40//! }
41//! ```
42
43use core::fmt;
44
45use alloc::string::{String, ToString};
46
47use imap_codec::{
48    CommandCodec,
49    fragmentizer::Fragmentizer,
50    imap_types::{
51        command::{Command, CommandBody},
52        core::TagGenerator,
53        mailbox::Mailbox,
54        response::{StatusKind, Tagged},
55    },
56};
57use log::trace;
58use thiserror::Error;
59
60use crate::{coroutine::*, imap_try, rfc3501::mailbox::encode_inplace, send::*};
61
62/// Failure causes during the IMAP RENAME flow.
63#[derive(Clone, Debug, Error)]
64pub enum ImapMailboxRenameError {
65    #[error("IMAP RENAME failed: NO {0}")]
66    No(String),
67    #[error("IMAP RENAME failed: BAD {0}")]
68    Bad(String),
69    #[error("IMAP RENAME failed: BYE {0}")]
70    Bye(String),
71
72    #[error("IMAP RENAME failed: server did not return a tagged response")]
73    MissingTagged,
74
75    #[error("IMAP RENAME failed: {0}")]
76    Send(#[from] SendImapCommandError),
77}
78
79/// I/O-free IMAP RENAME coroutine.
80pub struct ImapMailboxRename {
81    state: State,
82}
83
84impl ImapMailboxRename {
85    pub fn new(mut from: Mailbox<'static>, mut to: Mailbox<'static>) -> Self {
86        encode_inplace(&mut from);
87        encode_inplace(&mut to);
88
89        let command = Command {
90            tag: TagGenerator::new().generate(),
91            body: CommandBody::Rename { from, to },
92        };
93
94        trace!("send IMAP command {command:?}");
95
96        let state = State::Send(SendImapCommand::new(CommandCodec::new(), command));
97
98        Self { state }
99    }
100}
101
102impl ImapCoroutine for ImapMailboxRename {
103    type Yield = ImapYield;
104    type Return = Result<(), ImapMailboxRenameError>;
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!("rename: {}", 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 = ImapMailboxRenameError::Bye(bye.text.to_string());
120                        return ImapCoroutineState::Complete(Err(err));
121                    }
122
123                    let Some(Tagged { body, .. }) = out.tagged else {
124                        let err = ImapMailboxRenameError::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 = ImapMailboxRenameError::No(body.text.to_string());
132                            ImapCoroutineState::Complete(Err(err))
133                        }
134                        StatusKind::Bad => {
135                            let err = ImapMailboxRenameError::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 rename"),
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 rename = ImapMailboxRename::new(
168            "Old".try_into().expect("valid mailbox"),
169            "New".try_into().expect("valid mailbox"),
170        );
171        let mut frag = Fragmentizer::new(50 * 1024 * 1024);
172
173        let bytes = expect_wants_write(&mut rename, &mut frag, None);
174        let line = str::from_utf8(&bytes).expect("utf8 command");
175        let tag = first_word(line).to_owned();
176        assert!(line.contains("RENAME Old New"));
177
178        expect_wants_read(&mut rename, &mut frag);
179
180        let reply = format!("{tag} OK RENAME completed\r\n");
181        expect_complete_ok(&mut rename, &mut frag, reply.as_bytes());
182    }
183
184    #[test]
185    fn tagged_no_returns_no_error() {
186        let mut rename = ImapMailboxRename::new(
187            "Old".try_into().expect("valid mailbox"),
188            "New".try_into().expect("valid mailbox"),
189        );
190        let mut frag = Fragmentizer::new(50 * 1024 * 1024);
191
192        let bytes = expect_wants_write(&mut rename, &mut frag, None);
193        let tag = first_word(str::from_utf8(&bytes).expect("utf8 command")).to_owned();
194
195        expect_wants_read(&mut rename, &mut frag);
196
197        let reply = format!("{tag} NO target mailbox already exists\r\n");
198        let err = expect_complete_err(&mut rename, &mut frag, reply.as_bytes());
199        let ImapMailboxRenameError::No(text) = err else {
200            panic!("expected ImapMailboxRenameError::No, got {err:?}");
201        };
202        assert_eq!(text, "target mailbox already exists");
203    }
204
205    #[test]
206    fn bye_returns_bye_error() {
207        let mut rename = ImapMailboxRename::new(
208            "Old".try_into().expect("valid mailbox"),
209            "New".try_into().expect("valid mailbox"),
210        );
211        let mut frag = Fragmentizer::new(50 * 1024 * 1024);
212
213        let _ = expect_wants_write(&mut rename, &mut frag, None);
214        expect_wants_read(&mut rename, &mut frag);
215
216        let err = expect_complete_err(&mut rename, &mut frag, b"* BYE going down\r\n");
217        let ImapMailboxRenameError::Bye(text) = err else {
218            panic!("expected ImapMailboxRenameError::Bye, got {err:?}");
219        };
220        assert_eq!(text, "going down");
221    }
222
223    // --- utils
224
225    fn expect_wants_write(
226        cor: &mut ImapMailboxRename,
227        frag: &mut Fragmentizer,
228        arg: Option<&[u8]>,
229    ) -> Vec<u8> {
230        match cor.resume(frag, arg) {
231            ImapCoroutineState::Yielded(ImapYield::WantsWrite(bytes)) => bytes,
232            state => panic!("expected WantsWrite, got {state:?}"),
233        }
234    }
235
236    fn expect_wants_read(cor: &mut ImapMailboxRename, frag: &mut Fragmentizer) {
237        match cor.resume(frag, None) {
238            ImapCoroutineState::Yielded(ImapYield::WantsRead) => {}
239            state => panic!("expected WantsRead, got {state:?}"),
240        }
241    }
242
243    fn expect_complete_ok(cor: &mut ImapMailboxRename, frag: &mut Fragmentizer, reply: &[u8]) {
244        match cor.resume(frag, Some(reply)) {
245            ImapCoroutineState::Complete(Ok(())) => {}
246            state => panic!("expected Complete(Ok), got {state:?}"),
247        }
248    }
249
250    fn expect_complete_err(
251        cor: &mut ImapMailboxRename,
252        frag: &mut Fragmentizer,
253        reply: &[u8],
254    ) -> ImapMailboxRenameError {
255        match cor.resume(frag, Some(reply)) {
256            ImapCoroutineState::Complete(Err(err)) => err,
257            state => panic!("expected Complete(Err), got {state:?}"),
258        }
259    }
260
261    fn first_word(line: &str) -> &str {
262        line.split_whitespace()
263            .next()
264            .expect("first whitespace-separated token")
265    }
266}