Skip to main content

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

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