From 49e7ff5617a5c097cba18f4498798cae276a5dfb Mon Sep 17 00:00:00 2001 From: Jeremiah Russell Date: Fri, 10 Oct 2025 22:40:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(message=5Fage):=20correct=20?= =?UTF-8?q?data=20type=20for=20message=20age=20count?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - change the data type of count from usize to i64 to allow negative values - add parse method to parse message age from string --- src/retention/message_age.rs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/retention/message_age.rs b/src/retention/message_age.rs index 199090f..7f5e541 100644 --- a/src/retention/message_age.rs +++ b/src/retention/message_age.rs @@ -4,13 +4,13 @@ use std::fmt::Display; #[derive(Debug)] pub enum MessageAge { /// Number of days to retain the message - Days(usize), + Days(i64), /// Number of weeks to retain the message - Weeks(usize), + Weeks(i64), /// Number of months to retain the message - Months(usize), + Months(i64), /// Number of years to retain the message - Years(usize), + Years(i64), } impl Display for MessageAge { @@ -26,7 +26,7 @@ impl Display for MessageAge { impl MessageAge { /// Create a new MessageAge enum - pub fn new(period: &str, count: usize) -> Self { + pub fn new(period: &str, count: i64) -> Self { match period.to_lowercase().as_str() { "days" => MessageAge::Days(count), "weeks" => MessageAge::Weeks(count), @@ -36,6 +36,19 @@ impl MessageAge { } } + pub(crate) fn parse(str: &str) -> Option { + let period = str.chars().nth(0).unwrap_or('x'); + let count = str[2..].to_string().parse::().unwrap_or(0); + + match period { + 'd' => Some(MessageAge::Days(count)), + 'w' => Some(MessageAge::Weeks(count)), + 'm' => Some(MessageAge::Months(count)), + 'y' => Some(MessageAge::Years(count)), + _ => None, + } + } + pub(crate) fn label(&self) -> String { match self { MessageAge::Days(v) => format!("retention/{v}-days"),