yz-diary-date 0.1.2

parser for my personal diary directory structure, and a simple formatter
Documentation
// Copyright (c) 2021-2024 Alain Emilia Anna Zscheile
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{fmt, NaiveDate};
use proptest::prelude::*;

#[test]
fn standard() {
    use crate::parse_from_path;
    use std::path::Path;
    assert_eq!(parse_from_path(Path::new("201/01_01")), None);
    assert_eq!(parse_from_path(Path::new("2016/0")), None);
    assert_eq!(
        parse_from_path(Path::new("2016/08_28")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("2016/08-28")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("2016/08-28ä")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("teller/2016/08_28")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("teller/2016/08_28nox/fluppig.jpg")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("/blog/2017/1124y_vf.html")),
        NaiveDate::from_ymd_opt(2017, 11, 24)
    );
}

#[cfg(feature = "camino")]
#[test]
fn utf8path() {
    use crate::parse_from_utf8path as parse_from_path;
    use camino::Utf8Path as Path;
    assert_eq!(parse_from_path(Path::new("201/01_01")), None);
    assert_eq!(parse_from_path(Path::new("2016/0")), None);
    assert_eq!(
        parse_from_path(Path::new("2016/08_28")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("2016/08-28")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("2016/08-28ä")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("teller/2016/08_28")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("teller/2016/08_28nox/fluppig.jpg")),
        NaiveDate::from_ymd_opt(2016, 08, 28)
    );
    assert_eq!(
        parse_from_path(Path::new("/blog/2017/1124y_vf.html")),
        NaiveDate::from_ymd_opt(2017, 11, 24)
    );
}

#[test]
fn tfmt() {
    assert_eq!(
        fmt(&NaiveDate::from_ymd_opt(2016, 08, 28).unwrap(), None),
        "2016/0828"
    );
    assert_eq!(
        fmt(&NaiveDate::from_ymd_opt(2016, 08, 28).unwrap(), Some('_')),
        "2016/08_28"
    );
    assert_eq!(
        fmt(&NaiveDate::from_ymd_opt(2016, 08, 28).unwrap(), Some('-')),
        "2016/08-28"
    );
    assert_eq!(
        fmt(&NaiveDate::from_ymd_opt(2017, 11, 24).unwrap(), None),
        "2017/1124"
    );
    assert_eq!(
        fmt(&NaiveDate::from_ymd_opt(2017, 11, 24).unwrap(), Some('_')),
        "2017/11_24"
    );
    assert_eq!(
        fmt(&NaiveDate::from_ymd_opt(2017, 11, 24).unwrap(), Some('-')),
        "2017/11-24"
    );
}

#[test]
#[should_panic]
fn tfmt_fail() {
    fmt(&NaiveDate::from_ymd_opt(2016, 08, 28).unwrap(), Some('*'));
}

proptest! {
    #[test]
    fn doesnt_crash_intern(par in "\\PC*", fin in "\\PC*") {
        let _ = super::parse(&par, &fin);
    }

    #[test]
    fn doesnt_crash(s in "\\PC*") {
        let _ = super::parse_from_path(std::path::Path::new(&s));
    }

    #[test]
    #[cfg(feature = "camino")]
    fn doesnt_crash_camino(s in "\\PC*") {
        let _ = super::parse_from_utf8path(camino::Utf8Path::new(&s));
    }

    #[test]
    fn all_valid_dates02(par in "[1-9][0-9]{3}", fin in "02(-|_)?(0[1-9]|1[0-9]|2[0-8])\\PC*") {
        super::parse(&par, &fin).unwrap();
    }

    #[test]
    fn all_valid_dates(par in "[1-9][0-9]{3}", fin in "(0[13-9]|1[0-2])(-|_)?(0[1-9]|[12][0-9]|30)\\PC*") {
        super::parse(&par, &fin).unwrap();
    }
}