ocl_core/
error.rs

1//! Standard error type for ocl.
2//!
3
4use crate::functions::{ApiError, ApiWrapperError, ProgramBuildError, VersionLowError};
5use crate::util::UtilError;
6use crate::{EmptyInfoResultError, Status};
7
8/// Ocl error result type.
9pub type Result<T> = ::std::result::Result<T, Error>;
10
11/// An enum one of several error types.
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14    // String: An arbitrary error:
15    //
16    // TODO: Remove this eventually. We need to replace every usage
17    // (conversion from String/str) with a dedicated error type/variant for
18    // each. In the meanwhile, refrain from creating new instances of this by
19    // converting strings to `Error`!
20    #[error("{0}")]
21    String(String),
22    // FfiNul: Ffi string conversion error:
23    #[error("{0}")]
24    FfiNul(#[from] ::std::ffi::NulError),
25    // Io: std::io error:
26    #[error("{0}")]
27    Io(#[from] ::std::io::Error),
28    // FromUtf8: String conversion error:
29    #[error("{0}")]
30    FromUtf8(#[from] ::std::string::FromUtf8Error),
31    // IntoString: Ffi string conversion error:
32    #[error("{0}")]
33    IntoString(#[from] ::std::ffi::IntoStringError),
34    // EmptyInfoResult:
35    #[error("{0}")]
36    EmptyInfoResult(EmptyInfoResultError),
37    // Util:
38    #[error("{0}")]
39    Util(UtilError),
40    // Api:
41    #[error("{0}")]
42    Api(ApiError),
43    // VersionLow:
44    #[error("{0}")]
45    VersionLow(VersionLowError),
46    // ProgramBuild:
47    #[error("{0}")]
48    ProgramBuild(ProgramBuildError),
49    // ApiWrapper:
50    #[error("{0}")]
51    ApiWrapper(ApiWrapperError),
52}
53
54impl Error {
55    /// Returns the error status code for `Status` variants.
56    pub fn api_status(&self) -> Option<Status> {
57        match *self {
58            Error::Api(ref err) => Some(err.status()),
59            _ => None,
60        }
61    }
62}
63
64// TODO: Remove eventually
65impl<'a> From<&'a str> for Error {
66    fn from(desc: &'a str) -> Self {
67        Error::String(String::from(desc))
68    }
69}
70
71// TODO: Remove eventually
72impl From<String> for Error {
73    fn from(desc: String) -> Self {
74        Error::String(desc)
75    }
76}
77
78impl From<UtilError> for Error {
79    fn from(err: UtilError) -> Self {
80        Error::Util(err)
81    }
82}
83
84impl From<ApiError> for Error {
85    fn from(err: ApiError) -> Self {
86        Error::Api(err)
87    }
88}
89
90impl From<VersionLowError> for Error {
91    fn from(err: VersionLowError) -> Self {
92        Error::VersionLow(err)
93    }
94}
95
96impl From<ProgramBuildError> for Error {
97    fn from(err: ProgramBuildError) -> Self {
98        Error::ProgramBuild(err)
99    }
100}
101
102impl From<ApiWrapperError> for Error {
103    fn from(err: ApiWrapperError) -> Self {
104        Error::ApiWrapper(err)
105    }
106}