Skip to main content

io_msgraph/v1/rest/users/mail_folders/
copy.rs

1//! Copy a Microsoft Graph mail folder to another folder
2//! (`POST /me/mailFolders/{id}/copy`); returns the new copy.
3//!
4//! <https://learn.microsoft.com/en-us/graph/api/mailfolder-copy>
5
6use alloc::format;
7
8use io_http::rfc6750::bearer::HttpAuthBearer;
9use log::{debug, trace};
10use serde::Serialize;
11use url::Url;
12
13use crate::{
14    coroutine::*,
15    msgraph_try,
16    v1::{
17        rest::users::mail_folders::MsgraphMailFolder,
18        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
19    },
20};
21
22/// Body of the mail folder copy request (the destination folder id).
23#[derive(Debug, Serialize)]
24#[serde(rename_all = "camelCase")]
25struct MsgraphMailFolderCopyRequest<'a> {
26    destination_id: &'a str,
27}
28
29/// Copies a Microsoft Graph mail folder into another folder.
30pub struct MsgraphMailFolderCopy {
31    send: MsgraphSend<MsgraphMailFolder>,
32}
33
34impl MsgraphMailFolderCopy {
35    /// Copies `id` into `destination` (a folder id or a well-known name
36    /// such as `archive`).
37    pub fn new(
38        auth: &HttpAuthBearer,
39        user_id: &str,
40        id: &str,
41        destination: &str,
42    ) -> Result<Self, MsgraphSendError> {
43        debug!("prepare microsoft graph mail folder copy");
44        trace!("id: {id:?}");
45        trace!("destination: {destination:?}");
46
47        let user = user_path(user_id);
48        let url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/mailFolders/{id}/copy"))?;
49        let body = MsgraphMailFolderCopyRequest {
50            destination_id: destination,
51        };
52        let send = MsgraphSend::post_json(auth, url, &body)?;
53
54        Ok(Self { send })
55    }
56}
57
58impl MsgraphCoroutine for MsgraphMailFolderCopy {
59    type Yield = MsgraphYield;
60    type Return = Result<MsgraphSendOutput<MsgraphMailFolder>, MsgraphSendError>;
61
62    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
63        let out = msgraph_try!(&mut self.send, arg);
64        debug!("mail folder copied");
65        trace!("out: {out:?}");
66        MsgraphCoroutineState::Complete(Ok(out))
67    }
68}