feat(rules_cli): implement add command for managing retention rules

- add `add` subcommand to manage retention rules
- introduce `Period` enum for specifying time units
- implement logic to add and save new retention rules to the configuration
This commit is contained in:
Jeremiah Russell
2025-10-09 14:40:58 +01:00
committed by Jeremiah Russell
parent 801628310b
commit 44d8eef872

View File

@@ -0,0 +1,50 @@
use std::fmt;
use clap::{Parser, ValueEnum};
use cull_gmail::{Config, Error, MessageAge, Retention};
#[derive(Debug, Clone, Parser, ValueEnum)]
pub enum Period {
Days,
Weeks,
Months,
Years,
}
impl fmt::Display for Period {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Period::Days => write!(f, "days"),
Period::Weeks => write!(f, "weeks"),
Period::Months => write!(f, "months"),
Period::Years => write!(f, "years"),
}
}
}
#[derive(Debug, Parser)]
pub struct AddCli {
/// Period for the rule
#[arg(short, long)]
period: Period,
/// Count of the period
#[arg(short, long, default_value = "1")]
count: usize,
/// Optional specific label; if not specified one will be generated
#[arg(short, long)]
label: Option<String>,
/// Immediate delete instead of move to trash
#[arg(long)]
delete: bool,
}
impl AddCli {
pub fn run(&self, mut config: Config) -> Result<(), Error> {
let generate = self.label.is_none();
let message_age = MessageAge::new(self.period.to_string().as_str(), self.count);
let retention = Retention::new(message_age, generate);
config.add_rule(retention, self.label.as_ref(), self.delete);
config.save()
}
}