♻️ refactor(processor): use GmailClient instead of credential_file

- replace credential_file with GmailClient in Processor and ProcessorBuilder
- update builder method to accept GmailClient
- update trash_messages and delete_messages methods to use GmailClient
This commit is contained in:
Jeremiah Russell
2025-10-13 13:44:56 +01:00
committed by Jeremiah Russell
parent 4ba9d672a6
commit e5b93c0bdd

View File

@@ -1,23 +1,43 @@
// use crate::EolRule; // use crate::EolRule;
use crate::{Delete, EolAction, Error, Result, Trash, config::EolRule}; use std::fmt;
use crate::{Delete, EolAction, Error, GmailClient, Result, Trash, config::EolRule};
/// Rule processor /// Rule processor
#[derive(Debug)] #[derive()]
pub struct Processor<'a> { pub struct Processor<'a> {
credential_file: String, client: GmailClient,
rule: &'a EolRule, rule: &'a EolRule,
execute: bool, execute: bool,
} }
impl<'a> fmt::Debug for Processor<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Processor")
.field("rule", &self.rule)
.field("execute", &self.execute)
.finish()
}
}
/// Rule processor builder /// Rule processor builder
#[derive(Debug)] #[derive()]
pub struct ProcessorBuilder<'a> { pub struct ProcessorBuilder<'a> {
credential_file: String, client: GmailClient,
rule: &'a EolRule, rule: &'a EolRule,
execute: bool, execute: bool,
} }
impl<'a> fmt::Debug for ProcessorBuilder<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ProcessorBuilder")
.field("rule", &self.rule)
.field("execute", &self.execute)
.finish()
}
}
impl<'a> ProcessorBuilder<'a> { impl<'a> ProcessorBuilder<'a> {
/// Set the execute flag /// Set the execute flag
pub fn set_execute(&mut self, value: bool) -> &mut Self { pub fn set_execute(&mut self, value: bool) -> &mut Self {
@@ -28,7 +48,7 @@ impl<'a> ProcessorBuilder<'a> {
/// Build the Processor /// Build the Processor
pub fn build(&'_ self) -> Processor<'_> { pub fn build(&'_ self) -> Processor<'_> {
Processor { Processor {
credential_file: self.credential_file.clone(), client: self.client.clone(),
rule: self.rule, rule: self.rule,
execute: self.execute, execute: self.execute,
} }
@@ -37,9 +57,9 @@ impl<'a> ProcessorBuilder<'a> {
impl<'a> Processor<'a> { impl<'a> Processor<'a> {
/// Initialise a new processor /// Initialise a new processor
pub fn builder(credential_file: &str, rule: &'a EolRule) -> ProcessorBuilder<'a> { pub fn builder(client: GmailClient, rule: &'a EolRule) -> ProcessorBuilder<'a> {
ProcessorBuilder { ProcessorBuilder {
credential_file: credential_file.to_string(), client: client.clone(),
rule, rule,
execute: false, execute: false,
} }
@@ -52,10 +72,10 @@ impl<'a> Processor<'a> {
/// Trash the messages /// Trash the messages
pub async fn trash_messages(&self, label: &str) -> Result<()> { pub async fn trash_messages(&self, label: &str) -> Result<()> {
let mut messages_to_trash = Trash::new(&self.credential_file).await?; let mut messages_to_trash = Trash::new(&self.client).await?;
messages_to_trash messages_to_trash
.message_list() .message_list()
.add_labels(&self.credential_file, &[label.to_string()]) .add_labels(&self.client, &[label.to_string()])
.await?; .await?;
if messages_to_trash.message_list().label_ids().is_empty() { if messages_to_trash.message_list().label_ids().is_empty() {
@@ -81,11 +101,11 @@ impl<'a> Processor<'a> {
/// Delete the messages /// Delete the messages
pub async fn delete_messages(&self, label: &str) -> Result<()> { pub async fn delete_messages(&self, label: &str) -> Result<()> {
let mut messages_to_delete = Delete::new(&self.credential_file).await?; let mut messages_to_delete = Delete::new(&self.client).await?;
messages_to_delete messages_to_delete
.message_list() .message_list()
.add_labels(&self.credential_file, &[label.to_string()]) .add_labels(&self.client, &[label.to_string()])
.await?; .await?;
if messages_to_delete.message_list().label_ids().is_empty() { if messages_to_delete.message_list().label_ids().is_empty() {