use derive_more::{Display, Error};
#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[display(
"parse timedelta error: expected `0`, or counts with units from coarsest to finest, as `1m30s`"
)]
pub struct ParseTimedeltaError;
#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[display(
"parse timestamp error: expected RFC 3339 in UTC from 1677 to 2262, as `2026-09-16T07:45:35Z`"
)]
pub struct ParseTimestampError;
#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[display(
"parse TAI timestamp error: expected an RFC 3339 date and time in TAI from 1677 to 2262, as \
`2026-09-16T07:46:12 TAI`"
)]
pub struct ParseTaiTimestampError;
#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[display("parse tickdelta error: expected a count of ticks, as `24 ticks`")]
pub struct ParseTickdeltaError;
#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[display("parse tick rate error: expected a count of hertz above zero, as `24000000 Hz`")]
pub struct ParseTickRateError;
#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
#[display("out of range error: the time is outside what the target type holds")]
pub struct OutOfRangeError;
#[inline]
pub(crate) fn narrow<T, N: TryInto<T>>(count: N) -> Result<T, OutOfRangeError> {
count.try_into().map_err(|_past_the_range| OutOfRangeError)
}
#[cfg(test)]
mod tests {
use alloc::string::{String, ToString as _};
use rstest::rstest;
use crate::{
OutOfRangeError, ParseTaiTimestampError, ParseTickRateError, ParseTickdeltaError,
ParseTimedeltaError, ParseTimestampError,
};
#[rstest]
#[case::a_span(
ParseTimedeltaError.to_string(),
"parse timedelta error: expected `0`, or counts with units from coarsest to finest, as `1m30s`"
)]
#[case::an_instant(
ParseTimestampError.to_string(),
"parse timestamp error: expected RFC 3339 in UTC from 1677 to 2262, as `2026-09-16T07:45:35Z`"
)]
#[case::a_tai_instant(
ParseTaiTimestampError.to_string(),
"parse TAI timestamp error: expected an RFC 3339 date and time in TAI from 1677 to 2262, \
as `2026-09-16T07:46:12 TAI`"
)]
#[case::a_count_of_ticks(
ParseTickdeltaError.to_string(),
"parse tickdelta error: expected a count of ticks, as `24 ticks`"
)]
#[case::a_rate(
ParseTickRateError.to_string(),
"parse tick rate error: expected a count of hertz above zero, as `24000000 Hz`"
)]
#[case::a_conversion(
OutOfRangeError.to_string(),
"out of range error: the time is outside what the target type holds"
)]
fn each_refusal_says_why(#[case] message: String, #[case] text: &str) {
assert_eq!(message, text, "the message names what would have been read");
}
}