Skip to content

Commit

Permalink
refactor: use cleaner approach for defining today
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Aug 7, 2024
1 parent f403c07 commit cbca1b2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
}
Expand Down
5 changes: 0 additions & 5 deletions src/progress/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub use week::week;

use jiff::civil;
use jiff::Unit;
use jiff::Zoned;

use crate::error::Error;

Expand All @@ -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()
}
6 changes: 3 additions & 3 deletions src/progress/month.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use jiff::civil;

use super::{compute, today};
use super::compute;
use crate::error::Error;

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

fn month_ratio(today: civil::Date) -> Result<f64, Error> {
Expand Down
6 changes: 3 additions & 3 deletions src/progress/week.rs
Original file line number Diff line number Diff line change
@@ -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<f64, Error> {
week_ratio(today())
pub fn week(today: civil::Date) -> Result<f64, Error> {
week_ratio(today)
}

fn week_ratio(current: civil::Date) -> Result<f64, Error> {
Expand Down
6 changes: 3 additions & 3 deletions src/progress/year.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use jiff::civil;

use super::{compute, today};
use super::compute;
use crate::error::Error;

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

fn year_ratio(today: civil::Date) -> Result<f64, Error> {
Expand Down
6 changes: 4 additions & 2 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use anyhow::Result;
use assert_cmd::{crate_name, prelude::*};
use predicates::prelude::*; // Import chrono's prelude to use DateTime<Utc> and Local.

use zman::progress::today;

#[test]
fn help() -> Result<()> {
let mut cmd = Command::cargo_bin(crate_name!())?;
Expand Down Expand Up @@ -69,3 +67,7 @@ fn rest_bar() -> Result<()> {

Ok(())
}

fn today() -> jiff::civil::Date {
jiff::Zoned::now().date()
}

0 comments on commit cbca1b2

Please sign in to comment.