use std::fmt;
use rustdv_sim::executor::TaskError;
use rustdv_sim::{HandleError, ValueError};
use crate::config::ConfigError;
use crate::sequence::SeqError;
#[derive(Debug, Clone)]
pub struct TestError {
msg: String,
kind: Option<&'static str>,
}
impl TestError {
pub fn new(msg: impl Into<String>) -> TestError {
TestError { msg: msg.into(), kind: None }
}
pub fn with_kind(msg: impl Into<String>, kind: &'static str) -> TestError {
TestError { msg: msg.into(), kind: Some(kind) }
}
pub fn message(&self) -> &str {
&self.msg
}
pub fn kind(&self) -> Option<&'static str> {
self.kind
}
}
impl fmt::Display for TestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
impl std::error::Error for TestError {}
impl From<&str> for TestError {
fn from(s: &str) -> Self {
TestError::new(s)
}
}
impl From<String> for TestError {
fn from(s: String) -> Self {
TestError::new(s)
}
}
impl From<HandleError> for TestError {
fn from(e: HandleError) -> Self {
TestError::new(e.to_string())
}
}
impl From<ValueError> for TestError {
fn from(e: ValueError) -> Self {
TestError::new(e.to_string())
}
}
impl From<SeqError> for TestError {
fn from(e: SeqError) -> Self {
TestError::new(e.to_string())
}
}
impl From<TaskError> for TestError {
fn from(e: TaskError) -> Self {
TestError::new(e.to_string())
}
}
impl From<ConfigError> for TestError {
fn from(e: ConfigError) -> Self {
TestError::with_kind(e.to_string(), e.kind())
}
}