feat(rule_processor): add initialise_message_list to processor

- add initialise_message_list fn to RuleProcessor trait and impl
- ensure rule is only processed on in-scope messages
- call initialise_message_list before processing any labels
This commit is contained in:
Jeremiah Russell
2025-10-28 16:10:03 +00:00
committed by Jeremiah Russell
parent ef0c9ebd89
commit 4749e83cf3
2 changed files with 45 additions and 1 deletions

View File

@@ -497,6 +497,7 @@ async fn run_rules(client: &mut GmailClient, rules: Rules, execute: bool) -> Res
};
log::info!("Executing rule `#{}` for label `{label}`", rule.describe());
client.initialise_message_list();
client.set_rule(rule.clone());
client.set_execute(execute);
if let Err(e) = client.find_rule_and_messages_for_label(&label).await {

View File

@@ -196,6 +196,33 @@ pub trait RuleProcessor {
/// in dry-run mode (`false`) before enabling execution.
fn set_execute(&mut self, value: bool);
/// Initialises the message list to prepare for application of rule.
///
/// # Arguments
///
/// * none
///
/// # Example
///
/// ```text
/// use cull_gmail::{GmailClient, RuleProcessor, ClientConfig};
///
/// async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let config = ClientConfig::builder()
/// .with_client_id("your-client-id")
/// .with_client_secret("your-client-secret")
/// .build();
/// let mut client = GmailClient::new_with_config(config).await?;
///
/// // Rules would typically be loaded from configuration
/// // let rule = load_rule_from_config();
/// // client.initialise_message_list();
/// // client.set_rule(rule);
/// Ok(())
/// }
/// ```
fn initialise_message_list(&mut self);
/// Configures the end-of-life rule to apply during processing.
///
/// # Arguments
@@ -290,6 +317,16 @@ pub trait RuleProcessor {
}
impl RuleProcessor for GmailClient {
/// Initialise the message list.
///
/// The message list is initialised to ensure that the rule is only processed
/// on the in-scope messages.
///
/// This must be called before processing any labels.
fn initialise_message_list(&mut self) {
self.messages = Vec::new();
}
/// Configures the end-of-life rule for this Gmail client.
///
/// The rule defines which messages to target and what action to perform on them.
@@ -447,7 +484,7 @@ impl RuleProcessor for GmailClient {
#[cfg(test)]
mod tests {
use super::*;
use crate::{EolAction, Error, rules::EolRule};
use crate::{EolAction, Error, MessageSummary, rules::EolRule};
use std::sync::{Arc, Mutex};
/// Test helper to create a simple EolRule with or without a query
@@ -695,11 +732,16 @@ mod tests {
// For now, we'll create a simple struct that implements RuleProcessor
struct MockProcessor {
messages: Vec<MessageSummary>,
rule: Option<EolRule>,
execute: bool,
}
impl RuleProcessor for MockProcessor {
fn initialise_message_list(&mut self) {
self.messages = Vec::new();
}
fn set_rule(&mut self, rule: EolRule) {
self.rule = Some(rule);
}
@@ -732,6 +774,7 @@ mod tests {
let mut processor = MockProcessor {
rule: None,
execute: false,
messages: Vec::new(),
};
// Test initial state