rustdv_methodology/
error.rs1use std::fmt;
20
21use rustdv_sim::executor::TaskError;
22use rustdv_sim::{HandleError, ValueError};
23
24use crate::config::ConfigError;
25use crate::sequence::SeqError;
26
27#[derive(Debug, Clone)]
28pub struct TestError {
29 msg: String,
30 kind: Option<&'static str>,
31}
32
33impl TestError {
34 pub fn new(msg: impl Into<String>) -> TestError {
36 TestError { msg: msg.into(), kind: None }
37 }
38
39 pub fn with_kind(msg: impl Into<String>, kind: &'static str) -> TestError {
42 TestError { msg: msg.into(), kind: Some(kind) }
43 }
44
45 pub fn message(&self) -> &str {
46 &self.msg
47 }
48
49 pub fn kind(&self) -> Option<&'static str> {
51 self.kind
52 }
53}
54
55impl fmt::Display for TestError {
56 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "{}", self.msg)
58 }
59}
60impl std::error::Error for TestError {}
61
62impl From<&str> for TestError {
63 fn from(s: &str) -> Self {
64 TestError::new(s)
65 }
66}
67impl From<String> for TestError {
68 fn from(s: String) -> Self {
69 TestError::new(s)
70 }
71}
72impl From<HandleError> for TestError {
73 fn from(e: HandleError) -> Self {
74 TestError::new(e.to_string())
75 }
76}
77impl From<ValueError> for TestError {
78 fn from(e: ValueError) -> Self {
79 TestError::new(e.to_string())
80 }
81}
82impl From<SeqError> for TestError {
83 fn from(e: SeqError) -> Self {
84 TestError::new(e.to_string())
85 }
86}
87impl From<TaskError> for TestError {
88 fn from(e: TaskError) -> Self {
89 TestError::new(e.to_string())
90 }
91}
92
93impl From<ConfigError> for TestError {
96 fn from(e: ConfigError) -> Self {
97 TestError::with_kind(e.to_string(), e.kind())
98 }
99}