php-dates 0.2.0

PHP date() patterns for Rust: strict createFromFormat-style parsing, a pattern resolution contract and a Day.js export, with no framework dependency
Documentation
//! The shared PHP/Day.js date contract.

use php_dates::{
    DateConfig as Config,
    formats::{self as date_formats, Kind, Purpose},
};
use serde_json::json;

fn configured(change: impl FnOnce(&mut Config)) -> Config {
    let mut config = Config::default();
    change(&mut config);
    config
}

#[test]
fn defaults_match_the_php_package() {
    let config = Config::default();
    assert_eq!(date_formats::format(&config, Kind::Date, Purpose::Display, None).unwrap(), "d/m/Y");
    assert_eq!(date_formats::format(&config, Kind::DateTime, Purpose::Display, None).unwrap(), "d/m/Y h:i a");
    assert_eq!(date_formats::format(&config, Kind::Month, Purpose::Filter, None).unwrap(), "m/Y");
    // A cast falls back to its own historical pattern.
    assert_eq!(date_formats::format(&config, Kind::Date, Purpose::Value, Some("d/m/y")).unwrap(), "d/m/y");
}

#[test]
fn a_purpose_falls_back_to_the_top_level_then_to_the_legacy_app_settings() {
    let config = configured(|c| {
        c.date = Some("m/d/Y".into());
        c.filter.date = Some("Y-m-d".into());
        c.date_format = Some("d-m-Y H:i".into());
    });
    assert_eq!(date_formats::format(&config, Kind::Date, Purpose::Display, None).unwrap(), "m/d/Y");
    assert_eq!(date_formats::format(&config, Kind::Date, Purpose::Value, None).unwrap(), "m/d/Y");
    assert_eq!(date_formats::format(&config, Kind::Date, Purpose::Filter, None).unwrap(), "Y-m-d");
    assert_eq!(date_formats::format(&config, Kind::DateTime, Purpose::Display, None).unwrap(), "d-m-Y H:i");
    let legacy = configured(|c| c.date_format_solo = Some("j/n/Y".into()));
    assert_eq!(date_formats::format(&legacy, Kind::Date, Purpose::Filter, None).unwrap(), "j/n/Y");
}

#[test]
fn empty_patterns_are_rejected() {
    let config = configured(|c| c.date = Some("  ".into()));
    assert!(date_formats::format(&config, Kind::Date, Purpose::Display, None).is_err());
}

#[test]
fn the_frontend_config_translates_php_tokens_to_dayjs() {
    let config = configured(|c| {
        c.date = Some("d/m/Y".into());
        c.date_time = Some("m/d/Y h:i A".into());
        c.value.date = Some("Y-m-d".into());
        c.value.date_time = Some("Y-m-d H:i:s".into());
        c.filter.date = Some("Y-m-d".into());
    });
    assert_eq!(
        date_formats::frontend_config(&config).unwrap(),
        json!({
            "date": "DD/MM/YYYY", "dateTime": "MM/DD/YYYY hh:mm A", "month": "MM/YYYY",
            "value": {"date": "YYYY-MM-DD", "dateTime": "YYYY-MM-DD HH:mm:ss", "month": "MM/YYYY"},
            "filter": {"date": "YYYY-MM-DD", "dateTime": "MM/DD/YYYY hh:mm A", "month": "MM/YYYY"},
        })
    );
}

#[test]
fn the_value_formats_keep_the_casts_historical_fallbacks() {
    let frontend = date_formats::frontend_config(&Config::default()).unwrap();
    assert_eq!(frontend["value"]["date"], "DD/MM/YY");
    assert_eq!(frontend["value"]["dateTime"], "DD/MM/YY hh:mm A");
}

#[test]
fn dayjs_export_translates_what_day_js_can_express() {
    assert_eq!(date_formats::to_dayjs(r"d \d\e m").unwrap(), "DD [d][e] MM");
    assert_eq!(date_formats::to_dayjs("j-n-y G:i:s a").unwrap(), "D-M-YY H:mm:ss a");
    assert_eq!(date_formats::to_dayjs("D, d M Y H:i:s O").unwrap(), "ddd, DD MMM YYYY HH:mm:ss ZZ");
    assert_eq!(date_formats::to_dayjs("l, F j, Y g:i A P").unwrap(), "dddd, MMMM D, YYYY h:mm A Z");
    assert_eq!(date_formats::to_dayjs("Y-m-d H:i:s.v").unwrap(), "YYYY-MM-DD HH:mm:ss.SSS");
}

#[test]
fn dayjs_export_rejects_tokens_day_js_cannot_express() {
    // `B` Swatch beats, `t` days in the month, `S` suffix alone, `z` is zero based, and brackets mean something.
    for pattern in ["B", "t", "S", "z", "L", "d [m]", "d\\"] {
        assert!(date_formats::to_dayjs(pattern).is_err(), "{pattern}");
    }
}