style: format code with rustfmt

This commit is contained in:
Jeremiah Russell
2025-10-19 08:30:01 +01:00
committed by Jeremiah Russell
parent 8f908df8f3
commit 2b420e53cb
2 changed files with 106 additions and 87 deletions

View File

@@ -196,12 +196,12 @@ impl Rules {
label: Option<&str>, label: Option<&str>,
delete: bool, delete: bool,
) -> &mut Self { ) -> &mut Self {
let current_labels: Vec<String> = self.rules.values() let current_labels: Vec<String> =
.flat_map(|rule| rule.labels()) self.rules.values().flat_map(|rule| rule.labels()).collect();
.collect();
if let Some(label_ref) = label if let Some(label_ref) = label
&& current_labels.iter().any(|l| l == label_ref) { && current_labels.iter().any(|l| l == label_ref)
{
log::warn!("a rule already applies to label {label_ref}"); log::warn!("a rule already applies to label {label_ref}");
return self; return self;
} }
@@ -244,9 +244,7 @@ impl Rules {
/// println!("Configured labels: {:?}", labels); /// println!("Configured labels: {:?}", labels);
/// ``` /// ```
pub fn labels(&self) -> Vec<String> { pub fn labels(&self) -> Vec<String> {
self.rules.values() self.rules.values().flat_map(|rule| rule.labels()).collect()
.flat_map(|rule| rule.labels())
.collect()
} }
/// Find the id of the rule that contains a label /// Find the id of the rule that contains a label
@@ -494,9 +492,8 @@ impl Rules {
/// * IO errors when writing to the file system /// * IO errors when writing to the file system
/// * File system permission errors /// * File system permission errors
pub fn save(&self) -> Result<()> { pub fn save(&self) -> Result<()> {
let home_dir = env::home_dir().ok_or_else(|| { let home_dir = env::home_dir()
Error::HomeExpansionFailed("~/.cull-gmail/rules.toml".to_string()) .ok_or_else(|| Error::HomeExpansionFailed("~/.cull-gmail/rules.toml".to_string()))?;
})?;
let path = PathBuf::new().join(home_dir).join(".cull-gmail/rules.toml"); let path = PathBuf::new().join(home_dir).join(".cull-gmail/rules.toml");
// Ensure directory exists // Ensure directory exists
@@ -540,9 +537,8 @@ impl Rules {
/// * TOML parsing errors if the file is malformed /// * TOML parsing errors if the file is malformed
/// * File not found errors if the configuration doesn't exist /// * File not found errors if the configuration doesn't exist
pub fn load() -> Result<Rules> { pub fn load() -> Result<Rules> {
let home_dir = env::home_dir().ok_or_else(|| { let home_dir = env::home_dir()
Error::HomeExpansionFailed("~/.cull-gmail/rules.toml".to_string()) .ok_or_else(|| Error::HomeExpansionFailed("~/.cull-gmail/rules.toml".to_string()))?;
})?;
let path = PathBuf::new().join(home_dir).join(".cull-gmail/rules.toml"); let path = PathBuf::new().join(home_dir).join(".cull-gmail/rules.toml");
log::trace!("Loading config from {}", path.display()); log::trace!("Loading config from {}", path.display());
@@ -609,7 +605,10 @@ mod tests {
// Should have some default rules // Should have some default rules
let labels = rules.labels(); let labels = rules.labels();
assert!(!labels.is_empty(), "Default rules should create some labels"); assert!(
!labels.is_empty(),
"Default rules should create some labels"
);
// Should contain the expected retention labels // Should contain the expected retention labels
assert!(labels.iter().any(|l| l.contains("retention/1-years"))); assert!(labels.iter().any(|l| l.contains("retention/1-years")));

View File

@@ -477,8 +477,13 @@ mod test {
fn test_eol_query_for_eol_rule_5_years() { fn test_eol_query_for_eol_rule_5_years() {
let rule = build_test_rule(crate::MessageAge::Years(5)); let rule = build_test_rule(crate::MessageAge::Years(5));
let test_today = Local.with_ymd_and_hms(2025, 9, 15, 0, 0, 0).single().unwrap(); let test_today = Local
let query = rule.calculate_for_date(test_today).expect("Failed to calculate query"); .with_ymd_and_hms(2025, 9, 15, 0, 0, 0)
.single()
.unwrap();
let query = rule
.calculate_for_date(test_today)
.expect("Failed to calculate query");
assert_eq!("before: 2020-09-15", query); assert_eq!("before: 2020-09-15", query);
} }
@@ -487,8 +492,13 @@ mod test {
fn test_eol_query_for_eol_rule_1_month() { fn test_eol_query_for_eol_rule_1_month() {
let rule = build_test_rule(crate::MessageAge::Months(1)); let rule = build_test_rule(crate::MessageAge::Months(1));
let test_today = Local.with_ymd_and_hms(2025, 9, 15, 0, 0, 0).single().unwrap(); let test_today = Local
let query = rule.calculate_for_date(test_today).expect("Failed to calculate query"); .with_ymd_and_hms(2025, 9, 15, 0, 0, 0)
.single()
.unwrap();
let query = rule
.calculate_for_date(test_today)
.expect("Failed to calculate query");
assert_eq!("before: 2025-08-15", query); assert_eq!("before: 2025-08-15", query);
} }
@@ -497,8 +507,13 @@ mod test {
fn test_eol_query_for_eol_rule_13_weeks() { fn test_eol_query_for_eol_rule_13_weeks() {
let rule = build_test_rule(crate::MessageAge::Weeks(13)); let rule = build_test_rule(crate::MessageAge::Weeks(13));
let test_today = Local.with_ymd_and_hms(2025, 9, 15, 0, 0, 0).single().unwrap(); let test_today = Local
let query = rule.calculate_for_date(test_today).expect("Failed to calculate query"); .with_ymd_and_hms(2025, 9, 15, 0, 0, 0)
.single()
.unwrap();
let query = rule
.calculate_for_date(test_today)
.expect("Failed to calculate query");
assert_eq!("before: 2025-06-16", query); assert_eq!("before: 2025-06-16", query);
} }
@@ -507,8 +522,13 @@ mod test {
fn test_eol_query_for_eol_rule_365_days() { fn test_eol_query_for_eol_rule_365_days() {
let rule = build_test_rule(crate::MessageAge::Days(365)); let rule = build_test_rule(crate::MessageAge::Days(365));
let test_today = Local.with_ymd_and_hms(2025, 9, 15, 0, 0, 0).single().unwrap(); let test_today = Local
let query = rule.calculate_for_date(test_today).expect("Failed to calculate query"); .with_ymd_and_hms(2025, 9, 15, 0, 0, 0)
.single()
.unwrap();
let query = rule
.calculate_for_date(test_today)
.expect("Failed to calculate query");
assert_eq!("before: 2024-09-15", query); assert_eq!("before: 2024-09-15", query);
} }