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 d7089d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 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
23 changes: 16 additions & 7 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 @@ -31,12 +32,6 @@ enum Command {
Today {},
}

fn print_birthdays(birthdays: Vec<Birthday>) {
for birthday in birthdays {
println!("{:?}", birthday);
}
}

fn main() -> Result<()> {
let cli = Cli::parse();
println!("You ran cli with: {:?}", cli);
Expand All @@ -52,3 +47,17 @@ fn main() -> Result<()> {
Command::Today {} => todo!(),
}
}

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

0 comments on commit d7089d3

Please sign in to comment.