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
/// An error type representing the failure to load libwayland
#[derive(Debug)]
pub struct NoWaylandLib;

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

impl std::fmt::Display for NoWaylandLib {
    #[cfg_attr(coverage, coverage(off))]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        f.write_str("could not load libwayland-client.so")
    }
}

/// An error that can occur when using a Wayland connection
#[derive(Debug)]
pub enum WaylandError {
    /// The connection encountered an IO error
    Io(std::io::Error),
    /// The connection encountered a protocol error
    Protocol(crate::protocol::ProtocolError),
}

impl std::error::Error for WaylandError {
    #[cfg_attr(coverage, coverage(off))]
    fn cause(&self) -> Option<&dyn std::error::Error> {
        match self {
            Self::Io(e) => Some(e),
            Self::Protocol(e) => Some(e),
        }
    }
}

impl std::fmt::Display for WaylandError {
    #[cfg_attr(coverage, coverage(off))]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        match self {
            Self::Io(e) => write!(f, "Io error: {}", e),
            Self::Protocol(e) => std::fmt::Display::fmt(e, f),
        }
    }
}

impl Clone for WaylandError {
    #[cfg_attr(coverage, coverage(off))]
    fn clone(&self) -> Self {
        match self {
            Self::Protocol(e) => Self::Protocol(e.clone()),
            Self::Io(e) => {
                if let Some(code) = e.raw_os_error() {
                    Self::Io(std::io::Error::from_raw_os_error(code))
                } else {
                    Self::Io(std::io::Error::new(e.kind(), ""))
                }
            }
        }
    }
}

impl From<crate::protocol::ProtocolError> for WaylandError {
    #[cfg_attr(coverage, coverage(off))]
    fn from(err: crate::protocol::ProtocolError) -> Self {
        Self::Protocol(err)
    }
}

impl From<std::io::Error> for WaylandError {
    #[cfg_attr(coverage, coverage(off))]
    fn from(err: std::io::Error) -> Self {
        Self::Io(err)
    }
}

/// An error generated when trying to act on an invalid `ObjectId`.
#[derive(Clone, Debug)]
pub struct InvalidId;

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

impl std::fmt::Display for InvalidId {
    #[cfg_attr(coverage, coverage(off))]
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        write!(f, "Invalid ObjectId")
    }
}