verctl-core 0.9.6

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
use std::time::{Duration, UNIX_EPOCH};

use super::{parse, parse_prefix, wait};

fn seconds(text: &str) -> Option<u64> {
    parse(text).map(|when| {
        when.duration_since(UNIX_EPOCH)
            .expect("after the epoch")
            .as_secs()
    })
}

/// RFC 9110's own example, and a date crates.io gave in 2026.
#[test]
fn a_known_date_is_its_unix_seconds() {
    assert_eq!(seconds("Sun, 06 Nov 1994 08:49:37 GMT"), Some(784_111_777));
    assert_eq!(
        seconds("Fri, 13 Nov 2026 15:10:00 GMT"),
        Some(1_794_582_600)
    );
}

/// A leap day counts, and the epoch is day zero.
#[test]
fn leap_days_and_the_epoch() {
    assert_eq!(seconds("Thu, 01 Jan 1970 00:00:00 GMT"), Some(0));
    assert_eq!(
        seconds("Thu, 29 Feb 2024 12:00:00 GMT"),
        Some(1_709_208_000)
    );
    assert_eq!(seconds("Thu, 29 Feb 2023 12:00:00 GMT"), None);
}

#[test]
fn a_bad_weekday_or_month_is_refused() {
    assert_eq!(seconds("Fry, 13 Nov 2026 15:10:00 GMT"), None);
    assert_eq!(seconds("Sat, 13 Nov 2026 15:10:00 GMT"), None);
    assert_eq!(seconds("Fri, 13 Nob 2026 15:10:00 GMT"), None);
    assert_eq!(seconds("Fri, 13 November 2026 15:10:00 GMT"), None);
}

#[test]
fn a_zone_other_than_gmt_is_refused() {
    assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00 UTC"), None);
    assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00 +0000"), None);
    assert_eq!(seconds("Fri, 13 Nov 2026 15:10:00"), None);
}

#[test]
fn another_shape_is_refused() {
    for text in [
        "",
        "Friday, 13-Nov-26 15:10:00 GMT",
        "Fri Nov 13 15:10:00 2026",
        "Fri, 13 Nov 2026 25:10:00 GMT",
        "Fri, 13 Nov 2026 15:10 GMT",
        "Fri, 13 Nov 2026 15:10:00 GMT extra",
    ] {
        assert_eq!(seconds(text), None, "{text:?}");
    }
}

/// Seconds wait as given, and a date in the past is no wait.
#[test]
fn a_retry_after_is_seconds_or_a_date() {
    assert_eq!(wait("0"), Some(Duration::ZERO));
    assert_eq!(wait(" 30 "), Some(Duration::from_secs(30)));
    assert!(wait("Fri, 13 Nov 2026 15:10:00 GMT").is_some());
    assert_eq!(wait("Sun, 06 Nov 1994 08:49:37 GMT"), Some(Duration::ZERO));
    assert_eq!(wait("soon"), None);
    assert_eq!(wait("-5"), None);
}

#[test]
fn parse_prefix_stops_after_imf_fixdate_regardless_of_trailing_prose() {
    let current = parse_prefix(
        "Wed, 02 Dec 2026 15:49:31 GMT and see https://crates.io/docs/rate-limits for more details.",
    );
    assert_eq!(
        current.map(|(when, slice)| (when.duration_since(UNIX_EPOCH).unwrap().as_secs(), slice)),
        Some((1_796_226_571, "Wed, 02 Dec 2026 15:49:31 GMT"))
    );
    let legacy = parse_prefix("Mon, 19 Oct 2026 01:23:45 GMT or email help@crates.io.");
    assert_eq!(
        legacy.map(|(_, slice)| slice),
        Some("Mon, 19 Oct 2026 01:23:45 GMT")
    );
    assert!(parse_prefix("Wed, 02 Dec 2026 15:49:31 GMTX").is_none());
    assert!(parse_prefix("not a date").is_none());
}