♻️ refactor(message_list): implement MessageList trait for GmailClient

- move MessageList trait and implementation to message_list.rs
- implement MessageList trait for GmailClient struct
- remove unused MessageList struct
- update function signatures to use GmailClient instead of MessageList struct
This commit is contained in:
Jeremiah Russell
2025-10-13 14:58:38 +01:00
committed by Jeremiah Russell
parent 940daca729
commit 247f8e6b00

View File

@@ -1,65 +1,54 @@
use std::fmt::Debug; use crate::{GmailClient, MessageSummary, Result};
use crate::{GmailClient, Result};
use google_gmail1::{ use google_gmail1::{
Gmail, api::ListMessagesResponse, hyper_rustls::HttpsConnector, Gmail, api::ListMessagesResponse, hyper_rustls::HttpsConnector,
hyper_util::client::legacy::connect::HttpConnector, hyper_util::client::legacy::connect::HttpConnector,
}; };
mod message_summary;
use message_summary::MessageSummary;
use crate::utils::Elide; use crate::utils::Elide;
/// Default for the maximum number of results to return on a page pub(crate) trait MessageList {
pub const DEFAULT_MAX_RESULTS: &str = "200"; async fn log_message_subjects(&mut self) -> Result<()>;
async fn messages_list(
/// Struct to capture configuration for List API call. &mut self,
pub struct MessageList { next_page_token: Option<String>,
hub: Gmail<HttpsConnector<HttpConnector>>, ) -> Result<ListMessagesResponse>;
max_results: u32, async fn run(&mut self, pages: u32) -> Result<()>;
label_ids: Vec<String>, fn hub(&self) -> Gmail<HttpsConnector<HttpConnector>>;
messages: Vec<MessageSummary>, fn label_ids(&self) -> Vec<String>;
query: String, fn message_ids(&self) -> Vec<String>;
fn messages(&self) -> &Vec<MessageSummary>;
fn set_query(&mut self, query: &str);
fn add_labels_ids(&mut self, label_ids: &[String]);
async fn add_labels(&mut self, client: &GmailClient, labels: &[String]) -> Result<()>;
fn max_results(&self) -> u32;
fn set_max_results(&mut self, value: u32);
} }
impl Debug for MessageList { impl MessageList for GmailClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { // /// Create a new List struct and add the Gmail api connection.
f.debug_struct("MessageList") // async fn new(client: &GmailClient) -> Result<Self> {
.field("max_results", &self.max_results) // Ok(MessageList {
.field("label_ids", &self.label_ids) // hub: client.hub(),
.field("messages", &self.messages) // max_results: DEFAULT_MAX_RESULTS.parse::<u32>().unwrap(),
.field("query", &self.query) // label_ids: Vec::new(),
.finish() // messages: Vec::new(),
} // query: String::new(),
} // })
// }
impl MessageList {
/// Create a new List struct and add the Gmail api connection.
pub async fn new(client: &GmailClient) -> Result<Self> {
Ok(MessageList {
hub: client.hub(),
max_results: DEFAULT_MAX_RESULTS.parse::<u32>().unwrap(),
label_ids: Vec::new(),
messages: Vec::new(),
query: String::new(),
})
}
/// Set the maximum results /// Set the maximum results
pub fn set_max_results(&mut self, value: u32) { fn set_max_results(&mut self, value: u32) {
self.max_results = value; self.max_results = value;
} }
/// Report the maximum results value /// Report the maximum results value
pub fn max_results(&self) -> u32 { fn max_results(&self) -> u32 {
self.max_results self.max_results
} }
/// Add label to the labels collection /// Add label to the labels collection
pub async fn add_labels(&mut self, client: &GmailClient, labels: &[String]) -> Result<()> { async fn add_labels(&mut self, client: &GmailClient, labels: &[String]) -> Result<()> {
log::debug!("labels from command line: {labels:?}"); log::debug!("labels from command line: {labels:?}");
let mut label_ids = Vec::new(); let mut label_ids = Vec::new();
for label in labels { for label in labels {
@@ -81,17 +70,17 @@ impl MessageList {
} }
/// Set the query string /// Set the query string
pub fn set_query(&mut self, query: &str) { fn set_query(&mut self, query: &str) {
self.query = query.to_string() self.query = query.to_string()
} }
/// Get the summary of the messages /// Get the summary of the messages
pub(crate) fn messages(&self) -> &Vec<MessageSummary> { fn messages(&self) -> &Vec<MessageSummary> {
&self.messages &self.messages
} }
/// Get a reference to the message_ids /// Get a reference to the message_ids
pub fn message_ids(&self) -> Vec<String> { fn message_ids(&self) -> Vec<String> {
self.messages self.messages
.iter() .iter()
.map(|m| m.id().to_string()) .map(|m| m.id().to_string())
@@ -100,17 +89,17 @@ impl MessageList {
} }
/// Get a reference to the message_ids /// Get a reference to the message_ids
pub fn label_ids(&self) -> Vec<String> { fn label_ids(&self) -> Vec<String> {
self.label_ids.clone() self.label_ids.clone()
} }
/// Get the hub /// Get the hub
pub fn hub(&self) -> Gmail<HttpsConnector<HttpConnector>> { fn hub(&self) -> Gmail<HttpsConnector<HttpConnector>> {
self.hub.clone() self.hub().clone()
} }
/// Run the Gmail api as configured /// Run the Gmail api as configured
pub async fn run(&mut self, pages: u32) -> Result<()> { async fn run(&mut self, pages: u32) -> Result<()> {
let list = self.messages_list(None).await?; let list = self.messages_list(None).await?;
match pages { match pages {
1 => {} 1 => {}
@@ -151,8 +140,8 @@ impl MessageList {
&mut self, &mut self,
next_page_token: Option<String>, next_page_token: Option<String>,
) -> Result<ListMessagesResponse> { ) -> Result<ListMessagesResponse> {
let mut call = self let hub = self.hub();
.hub let mut call = hub
.users() .users()
.messages_list("me") .messages_list("me")
.max_results(self.max_results); .max_results(self.max_results);
@@ -198,10 +187,10 @@ impl MessageList {
} }
async fn log_message_subjects(&mut self) -> Result<()> { async fn log_message_subjects(&mut self) -> Result<()> {
let hub = self.hub();
for message in &mut self.messages { for message in &mut self.messages {
log::trace!("{}", message.id()); log::trace!("{}", message.id());
let (_res, m) = self let (_res, m) = hub
.hub
.users() .users()
.messages_get("me", message.id()) .messages_get("me", message.id())
.add_scope("https://www.googleapis.com/auth/gmail.metadata") .add_scope("https://www.googleapis.com/auth/gmail.metadata")