Skip to main content

daft_ext/
error.rs

1use std::fmt;
2
3/// Result type for extension functions.
4pub type DaftResult<T> = Result<T, DaftError>;
5
6/// Errors produced by extension scalar functions.
7#[derive(Debug)]
8pub enum DaftError {
9    /// Errors produced by the extension's type system.
10    TypeError(String),
11    /// Errors produced by the extension's runtime logic.
12    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
24// Blanket impl: any `std::error::Error` can be converted to a `DaftError` via `?`.
25// Note: DaftError intentionally does NOT implement `std::error::Error` to avoid
26// coherence conflicts with this blanket impl (`From<T> for T`).
27impl<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}