From cc5a0ee155061acdcd29318093b71fcb07bac0cd Mon Sep 17 00:00:00 2001 From: veeshi <31014797+veeshi@users.noreply.github.com> Date: Sat, 23 Nov 2024 21:21:19 +0000 Subject: [PATCH] impl Display for Cron and as_str to get the original pattern (#7) --- src/lib.rs | 10 ++++++++++ src/pattern.rs | 11 ++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d2bce4d..9c38156 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -455,6 +455,16 @@ impl Cron { self.pattern.with_alternative_weekdays(); self } + + pub fn as_str(&self) -> &str { + self.pattern.as_str() + } +} + +impl std::fmt::Display for Cron { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.pattern) + } } // Enables creating a Cron instance from a string slice, returning a CronError if parsing fails. diff --git a/src/pattern.rs b/src/pattern.rs index 034eb4d..92b6807 100644 --- a/src/pattern.rs +++ b/src/pattern.rs @@ -506,11 +506,16 @@ impl CronPattern { self.days_of_week = CronComponent::new(0, 7, LAST_BIT | NTH_ALL, 1); self } + + // Get a reference to the original pattern + pub fn as_str(&self) -> &str { + &self.pattern + } } -impl ToString for CronPattern { - fn to_string(&self) -> String { - self.pattern.clone() +impl std::fmt::Display for CronPattern { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.pattern) } }