diff --git a/README.md b/README.md index 40b4f0b..a289dd1 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ of your OS, but you can also override this behavior by defining a custom path vi ```shell # Add someone's birthday -$ birthday add "Ben Dover" 03/05/1990 -$ birthday add "Hugh Jarse" 10/12/1995 -$ birthday add "Anita Bath" 22/09/1987 +$ birthday add "Ben Dover" 03 05 1990 +$ birthday add "Hugh Jarse" 10 12 1995 +$ birthday add "Anita Bath" 22 09 1987 # Show all birthdays $ birthday all diff --git a/src/birthday_store.rs b/src/birthday_store.rs index c42676f..a3efaa5 100644 --- a/src/birthday_store.rs +++ b/src/birthday_store.rs @@ -7,9 +7,8 @@ use rusqlite::Connection; use crate::Birthday; -pub fn add(name: String, date: String) -> Result<()> { +pub fn add(name: String, date: NaiveDate) -> Result<()> { let db = get_db()?; - let date = NaiveDate::parse_from_str(&date, "%Y-%m-%d")?; let timestamp = to_timestamp(date); db.execute( "INSERT INTO birthdays(name, date_timestamp) VALUES(?1, ?2)", diff --git a/src/lib.rs b/src/lib.rs index 9d4e1fd..1d135c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,8 +4,9 @@ use anyhow::{bail, Result}; pub use birthday::Birthday; use chrono::{Datelike, NaiveDate}; -pub fn add(name: String, date: String) -> Result<()> { - birthday_store::add(name, date) +pub fn add(name: String, day: u32, month: u32, year: i32) -> Result<()> { + let birthdate = NaiveDate::from_ymd_opt(year, month, day).expect("Invalid date"); + birthday_store::add(name, birthdate) } pub fn get_all() -> Result> { diff --git a/src/main.rs b/src/main.rs index d075256..4d841b9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,13 @@ struct Cli { #[derive(Subcommand, Debug)] enum Command { #[command(about = "Add a person's birthday")] - Add { name: String, date: String }, + Add { + name: String, + day: u32, + month: u32, + #[arg(allow_negative_numbers = true)] + year: i32, + }, #[command(about = "Show all birthdays")] All {}, #[command(about = "Show the next birthday")] @@ -42,7 +48,12 @@ fn main() -> Result<()> { let cli = Cli::parse(); let today = Utc::now().date_naive(); match cli.command { - Command::Add { name, date } => birthday::add(name, date), + Command::Add { + name, + day, + month, + year, + } => birthday::add(name, day, month, year), Command::All {} => { let birthdays = birthday::get_all()?; output::output(birthdays, today);