use chrono::{Datelike, 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: &'static str = "%Y-%m-%d";
pub fn parse(value: impl AsRef<str>) -> TypeResult<Self> {
let raw = value.as_ref().trim();
let date = NaiveDate::parse_from_str(raw, Self::FORMAT)
.map_err(|_| BirthDateError::InvalidDate)?;
let today = chrono::Local::now().date_naive();
if date > today {
return Err(BirthDateError::FutureDate.into());
}
Ok(Self(date))
}
#[inline]
pub fn year(&self) -> i32 {
self.0.year()
}
#[inline]
pub fn month(&self) -> u32 {
self.0.month()
}
#[inline]
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) -> TypeResult<Self> {
Self::parse(value)
}
}
impl TryFrom<String> for BirthDate {
type Error = TypeError;
fn try_from(value: String) -> TypeResult<Self> {
Self::parse(value)
}
}
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 value = <&str>::deserialize(deserializer)?;
Self::parse(value).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum BirthDateError {
#[error("birth date cannot be in the future")]
FutureDate,
#[error("invalid birth date")]
InvalidDate,
}
#[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\"");
}
}