use chrono::Datelike;
use chrono::NaiveDate;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::error::{TypeError, TypeResult};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BirthDate(NaiveDate);
impl BirthDate {
const FORMAT: &str = "%Y-%m-%d";
pub fn parse(value: impl AsRef<str>) -> TypeResult<Self> {
let raw: &str = value.as_ref().trim();
let date: NaiveDate = NaiveDate::parse_from_str(raw, Self::FORMAT).map_err(|_| {
TypeError::validation(format!("birth_date is not a valid YYYY-MM-DD date: {raw}"))
})?;
let today: NaiveDate = chrono::Local::now().date_naive();
if date > today {
return Err(TypeError::validation(format!(
"birth_date cannot be in the future: {raw}"
)));
}
Ok(Self(date))
}
pub fn year(&self) -> u32 {
self.0.year() as u32
}
pub fn month(&self) -> u32 {
self.0.month()
}
pub fn day(&self) -> u32 {
self.0.day()
}
}
impl std::fmt::Display for BirthDate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.format(Self::FORMAT))
}
}
impl TryFrom<&str> for BirthDate {
type Error = TypeError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::parse(value)
}
}
impl TryFrom<String> for BirthDate {
type Error = TypeError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::try_from(value.as_str())
}
}
impl From<BirthDate> for String {
fn from(value: BirthDate) -> Self {
value.to_string()
}
}
impl Serialize for BirthDate {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for BirthDate {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = <&str>::deserialize(deserializer)?;
Self::parse(s).map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_valid_birth_date() {
let date = BirthDate::parse("1990-05-15").unwrap();
assert_eq!(date.to_string(), "1990-05-15");
}
#[test]
fn parse_should_fail_for_invalid_format() {
assert!(BirthDate::parse("15-05-1990").is_err());
assert!(BirthDate::parse("1990/05/15").is_err());
}
#[test]
fn parse_should_fail_for_invalid_month() {
assert!(BirthDate::parse("1990-13-01").is_err());
}
#[test]
fn parse_should_fail_for_invalid_day() {
assert!(BirthDate::parse("1990-01-32").is_err());
}
#[test]
fn parse_should_validate_leap_year() {
assert!(BirthDate::parse("2000-02-29").is_ok());
assert!(BirthDate::parse("1999-02-29").is_err());
}
#[test]
fn parse_should_fail_for_future_date() {
assert!(BirthDate::parse("2100-01-01").is_err());
}
#[test]
fn parse_should_trim_whitespace() {
let date = BirthDate::parse(" 1990-05-15 ").unwrap();
assert_eq!(date.to_string(), "1990-05-15");
}
#[test]
fn display_should_return_expected_format() {
let date = BirthDate::parse("2000-01-01").unwrap();
assert_eq!(format!("{}", date), "2000-01-01");
}
#[test]
fn birth_date_should_implement_copy() {
let first = BirthDate::parse("1985-03-20").unwrap();
let second = first;
assert_eq!(first, second);
}
#[test]
fn birth_date_should_convert_into_string() {
let date = BirthDate::parse("1990-05-15").unwrap();
let value: String = date.into();
assert_eq!(value, "1990-05-15");
}
#[test]
fn birth_date_should_support_serde_roundtrip() {
let date = BirthDate::parse("1990-05-15").unwrap();
let json = serde_json::to_string(&date).unwrap();
let restored: BirthDate = serde_json::from_str(&json).unwrap();
assert_eq!(date, restored);
}
#[test]
fn deserialize_should_create_valid_birth_date() {
let json = "\"2025-01-01\"";
let date: BirthDate = serde_json::from_str(json).unwrap();
assert_eq!(date.to_string(), "2025-01-01");
}
#[test]
fn serialize_should_use_expected_format() {
let date = BirthDate::parse("2025-01-01").unwrap();
let json = serde_json::to_string(&date).unwrap();
assert_eq!(json, "\"2025-01-01\"");
}
}