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
include!("./error.inc.rs");

use std::ffi::CStr;
use std::fmt::{Display, Formatter};
use uv::{uv_err_name, uv_strerror};

impl Error {
    /// The name of the error.
    pub fn name(&self) -> String {
        unsafe {
            CStr::from_ptr(uv_err_name(self.code() as _))
                .to_string_lossy()
                .into_owned()
        }
    }

    /// A message for the error.
    pub fn message(&self) -> String {
        unsafe {
            CStr::from_ptr(uv_strerror(self.code() as _))
                .to_string_lossy()
                .into_owned()
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.name(), self.message())
    }
}

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

#[derive(Clone, Copy, Debug)]
pub struct ConversionError {
    from: crate::HandleType,
    to: crate::HandleType,
}

impl ConversionError {
    pub(crate) fn new(from: crate::HandleType, to: crate::HandleType) -> ConversionError {
        ConversionError { from, to }
    }
}

impl Display for ConversionError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "Cannot convert {} to {}", self.from, self.to)
    }
}

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