Skip to main content

odem_rs_util/
error.rs

1//! A module for util-specific error types.
2
3use core::fmt;
4
5/* ************************************************************** Error Types */
6
7/// Enumeration of errors for the functions and structures contained in this
8/// module of the simulation library.
9#[derive(Copy, Clone, Debug, PartialEq, Eq, thiserror::Error)]
10pub enum Error {
11	/// Signals an attempt to tabulate a value at a time point that is in the
12	/// past of the previous tabulation.
13	#[error("attempted tabulation of a value in the past of the previous tabulation")]
14	Causality,
15	/// Signals an attempt to tabulate a value with negative weight.
16	#[error("attempted tabulation of a value with negative weight")]
17	NegativeWeight,
18	/// Signals that the storage space of a pool has been exhaused.
19	#[error("insufficient space in storage")]
20	InsufficientSpace,
21}
22
23/* ******************************************************** Minor Error Types */
24
25/// Indicates that an attempt to tabulate a value at a time point that is in the
26/// past of the previous tabulation has been made.
27#[derive(Debug, PartialEq, Eq, thiserror::Error)]
28#[error(
29	"tabulated value at model-time {cause:?} (cause) happens before \
30         model-time {effect:?} (effect), violating causality"
31)]
32pub struct Causality<T> {
33	/// Prior tabulated model-time.
34	pub cause: T,
35	/// Target tabulated model-time.
36	pub effect: T,
37}
38
39impl<T> From<Causality<T>> for Error {
40	#[inline]
41	fn from(_: Causality<T>) -> Self {
42		Error::Causality
43	}
44}
45
46/// Indicates that an attempt to tabulate a value with negative weight has been
47/// made.
48#[derive(Debug, PartialEq, Eq, thiserror::Error)]
49#[error("tabulated value has negative weight {weight:?}")]
50pub struct NegativeWeight<W> {
51	/// The associated (negative) weight for the tabulated value.
52	pub weight: W,
53}
54
55impl<W> From<NegativeWeight<W>> for Error {
56	#[inline]
57	fn from(_: NegativeWeight<W>) -> Self {
58		Error::NegativeWeight
59	}
60}
61
62/// Indicates that the storage space of a pool has been exhaused.
63#[derive(PartialEq, Eq, thiserror::Error)]
64#[error("insufficient space in storage")]
65pub struct InsufficientSpace<B>(pub B);
66
67impl<B> fmt::Debug for InsufficientSpace<B> {
68	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69		f.write_str("Insufficient space in storage")
70	}
71}
72
73impl<B> From<InsufficientSpace<B>> for Error {
74	#[inline]
75	fn from(_: InsufficientSpace<B>) -> Self {
76		Error::InsufficientSpace
77	}
78}