✨ 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:
committed by
Jeremiah Russell
parent
ef0c9ebd89
commit
4749e83cf3
@@ -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());
|
log::info!("Executing rule `#{}` for label `{label}`", rule.describe());
|
||||||
|
client.initialise_message_list();
|
||||||
client.set_rule(rule.clone());
|
client.set_rule(rule.clone());
|
||||||
client.set_execute(execute);
|
client.set_execute(execute);
|
||||||
if let Err(e) = client.find_rule_and_messages_for_label(&label).await {
|
if let Err(e) = client.find_rule_and_messages_for_label(&label).await {
|
||||||
|
|||||||
@@ -196,6 +196,33 @@ pub trait RuleProcessor {
|
|||||||
/// in dry-run mode (`false`) before enabling execution.
|
/// in dry-run mode (`false`) before enabling execution.
|
||||||
fn set_execute(&mut self, value: bool);
|
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.
|
/// Configures the end-of-life rule to apply during processing.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
@@ -290,6 +317,16 @@ pub trait RuleProcessor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl RuleProcessor for GmailClient {
|
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.
|
/// Configures the end-of-life rule for this Gmail client.
|
||||||
///
|
///
|
||||||
/// The rule defines which messages to target and what action to perform on them.
|
/// The rule defines which messages to target and what action to perform on them.
|
||||||
@@ -447,7 +484,7 @@ impl RuleProcessor for GmailClient {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{EolAction, Error, rules::EolRule};
|
use crate::{EolAction, Error, MessageSummary, rules::EolRule};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
/// Test helper to create a simple EolRule with or without a query
|
/// 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
|
// For now, we'll create a simple struct that implements RuleProcessor
|
||||||
|
|
||||||
struct MockProcessor {
|
struct MockProcessor {
|
||||||
|
messages: Vec<MessageSummary>,
|
||||||
rule: Option<EolRule>,
|
rule: Option<EolRule>,
|
||||||
execute: bool,
|
execute: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RuleProcessor for MockProcessor {
|
impl RuleProcessor for MockProcessor {
|
||||||
|
fn initialise_message_list(&mut self) {
|
||||||
|
self.messages = Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
fn set_rule(&mut self, rule: EolRule) {
|
fn set_rule(&mut self, rule: EolRule) {
|
||||||
self.rule = Some(rule);
|
self.rule = Some(rule);
|
||||||
}
|
}
|
||||||
@@ -732,6 +774,7 @@ mod tests {
|
|||||||
let mut processor = MockProcessor {
|
let mut processor = MockProcessor {
|
||||||
rule: None,
|
rule: None,
|
||||||
execute: false,
|
execute: false,
|
||||||
|
messages: Vec::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Test initial state
|
// Test initial state
|
||||||
|
|||||||
Reference in New Issue
Block a user