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