Skip to content

Commit

Permalink
feat: Add birthdays search
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 25, 2023
1 parent db1613e commit 69d6bef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ Ben Dover 03/05/1990 34yo
$ birthday search --name Ben
Ben Dover 03/05/1990 34yo

# Get all birthdays for a specific day and month
$ birthday search --date 22/09
# Get all birthdays for a specific year
$ birthday search --year 1987
Anita Bath 22/09/1987 37yo

# Get all birthdays for a specific month
$ birthday search --month 12
Hugh Jarse 10/12/1995 39yo

# Get all birthdays for a specific day
$ birthday search --month 3
Ben Dover 03/05/1990 34yo
```
35 changes: 34 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{bail, Result};
use chrono::{Datelike, NaiveDate, NaiveDateTime};
use rusqlite::Connection;

Expand Down Expand Up @@ -85,3 +85,36 @@ pub fn get_next_birthday(today: NaiveDate) -> Result<Option<Birthday>> {
birthdays.sort_by_key(|birthday| birthday.next(today));
Ok(birthdays.into_iter().next())
}

pub fn search_birthdays(
name: Option<String>,
year: Option<i32>,
month: Option<u32>,
day: Option<u32>,
) -> Result<Vec<Birthday>> {
let mut birthdays = get_all_birthdays()?;

if let Some(name) = name {
birthdays.retain(|birthday| birthday.name.contains(&name));
}

if let Some(year) = year {
birthdays.retain(|birthday| birthday.date.year() == year);
}

if let Some(month) = month {
if !(1..=12).contains(&month) {
bail!("Month must be between 1 and 12");
}
birthdays.retain(|birthday| birthday.date.month() == month);
}

if let Some(day) = day {
if !(1..=31).contains(&day) {
bail!("Month must be between 1 and 31");
}
birthdays.retain(|birthday| birthday.date.day() == day);
}

Ok(birthdays)
}
19 changes: 16 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ enum Command {
Search {
#[arg(short, long, help = "match for names containing <NAME>")]
name: Option<String>,
#[arg(short, long, help = "match for a specific <DATE>")]
date: Option<String>,
#[arg(short, long, help = "match for a specific <YEAR>")]
year: Option<i32>,
#[arg(short, long, help = "match for a specific <MONTH>")]
month: Option<u32>,
#[arg(short, long, help = "match for a specific <DAY>")]
day: Option<u32>,
},
#[command(about = "Show today's birthdays")]
Today {},
Expand All @@ -50,7 +54,16 @@ fn main() -> Result<()> {
}
Ok(())
}
Command::Search { .. } => todo!(),
Command::Search {
name,
year,
month,
day,
} => {
let birthdays = birthday::search_birthdays(name, year, month, day)?;
print_birthdays(birthdays);
Ok(())
}
Command::Today {} => {
let today = Utc::now().date_naive();
let birthdays = birthday::get_birthdays_for_date(today)?;
Expand Down

0 comments on commit 69d6bef

Please sign in to comment.