Skip to main content

t2t_clock/
errors.rs

1//! Why the CPU's counter cannot be read as time.
2
3use derive_more::{Display, Error};
4
5/// Why [`Counter::discover`](crate::Counter::discover) found no counter to read as time.
6///
7/// # Examples
8/// ```
9/// use t2t_clock::CounterError;
10///
11/// let refusal = CounterError::ImplausibleRate { hertz: 42 };
12/// assert_eq!(
13///     refusal.to_string(),
14///     "counter error: a rate of 42 Hz is outside 1 MHz to 10 GHz",
15///     "it names the rate it refused"
16/// );
17/// ```
18#[derive(Debug, Display, Error, Clone, Copy, PartialEq, Eq, Hash)]
19#[non_exhaustive]
20pub enum CounterError {
21    /// The time-stamp counter's rate follows the core's, so its ticks have no fixed length.
22    #[display("counter error: the time-stamp counter's rate follows the core's")]
23    NotInvariant,
24    /// The CPU reports no rate, and none could be measured.
25    #[display("counter error: the CPU reports no rate, and none could be measured")]
26    NoRate,
27    /// The rate is outside what any counter runs at.
28    #[display("counter error: a rate of {hertz} Hz is outside 1 MHz to 10 GHz")]
29    ImplausibleRate {
30        /// The rate the CPU reported, or that was measured, in ticks a second.
31        hertz: u64,
32    },
33}
34
35#[cfg(test)]
36mod tests {
37    use alloc::string::{String, ToString as _};
38
39    use rstest::rstest;
40
41    use crate::CounterError;
42
43    #[rstest]
44    #[case::not_invariant(
45        CounterError::NotInvariant.to_string(),
46        "counter error: the time-stamp counter's rate follows the core's"
47    )]
48    #[case::no_rate(
49        CounterError::NoRate.to_string(),
50        "counter error: the CPU reports no rate, and none could be measured"
51    )]
52    #[case::implausible_rate(
53        CounterError::ImplausibleRate { hertz: 42 }.to_string(),
54        "counter error: a rate of 42 Hz is outside 1 MHz to 10 GHz"
55    )]
56    fn each_refusal_says_why(#[case] message: String, #[case] text: &str) {
57        assert_eq!(message, text, "the message names what went wrong");
58    }
59}