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
use std::error::Error as StdError;
use std::fmt;
use foreign_types::ForeignType;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Error {
    ObjectCreationError,
    MissingData,
    InvalidString,
}

impl Error {
    pub(crate) unsafe fn if_null<T>(handle: *mut <T as ForeignType>::CType) -> LCMSResult<T> where T: ForeignType {
        if !handle.is_null() {
            Ok(T::from_ptr(handle))
        } else {
            Err(Error::ObjectCreationError)
        }
    }
}

/// This is a regular `Result` type with LCMS-specific `Error`
pub type LCMSResult<T> = Result<T, Error>;

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.description())
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::ObjectCreationError => "Could not create the object.\nThe reason is not known, but it's usually caused by wrong input parameters.",
            Error::InvalidString => "String is not valid. Contains unsupported characters or is too long.",
            Error::MissingData => "Requested data is empty or does not exist.",
        }
    }
}