1use std::fmt;
2
3pub type DaftResult<T> = Result<T, DaftError>;
5
6#[derive(Debug)]
8pub enum DaftError {
9 TypeError(String),
11 RuntimeError(String),
13}
14
15impl fmt::Display for DaftError {
16 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17 match self {
18 Self::RuntimeError(msg) => write!(f, "RuntimeError: {msg}"),
19 Self::TypeError(msg) => write!(f, "TypeError: {msg}"),
20 }
21 }
22}
23
24impl<E: std::error::Error> From<E> for DaftError {
28 fn from(e: E) -> Self {
29 Self::RuntimeError(e.to_string())
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 #[test]
38 fn type_error_display() {
39 let err = DaftError::TypeError("expected Int32".into());
40 assert_eq!(err.to_string(), "TypeError: expected Int32");
41 }
42
43 #[test]
44 fn runtime_error_display() {
45 let err = DaftError::RuntimeError("division by zero".into());
46 assert_eq!(err.to_string(), "RuntimeError: division by zero");
47 }
48
49 #[test]
50 fn from_std_io_error() {
51 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file missing");
52 let daft_err: DaftError = io_err.into();
53 assert!(daft_err.to_string().contains("file missing"));
54 }
55
56 #[test]
57 fn error_debug_format() {
58 let err = DaftError::TypeError("bad".into());
59 let debug = format!("{err:?}");
60 assert!(debug.contains("TypeError"));
61 assert!(debug.contains("bad"));
62 }
63}