zerodds_types/dynamic/
error.rs1use alloc::string::String;
6use core::fmt;
7
8#[derive(Debug, Clone, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum DynamicError {
13 BadParameter(String),
15 PreconditionNotMet(String),
17 IllegalOperation(String),
19 Unsupported(String),
21 Inconsistent(String),
23 BuilderConflict(String),
25 LoanError(String),
27}
28
29impl DynamicError {
30 pub(crate) fn bad_parameter(msg: impl Into<String>) -> Self {
32 Self::BadParameter(msg.into())
33 }
34
35 pub(crate) fn inconsistent(msg: impl Into<String>) -> Self {
37 Self::Inconsistent(msg.into())
38 }
39
40 pub(crate) fn builder(msg: impl Into<String>) -> Self {
42 Self::BuilderConflict(msg.into())
43 }
44
45 pub(crate) fn unsupported(msg: impl Into<String>) -> Self {
47 Self::Unsupported(msg.into())
48 }
49
50 pub(crate) fn loan(msg: impl Into<String>) -> Self {
52 Self::LoanError(msg.into())
53 }
54}
55
56impl fmt::Display for DynamicError {
57 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58 match self {
59 Self::BadParameter(m) => write!(f, "bad parameter: {m}"),
60 Self::PreconditionNotMet(m) => write!(f, "precondition not met: {m}"),
61 Self::IllegalOperation(m) => write!(f, "illegal operation: {m}"),
62 Self::Unsupported(m) => write!(f, "unsupported: {m}"),
63 Self::Inconsistent(m) => write!(f, "inconsistent: {m}"),
64 Self::BuilderConflict(m) => write!(f, "builder conflict: {m}"),
65 Self::LoanError(m) => write!(f, "loan error: {m}"),
66 }
67 }
68}
69
70#[cfg(feature = "std")]
71extern crate std;
72
73#[cfg(feature = "std")]
74impl std::error::Error for DynamicError {}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use alloc::string::ToString;
80
81 #[test]
82 fn dynamic_error_display_emits_helpful_text() {
83 let e = DynamicError::bad_parameter("x");
84 assert!(e.to_string().contains("bad parameter"));
85 assert!(e.to_string().contains('x'));
86 }
87}