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::{
19            MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, path_segment,
20            user_path,
21        },
22    },
23};
24
25/// Body of the mail folder copy request (the destination folder id).
26#[derive(Debug, Serialize)]
27#[serde(rename_all = "camelCase")]
28struct MsgraphMailFolderCopyRequest<'a> {
29    destination_id: &'a str,
30}
31
32/// Copies a Microsoft Graph mail folder into another folder.
33pub struct MsgraphMailFolderCopy {
34    send: MsgraphSend<MsgraphMailFolder>,
35}
36
37impl MsgraphMailFolderCopy {
38    /// Copies `id` into `destination` (a folder id or a well-known name
39    /// such as `archive`).
40    pub fn new(
41        auth: &HttpAuthBearer,
42        user_id: &str,
43        id: &str,
44        destination: &str,
45    ) -> Result<Self, MsgraphSendError> {
46        debug!("prepare microsoft graph mail folder copy");
47        trace!("id: {id:?}");
48        trace!("destination: {destination:?}");
49
50        let user = user_path(user_id)?;
51        let url = Url::parse(MSGRAPH_API_BASE)?
52            .join(&format!("{user}/mailFolders/{}/copy", path_segment(id)?))?;
53        let body = MsgraphMailFolderCopyRequest {
54            destination_id: destination,
55        };
56        let send = MsgraphSend::post_json(auth, url, &body)?;
57
58        Ok(Self { send })
59    }
60}
61
62impl MsgraphCoroutine for MsgraphMailFolderCopy {
63    type Yield = MsgraphYield;
64    type Return = Result<MsgraphSendOutput<MsgraphMailFolder>, MsgraphSendError>;
65
66    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
67        let out = msgraph_try!(&mut self.send, arg);
68        debug!("mail folder copied");
69        trace!("out: {out:?}");
70        MsgraphCoroutineState::Complete(Ok(out))
71    }
72}