Skip to content

Commit

Permalink
feat: Display the person's age
Browse files Browse the repository at this point in the history
  • Loading branch information
ducdetronquito committed Dec 23, 2023
1 parent 7dc3959 commit 1bba2f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ pub fn add_birthday(name: String, date: String) -> Result<()> {
Ok(())
}

#[derive(Debug)]
pub struct Birthday {
name: String,
date: NaiveDate,
pub name: String,
pub date: NaiveDate,
}

impl Birthday {
pub fn age(&self, today: NaiveDate) -> Option<u32> {
today.years_since(self.date)
}
}

pub fn get_all_birthdays() -> Result<Vec<Birthday>> {
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;
use birthday::{add_birthday, Birthday};
use birthday::Birthday;
use chrono::Utc;
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -32,8 +33,16 @@ enum Command {
}

fn print_birthdays(birthdays: Vec<Birthday>) {
let today = Utc::now().date_naive();
for birthday in birthdays {
println!("{:?}", birthday);
let age = match birthday.age(today) {
Some(age) => age.to_string(),
None => "".to_owned(),
};
println!(
"Name={} Birthdate={} Age={}",
birthday.name, birthday.date, age
);
}
}

Expand Down

0 comments on commit 1bba2f7

Please sign in to comment.