Skip to main content

resource_timeline/
violation.rs

1//! Explanations of resource bound violations.
2
3#[cfg(feature = "alloc")]
4use crate::storage::Heap;
5use crate::storage::Storage;
6use crate::{ActivityId, Level, Tick};
7
8/// The bound a level broke, with the bound's value.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub enum Bound {
12    /// The level fell below the lower bound (reserve, or no units left to claim).
13    BelowMin(Level),
14    /// The level rose above the upper bound (capacity).
15    AboveMax(Level),
16    /// The level at a [`Target`](crate::Target) tick is below the target's minimum, for
17    /// example the battery reserve required at the end of a sol.
18    BelowTarget(Level),
19}
20
21impl Bound {
22    /// Whether the level is too low ([`BelowMin`](Self::BelowMin),
23    /// [`BelowTarget`](Self::BelowTarget)) rather than too high.
24    #[must_use]
25    pub const fn is_lower(&self) -> bool {
26        matches!(self, Self::BelowMin(_) | Self::BelowTarget(_))
27    }
28
29    /// The value of the broken bound.
30    #[must_use]
31    pub const fn value(&self) -> Level {
32        match *self {
33            Self::BelowMin(value) | Self::AboveMax(value) | Self::BelowTarget(value) => value,
34        }
35    }
36
37    /// Whether a net effect of `effect` pushes the level towards this bound.
38    pub(crate) const fn is_pushed_by(&self, effect: i128) -> bool {
39        if self.is_lower() {
40            effect < 0
41        } else {
42            effect > 0
43        }
44    }
45}
46
47/// The first tick at which a timeline's level is out of bounds, and why.
48///
49/// The contributor list uses the storage `S` of the timeline that reported it, so explaining a
50/// violation of a [`Fixed`](crate::storage::Fixed) timeline does not allocate.
51#[derive(Debug, Clone, PartialEq, Eq, Hash)]
52pub struct Violation<
53    #[cfg(feature = "alloc")] S: Storage = Heap,
54    #[cfg(not(feature = "alloc"))] S: Storage,
55> {
56    /// The earliest tick at which the level is out of bounds.
57    ///
58    /// All impacts at this tick are already applied.
59    pub at: Tick,
60    /// The level at [`at`](Self::at).
61    pub level: Level,
62    /// The bound that is broken.
63    pub bound: Bound,
64    /// The activities whose net effect in force at [`at`](Self::at) pushes the level towards
65    /// the broken bound.
66    ///
67    /// Net negative for [`Bound::BelowMin`] and [`Bound::BelowTarget`], net positive for
68    /// [`Bound::AboveMax`]. Effects are
69    /// netted per activity first. Sorted ascending, without duplicates. Empty when the initial
70    /// level alone breaks the bound.
71    pub contributors: S::List<ActivityId>,
72}