x11_dl/
error.rs

1// x11-rs: Rust bindings for X11 libraries
2// The X11 libraries are available under the MIT license.
3// These bindings are public domain.
4
5use std::error::Error;
6use std::fmt::{Display, Formatter};
7
8//
9// OpenError
10
11//
12
13#[derive(Clone, Debug)]
14pub struct OpenError {
15    kind: OpenErrorKind,
16    detail: String,
17}
18
19impl OpenError {
20    pub fn detail(&self) -> &str {
21        self.detail.as_ref()
22    }
23
24    pub fn kind(&self) -> OpenErrorKind {
25        self.kind
26    }
27
28    pub fn new(kind: OpenErrorKind, detail: String) -> OpenError {
29        OpenError { kind, detail }
30    }
31}
32
33impl Display for OpenError {
34    fn fmt(&self, f: &mut Formatter) -> Result<(), ::std::fmt::Error> {
35        //try!(f.write_str(self.kind.as_str())); TEST Erle July 2020 (on dev branch)
36        f.write_str(self.kind.as_str())?;
37        if !self.detail.is_empty() {
38            f.write_str(" (")?;
39            f.write_str(self.detail.as_ref())?;
40            f.write_str(")")?;
41        }
42        Ok(())
43    }
44}
45
46impl Error for OpenError {
47    fn description(&self) -> &str {
48        self.kind.as_str()
49    }
50}
51
52//
53// OpenErrorKind
54//
55
56#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
57pub enum OpenErrorKind {
58    Library,
59    Symbol,
60}
61
62impl OpenErrorKind {
63    pub fn as_str(self) -> &'static str {
64        match self {
65            OpenErrorKind::Library => "opening library failed",
66            OpenErrorKind::Symbol => "loading symbol failed",
67        }
68    }
69}