From f1ae06bf744fb775028ae06df028b38a23c4bae7 Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Fri, 3 Oct 2025 14:34:19 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(list):=20add=20label=20filteri?= =?UTF-8?q?ng=20capability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - allow users to filter messages by label - implement add_label method to add label IDs to the filter - integrate label filtering into the messages_list call --- src/list.rs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/list.rs b/src/list.rs index 8b4baf0..c00644c 100644 --- a/src/list.rs +++ b/src/list.rs @@ -18,6 +18,7 @@ pub const DEFAULT_MAX_RESULTS: &str = "10"; pub struct List { hub: Gmail>, max_results: u32, + label_ids: Vec, } impl List { @@ -53,6 +54,7 @@ impl List { Ok(List { hub: Gmail::new(client, auth), max_results: DEFAULT_MAX_RESULTS.parse::().unwrap(), + label_ids: Vec::new(), }) } @@ -66,6 +68,11 @@ impl List { self.max_results } + /// Add label to the labels collection + pub fn add_label(&mut self, label_id: &str) { + self.label_ids.push(label_id.to_string()) + } + /// Run the Gmail api as configured pub async fn run(&self, pages: u32) -> Result<(), Error> { let log_estimate = |est: &Option| { @@ -116,16 +123,24 @@ impl List { &self, next_page_token: Option, ) -> Result { - let call = self + let mut call = self .hub .users() .messages_list("me") .max_results(self.max_results); - let call = if let Some(page_token) = next_page_token { - call.page_token(&page_token) - } else { - call - }; + // Add any labels specified + if !self.label_ids.is_empty() { + log::debug!("Setting labels for list: {:#?}", self.label_ids); + for id in self.label_ids.as_slice() { + call = call.add_label_ids(id); + } + } + // Add a page token if it exists + if let Some(page_token) = next_page_token { + log::debug!("Setting token for next page."); + call = call.page_token(&page_token); + } + let (_response, list) = call.doit().await.map_err(Box::new)?; Ok(list)