Skip to content

Commit

Permalink
refactor: Reuse logic to get today's birthdays
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 26, 2023
1 parent 3380a2e commit 153f808
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ pub fn get_all_birthdays() -> Result<Vec<Birthday>> {
Ok(birthdays.unwrap())
}

pub fn get_birthdays_for_date(date: NaiveDate) -> Result<Vec<Birthday>> {
let birthdays = get_all_birthdays()?
.into_iter()
.filter(|birthday| {
birthday.date.month() == date.month() && birthday.date.day() == date.day()
})
.collect();
Ok(birthdays)
}

pub fn get_next_birthday(today: NaiveDate) -> Result<Option<Birthday>> {
let mut birthdays = get_all_birthdays()?;
birthdays.sort_by_key(|birthday| birthday.next(today));
Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use birthday::Birthday;
use chrono::Utc;
use chrono::{Datelike, Utc};
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -66,7 +66,12 @@ fn main() -> Result<()> {
}
Command::Today {} => {
let today = Utc::now().date_naive();
let birthdays = birthday::get_birthdays_for_date(today)?;
let birthdays = birthday::search_birthdays(
None,
Some(today.year()),
Some(today.month()),
Some(today.day()),
)?;
print_birthdays(birthdays);
Ok(())
}
Expand Down

0 comments on commit 153f808

Please sign in to comment.