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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//! Defines [`Error`], representing all errors returned by this crate.
use std::fmt::{Debug, Display, Formatter};

/// Enum with all errors in this crate.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Returned when functionality is not yet available.
    NotYetImplemented(String),
    /// Wrapper for an error triggered by a dependency
    External(String, Box<dyn std::error::Error + Send + Sync>),
    /// Wrapper for IO errors
    Io(std::io::Error),
    /// When an invalid argument is passed to a function.
    InvalidArgumentError(String),
    /// Error during import or export to/from a format
    ExternalFormat(String),
    /// Whenever pushing to a container fails because it does not support more entries.
    /// The solution is usually to use a higher-capacity container-backing type.
    Overflow,
    /// Whenever incoming data from the C data interface, IPC or Flight does not fulfil the Arrow specification.
    OutOfSpec(String),
}

impl Error {
    /// Wraps an external error in an `Error`.
    pub fn from_external_error(error: impl std::error::Error + Send + Sync + 'static) -> Self {
        Self::External("".to_string(), Box::new(error))
    }

    pub(crate) fn oos<A: Into<String>>(msg: A) -> Self {
        Self::OutOfSpec(msg.into())
    }

    #[allow(dead_code)]
    pub(crate) fn nyi<A: Into<String>>(msg: A) -> Self {
        Self::NotYetImplemented(msg.into())
    }
}

impl From<::std::io::Error> for Error {
    fn from(error: std::io::Error) -> Self {
        Error::Io(error)
    }
}

impl From<std::str::Utf8Error> for Error {
    fn from(error: std::str::Utf8Error) -> Self {
        Error::External("".to_string(), Box::new(error))
    }
}

impl From<std::string::FromUtf8Error> for Error {
    fn from(error: std::string::FromUtf8Error) -> Self {
        Error::External("".to_string(), Box::new(error))
    }
}

impl From<simdutf8::basic::Utf8Error> for Error {
    fn from(error: simdutf8::basic::Utf8Error) -> Self {
        Error::External("".to_string(), Box::new(error))
    }
}

impl From<std::collections::TryReserveError> for Error {
    fn from(_: std::collections::TryReserveError) -> Error {
        Error::Overflow
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::NotYetImplemented(source) => {
                write!(f, "Not yet implemented: {}", &source)
            }
            Error::External(message, source) => {
                write!(f, "External error{}: {}", message, &source)
            }
            Error::Io(desc) => write!(f, "Io error: {}", desc),
            Error::InvalidArgumentError(desc) => {
                write!(f, "Invalid argument error: {}", desc)
            }
            Error::ExternalFormat(desc) => {
                write!(f, "External format error: {}", desc)
            }
            Error::Overflow => {
                write!(f, "Operation overflew the backing container.")
            }
            Error::OutOfSpec(message) => {
                write!(f, "{}", message)
            }
        }
    }
}

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

/// Typedef for a [`std::result::Result`] of an [`Error`].
pub type Result<T> = std::result::Result<T, Error>;