Skip to main content

qubit_clock/mock/
mock_time_error.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Errors returned by mock time controls.
11
12use std::error::Error;
13use std::fmt::{
14    Display,
15    Formatter,
16};
17
18/// Error returned when a mock time control operation is rejected.
19///
20/// Mock time controls are usually infallible because tests advance time
21/// explicitly. Resetting elapsed time while a sleeper or deadline waiter is
22/// active would rewind the timeline underneath a blocked operation. Passing an
23/// instant from one timeline into another timeline would make a deadline refer
24/// to the wrong logical time source. Both cases are rejected instead of
25/// silently producing inconsistent deadline behavior.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum MockTimeError {
28    /// The operation would reset a timeline while waiters are still registered.
29    ActiveWaiters,
30    /// The operation received an instant created by a different timeline.
31    MismatchedTimeline {
32        /// Timeline id expected by the operation.
33        expected: u64,
34        /// Timeline id carried by the provided instant.
35        actual: u64,
36    },
37}
38
39impl Display for MockTimeError {
40    /// Formats the mock time error.
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        match self {
43            Self::ActiveWaiters => write!(f, "mock timeline has active waiters"),
44            Self::MismatchedTimeline { expected, actual } => write!(
45                f,
46                "mock instant belongs to timeline {actual}, but timeline {expected} was expected",
47            ),
48        }
49    }
50}
51
52impl Error for MockTimeError {}