1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Types for errors that can occur in databento-defs and dependent crates.
use std::{ffi::NulError, fmt::Display, num::TryFromIntError};

/// Simple error type for failed conversions.
#[derive(Debug, Clone)]
pub enum Error {
    /// Received an unexpected `NULL` back from an FFI function.
    NullPointer,
    /// Failed type conversion or casting.
    TypeConversion(&'static str),
    /// A file that was expected to exist does not.
    FileDoesNotExist(String),
}

pub type Result<T> = std::result::Result<T, Error>;

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::NullPointer => write!(f, "Received unexpected NULL from the FFI"),
            Error::TypeConversion(msg) => write!(f, "Type conversion error: {msg}"),
            Error::FileDoesNotExist(path) => write!(f, "Path doesn't exist: {path}"),
        }
    }
}

impl std::error::Error for Error {}

impl From<NulError> for Error {
    fn from(_: NulError) -> Self {
        Self::TypeConversion("Missing null byte in CString conversion")
    }
}

impl From<TryFromIntError> for Error {
    fn from(_: TryFromIntError) -> Self {
        Self::TypeConversion("Out of range int conversion")
    }
}