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, string::String, vec::Vec};
6
7use io_http::rfc6750::bearer::HttpAuthBearer;
8use log::{debug, trace};
9use serde::{Deserialize, Serialize};
10use url::Url;
11
12use crate::{
13    coroutine::*,
14    msgraph_try,
15    v1::{
16        query::to_query_pairs,
17        rest::users::mail_folders::MsgraphMailFolder,
18        send::{MSGRAPH_API_BASE, MsgraphSend, MsgraphSendError, MsgraphSendOutput, user_path},
19    },
20};
21
22/// One page of mail folders (`value` plus the OData paging link).
23#[derive(Debug, Clone, Default, Deserialize, Serialize, Eq, PartialEq)]
24pub struct MsgraphMailFoldersListResponse {
25    /// The mail folders of the page.
26    #[serde(default)]
27    pub value: Vec<MsgraphMailFolder>,
28    /// The URL of the next page, when one exists.
29    #[serde(default, rename = "@odata.nextLink")]
30    pub next_link: Option<String>,
31}
32
33/// OData query parameters for listing mail folders.
34#[derive(Debug, Clone, Default, Serialize, Eq, PartialEq)]
35pub struct MsgraphMailFoldersListParams<'a> {
36    /// Maximum number of folders per page (`$top`).
37    #[serde(rename = "$top")]
38    pub top: Option<u32>,
39    /// Number of folders to skip (`$skip`).
40    #[serde(rename = "$skip")]
41    pub skip: Option<u32>,
42    /// Comma-separated properties to return (`$select`).
43    #[serde(rename = "$select")]
44    pub select: Option<&'a str>,
45    /// Whether hidden folders are included in the listing.
46    #[serde(rename = "includeHiddenFolders")]
47    pub include_hidden_folders: Option<bool>,
48}
49
50/// Lists the Microsoft Graph mail folders of a mailbox.
51pub struct MsgraphMailFoldersList {
52    send: MsgraphSend<MsgraphMailFoldersListResponse>,
53}
54
55impl MsgraphMailFoldersList {
56    /// Lists the top-level mail folders, filtered by the OData
57    /// `params`.
58    pub fn new(
59        auth: &HttpAuthBearer,
60        user_id: &str,
61        params: &MsgraphMailFoldersListParams,
62    ) -> Result<Self, MsgraphSendError> {
63        debug!("prepare microsoft graph mail folders listing");
64        trace!("params: {params:?}");
65
66        let user = user_path(user_id);
67        let mut url = Url::parse(MSGRAPH_API_BASE)?.join(&format!("{user}/mailFolders"))?;
68        url.query_pairs_mut().extend_pairs(to_query_pairs(params));
69
70        let send = MsgraphSend::get(auth, url);
71
72        Ok(Self { send })
73    }
74}
75
76impl MsgraphCoroutine for MsgraphMailFoldersList {
77    type Yield = MsgraphYield;
78    type Return = Result<MsgraphSendOutput<MsgraphMailFoldersListResponse>, MsgraphSendError>;
79
80    fn resume(&mut self, arg: Option<&[u8]>) -> MsgraphCoroutineState<Self::Yield, Self::Return> {
81        let out = msgraph_try!(&mut self.send, arg);
82        debug!("mail folders listed");
83        trace!("out: {out:?}");
84        MsgraphCoroutineState::Complete(Ok(out))
85    }
86}