Skip to content

Commit cbca1b2

Browse files
committed
refactor: use cleaner approach for defining today
1 parent f403c07 commit cbca1b2

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

src/main.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use clap::Parser;
22
use std::{process, sync::Arc};
33

44
use anyhow::Result;
5+
use jiff::Zoned;
6+
57
use zman::{
68
cli::{Opts, Period},
79
output::Printer,
@@ -18,20 +20,21 @@ fn main() {
1820
fn run() -> Result<()> {
1921
let opts = Arc::new(Opts::parse());
2022

23+
let today = Zoned::now().date();
2124
let mut printer = Printer::new(Arc::clone(&opts));
2225
match opts.period {
2326
Period::Year => {
24-
let ratio = progress::year()?;
27+
let ratio = progress::year(today)?;
2528
printer = printer.ratio(ratio).ratio_char("y");
2629
printer.print();
2730
}
2831
Period::Month => {
29-
let ratio = progress::month()?;
32+
let ratio = progress::month(today)?;
3033
printer = printer.ratio(ratio).ratio_char("m");
3134
printer.print();
3235
}
3336
Period::Week => {
34-
let ratio = progress::week()?;
37+
let ratio = progress::week(today)?;
3538
printer = printer.ratio(ratio).ratio_char("w");
3639
printer.print();
3740
}

src/progress/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub use week::week;
77

88
use jiff::civil;
99
use jiff::Unit;
10-
use jiff::Zoned;
1110

1211
use crate::error::Error;
1312

@@ -26,7 +25,3 @@ fn compute(current: civil::Date, start: civil::Date, end: civil::Date) -> Result
2625
let ratio = current_diff_in_seconds / whole_diff_in_seconds;
2726
Ok(ratio)
2827
}
29-
30-
pub fn today() -> civil::Date {
31-
Zoned::now().date()
32-
}

src/progress/month.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use jiff::civil;
22

3-
use super::{compute, today};
3+
use super::compute;
44
use crate::error::Error;
55

6-
pub fn month() -> Result<f64, Error> {
7-
month_ratio(today())
6+
pub fn month(today: civil::Date) -> Result<f64, Error> {
7+
month_ratio(today)
88
}
99

1010
fn month_ratio(today: civil::Date) -> Result<f64, Error> {

src/progress/week.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use jiff::civil;
22
use jiff::ToSpan;
33

4-
use super::{compute, today};
4+
use super::compute;
55
use crate::error::Error;
66

7-
pub fn week() -> Result<f64, Error> {
8-
week_ratio(today())
7+
pub fn week(today: civil::Date) -> Result<f64, Error> {
8+
week_ratio(today)
99
}
1010

1111
fn week_ratio(current: civil::Date) -> Result<f64, Error> {

src/progress/year.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use jiff::civil;
22

3-
use super::{compute, today};
3+
use super::compute;
44
use crate::error::Error;
55

6-
pub fn year() -> Result<f64, Error> {
7-
year_ratio(today())
6+
pub fn year(today: civil::Date) -> Result<f64, Error> {
7+
year_ratio(today)
88
}
99

1010
fn year_ratio(today: civil::Date) -> Result<f64, Error> {

tests/integration.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use anyhow::Result;
44
use assert_cmd::{crate_name, prelude::*};
55
use predicates::prelude::*; // Import chrono's prelude to use DateTime<Utc> and Local.
66

7-
use zman::progress::today;
8-
97
#[test]
108
fn help() -> Result<()> {
119
let mut cmd = Command::cargo_bin(crate_name!())?;
@@ -69,3 +67,7 @@ fn rest_bar() -> Result<()> {
6967

7068
Ok(())
7169
}
70+
71+
fn today() -> jiff::civil::Date {
72+
jiff::Zoned::now().date()
73+
}

0 commit comments

Comments
 (0)