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)