use {
crate::{
error::HbdResult,
files::{config::ToolConfig, storage::Storage},
utils::{
date::DateAndYear,
fmt::{fmt_string, FormatWith},
},
},
chrono::{Datelike, Utc},
};
pub fn get(separator: Option<String>) -> HbdResult<()> {
let storage_birthdays = Storage::read_from_json()?;
let mut config_birthdays = ToolConfig::read_from_config()?;
let now_utc = Utc::now();
let now = DateAndYear::from_naivedate(&now_utc.date_naive());
let ref_separator: &Option<String> = &separator;
if let Some(birthdays) = storage_birthdays.birthdays().get(now.date_u16()) {
let iter = birthdays
.iter()
.filter(|b| {
!storage_birthdays
.reads()
.get(*b)
.map(|v| v.contains(&(now_utc.year() as u16)))
.unwrap_or(false)
})
.collect::<Vec<&String>>();
let num_of_birthdays = iter.len();
for (idx, birthday_of) in iter.into_iter().enumerate() {
if let Some(year) = storage_birthdays.ages().get(birthday_of) {
let age = now_utc.year() - *year as i32;
print!(
"{}",
fmt_string(
if ref_separator.is_some() {
config_birthdays.separator_happy_birthday_age()
} else {
config_birthdays.happy_birthday_age()
},
FormatWith::new(birthday_of, age.to_string().as_str()),
)
);
}
else {
print!(
"{}",
fmt_string(
if ref_separator.is_some() {
config_birthdays.separator_happy_birthday()
} else {
config_birthdays.happy_birthday()
},
FormatWith::s(birthday_of),
)
);
}
if ref_separator.is_some() {
if num_of_birthdays - 1 != idx {
print!("{}", ref_separator.as_ref().unwrap());
}
} else {
println!();
}
}
}
Ok(())
}