From cbca1b2169889defa66d856f1ba882ee0e3e6a41 Mon Sep 17 00:00:00 2001 From: "Azzam S.A" Date: Wed, 7 Aug 2024 07:18:23 +0700 Subject: [PATCH] refactor: use cleaner approach for defining `today` --- src/main.rs | 9 ++++++--- src/progress/mod.rs | 5 ----- src/progress/month.rs | 6 +++--- src/progress/week.rs | 6 +++--- src/progress/year.rs | 6 +++--- tests/integration.rs | 6 ++++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6f2837b..390b8ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ use clap::Parser; use std::{process, sync::Arc}; use anyhow::Result; +use jiff::Zoned; + use zman::{ cli::{Opts, Period}, output::Printer, @@ -18,20 +20,21 @@ fn main() { fn run() -> Result<()> { let opts = Arc::new(Opts::parse()); + let today = Zoned::now().date(); let mut printer = Printer::new(Arc::clone(&opts)); match opts.period { Period::Year => { - let ratio = progress::year()?; + let ratio = progress::year(today)?; printer = printer.ratio(ratio).ratio_char("y"); printer.print(); } Period::Month => { - let ratio = progress::month()?; + let ratio = progress::month(today)?; printer = printer.ratio(ratio).ratio_char("m"); printer.print(); } Period::Week => { - let ratio = progress::week()?; + let ratio = progress::week(today)?; printer = printer.ratio(ratio).ratio_char("w"); printer.print(); } diff --git a/src/progress/mod.rs b/src/progress/mod.rs index e1332a2..4deca00 100644 --- a/src/progress/mod.rs +++ b/src/progress/mod.rs @@ -7,7 +7,6 @@ pub use week::week; use jiff::civil; use jiff::Unit; -use jiff::Zoned; use crate::error::Error; @@ -26,7 +25,3 @@ fn compute(current: civil::Date, start: civil::Date, end: civil::Date) -> Result let ratio = current_diff_in_seconds / whole_diff_in_seconds; Ok(ratio) } - -pub fn today() -> civil::Date { - Zoned::now().date() -} diff --git a/src/progress/month.rs b/src/progress/month.rs index 3c133f9..68c7e4e 100644 --- a/src/progress/month.rs +++ b/src/progress/month.rs @@ -1,10 +1,10 @@ use jiff::civil; -use super::{compute, today}; +use super::compute; use crate::error::Error; -pub fn month() -> Result { - month_ratio(today()) +pub fn month(today: civil::Date) -> Result { + month_ratio(today) } fn month_ratio(today: civil::Date) -> Result { diff --git a/src/progress/week.rs b/src/progress/week.rs index 7bb1cd5..b642790 100644 --- a/src/progress/week.rs +++ b/src/progress/week.rs @@ -1,11 +1,11 @@ use jiff::civil; use jiff::ToSpan; -use super::{compute, today}; +use super::compute; use crate::error::Error; -pub fn week() -> Result { - week_ratio(today()) +pub fn week(today: civil::Date) -> Result { + week_ratio(today) } fn week_ratio(current: civil::Date) -> Result { diff --git a/src/progress/year.rs b/src/progress/year.rs index 861ae78..6d25d93 100644 --- a/src/progress/year.rs +++ b/src/progress/year.rs @@ -1,10 +1,10 @@ use jiff::civil; -use super::{compute, today}; +use super::compute; use crate::error::Error; -pub fn year() -> Result { - year_ratio(today()) +pub fn year(today: civil::Date) -> Result { + year_ratio(today) } fn year_ratio(today: civil::Date) -> Result { diff --git a/tests/integration.rs b/tests/integration.rs index 73d616e..c986ea5 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -4,8 +4,6 @@ use anyhow::Result; use assert_cmd::{crate_name, prelude::*}; use predicates::prelude::*; // Import chrono's prelude to use DateTime and Local. -use zman::progress::today; - #[test] fn help() -> Result<()> { let mut cmd = Command::cargo_bin(crate_name!())?; @@ -69,3 +67,7 @@ fn rest_bar() -> Result<()> { Ok(()) } + +fn today() -> jiff::civil::Date { + jiff::Zoned::now().date() +}