databento_defs/
error.rs

1//! Types for errors that can occur in databento-defs and dependent crates.
2use std::{ffi::NulError, fmt::Display, num::TryFromIntError};
3
4/// Simple error type for failed conversions.
5#[derive(Debug, Clone)]
6pub enum Error {
7    /// Received an unexpected `NULL` back from an FFI function.
8    NullPointer,
9    /// Failed type conversion or casting.
10    TypeConversion(&'static str),
11    /// A file that was expected to exist does not.
12    FileDoesNotExist(String),
13}
14
15pub type Result<T> = std::result::Result<T, Error>;
16
17impl Display for Error {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            Error::NullPointer => write!(f, "Received unexpected NULL from the FFI"),
21            Error::TypeConversion(msg) => write!(f, "Type conversion error: {msg}"),
22            Error::FileDoesNotExist(path) => write!(f, "Path doesn't exist: {path}"),
23        }
24    }
25}
26
27impl std::error::Error for Error {}
28
29impl From<NulError> for Error {
30    fn from(_: NulError) -> Self {
31        Self::TypeConversion("Missing null byte in CString conversion")
32    }
33}
34
35impl From<TryFromIntError> for Error {
36    fn from(_: TryFromIntError) -> Self {
37        Self::TypeConversion("Out of range int conversion")
38    }
39}