Skip to main content

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

1//! List the Microsoft Graph mail folders (`GET /me/mailFolders`).
2//!
3//! <https://learn.microsoft.com/en-us/graph/api/user-list-mailfolders>
4
5use alloc::format;
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::Serialize;
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::{
16        query::to_query_pairs,
17        rest::users::mail_folders::MsgraphMailFoldersListResponse,
18        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
19    },
20};
21
22/// OData query parameters for listing mail folders.
23#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
24pub struct MsgraphMailFoldersListParams<'a> {
25    /// Maximum number of folders per page (`$top`).
26    #[serde(rename = "$top")]
27    pub top: Option<u32>,
28    /// Number of folders to skip (`$skip`).
29    #[serde(rename = "$skip")]
30    pub skip: Option<u32>,
31    /// Comma-separated properties to return (`$select`).
32    #[serde(rename = "$select")]
33    pub select: Option<&'a str>,
34    /// Whether hidden folders are included in the listing.
35    #[serde(rename = "includeHiddenFolders")]
36    pub include_hidden_folders: Option<bool>,
37}
38
39/// Lists the Microsoft Graph mail folders of a mailbox.
40pub struct MsgraphMailFoldersList {
41    send: MsgraphSend<MsgraphMailFoldersListResponse>,
42}
43
44impl MsgraphMailFoldersList {
45    /// Lists the top-level mail folders, filtered by the OData
46    /// `params`.
47    pub fn new(
48        auth: &HttpAuthBearer,
49        user_id: &str,
50        params: &MsgraphMailFoldersListParams,
51    ) -> Result<Self, MsgraphSendError> {
52        debug!("prepare microsoft graph mail folders listing");
53        trace!("params: {params:?}");
54
55        let user = user_path(user_id);
56        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/mailFolders"))?;
57        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
58
59        let send = MsgraphSend::get(auth, url);
60
61        Ok(Self { send })
62    }
63}
64
65impl MsgraphCoroutine for MsgraphMailFoldersList {
66    type Yield = MsgraphYield;
67    type Return = Result<MsgraphSendOutput<MsgraphMailFoldersListResponse>, MsgraphSendError>;
68
69    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
70        let out = msgraph_try!(&mut self.send, arg);
71        debug!("mail folders listed");
72        trace!("out: {out:?}");
73        MsgraphCoroutineState::Complete(Ok(out))
74    }
75}