breadx 3.1.0

Pure-Rust X11 connection implementation with a focus on adaptability
Documentation
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//               Copyright John Nunley, 2022.
// Distributed under the Boost Software License, Version 1.0.
//       (See accompanying file LICENSE or copy at
//         https://www.boost.org/LICENSE_1_0.txt)

use core::any::type_name;
use core::fmt;
use core::str;

use alloc::string::String;
use alloc::string::ToString;
use x11rb_protocol::x11_utils::X11Error;
use x11rb_protocol::{
    errors::{ConnectError, ParseError},
    protocol::xproto::{SetupAuthenticate, SetupFailed},
};

cfg_std! {
    use std::error::Error as StdError;
    use std::io::Error as IoError;
    use std::io::ErrorKind;
}

cfg_std_unix! {
    use nix::errno::Errno;
}

/// An error that may occur during operation of this crate.
pub struct Error {
    /// The inner details of the error.
    inner: Inner,
    /// Whether or not this error occurred during connection
    /// initialization.
    initialization: bool,
}

/// The innards of the [`Error`] struct.
///
/// Making this type not public allows us to change it without it being
/// a breaking change.
enum Inner {
    /// We tried to run an operation that is not supported.
    #[allow(dead_code)]
    Unsupported(Unsupported),
    /// A custom message.
    Message(String),
    /// We did something that has put the X11 connection into an invalid state.
    ///
    /// This is a signal to the connection to halt all operations.
    InvalidState(InvalidState),
    /// We could not parse the display string.
    CouldntParseDisplay {
        /// True if we got the display string from an environment
        /// variable, false otherwise.
        from_env: bool,
    },
    /// This type was poisoned.
    Poisoned {
        /// The name of the involved type.
        type_name: &'static str,
    },
    /// We could not parse the given type.
    ParseError(ParseError),
    /// A failure occurred during the setup process.
    SetupFailed(SetupFailure),
    /// An X11 error occurred.
    X11Error(X11Error),
    /// The extension is missing.
    MissingExtension {
        /// The name of the extension.
        name: &'static str,
    },
    /// Request was too large to be sent.
    RequestTooLarge {
        /// Length of the request.
        x_len: usize,
        /// Maximum request length.
        max_len: usize,
    },
    /// Attempted to send a request while another was in progress.
    #[cfg(feature = "async")]
    AsyncSendInProgress {
        /// The sequence number of the request that was in progress.
        seq: u64,
    },
    /// An I/O error occurred.
    #[cfg(feature = "std")]
    Io(IoError),
}

#[allow(dead_code)]
#[derive(Clone, Copy)]
pub(crate) enum Unsupported {
    Fds,
    Socket,
}

/// Reason why something entered an invalid state.
///
/// Keeping this crate private means that we can add more in the future
/// if we want to.
#[derive(Clone, Copy)]
pub(crate) enum InvalidState {
    UnexpectedFds,
    NotEnoughSetup,
    ScreenOutOfRange,
    ZeroIdMask,
    BadError,
    XidsExhausted,
}

/// The setup for the connection failed.
#[derive(Clone)]
pub enum SetupFailure {
    Failed(SetupFailed),
    Authenticate(SetupAuthenticate),
}

// crate-private API
impl Error {
    fn from_inner(inner: Inner) -> Self {
        Self {
            inner,
            initialization: false,
        }
    }

    pub(crate) fn make_invalid_state(is: InvalidState) -> Self {
        Error::from_inner(Inner::InvalidState(is))
    }

    #[allow(dead_code)]
    pub(crate) fn make_unsupported(us: Unsupported) -> Self {
        Error::from_inner(Inner::Unsupported(us))
    }

    pub(crate) fn couldnt_parse_display(from_env: bool) -> Self {
        Error::from_inner(Inner::CouldntParseDisplay { from_env })
    }

    pub(crate) fn make_poisoned<T>() -> Self {
        Error::from_inner(Inner::Poisoned {
            type_name: type_name::<T>(),
        })
    }

    pub(crate) fn make_large_request(x_len: usize, max_len: usize) -> Self {
        Error::from_inner(Inner::RequestTooLarge { x_len, max_len })
    }

    pub(crate) fn make_setup_failure(sf: SetupFailure) -> Self {
        Error::from_inner(Inner::SetupFailed(sf))
    }

    pub(crate) fn make_connect_error(ce: ConnectError) -> Self {
        // convert the error to one of our other variants
        let mut err = match ce {
            ConnectError::ParseError(pe) => Error::make_parse_error(pe),
            #[cfg(feature = "std")]
            ConnectError::IoError(io) => Error::io(io),
            ConnectError::InvalidScreen => {
                Error::make_invalid_state(InvalidState::ScreenOutOfRange)
            }
            ConnectError::ZeroIdMask => Error::make_invalid_state(InvalidState::ZeroIdMask),
            ConnectError::SetupFailed(sf) => Error::make_setup_failure(SetupFailure::Failed(sf)),
            ConnectError::SetupAuthenticate(sa) => {
                Error::make_setup_failure(SetupFailure::Authenticate(sa))
            }
            ConnectError::DisplayParsingError => Error::couldnt_parse_display(false),
            ConnectError::Incomplete { .. } => {
                Error::make_invalid_state(InvalidState::NotEnoughSetup)
            }
            _ => unreachable!(),
        };

        err.initialization = true;
        err
    }

    /// Tell if this is a would-block I/O error.
    pub(crate) fn would_block(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "std")] {
                match self.as_io_error() {
                    Some(io) => io.kind() == ErrorKind::WouldBlock,
                    None => false,
                }
            } else {
                false
            }
        }
    }

    #[allow(dead_code)]
    pub(crate) fn is_protocol_error(&self) -> bool {
        matches!(
            self.inner,
            Inner::X11Error(..) | Inner::MissingExtension { .. }
        )
    }
}

cfg_std! {
    impl Error {
        pub(crate) fn io(io: IoError) -> Self {
            if !matches!(io.kind(), ErrorKind::WouldBlock) {
                tracing::error!("encountered I/O error: {io:?}", io = io);
            }
            Error::from_inner(Inner::Io(io))
        }
    }
}

// public API
impl Error {
    /// Create a new error from something that can be formatted into
    /// a message.
    #[must_use]
    pub fn make_msg<D: fmt::Display>(msg: D) -> Self {
        Self::from_inner(Inner::Message(msg.to_string()))
    }

    /// Did this error happen as the result of calling an
    /// unsupported operation?
    #[must_use]
    pub fn unsupported(&self) -> bool {
        matches!(self.inner, Inner::Unsupported(_))
    }

    /// Create an error from an X11 parse error.
    #[must_use]
    pub fn make_parse_error(pe: ParseError) -> Self {
        Error::from_inner(Inner::ParseError(pe))
    }

    /// Create an error from a missing extension.
    #[must_use]
    pub fn make_missing_extension(name: &'static str) -> Self {
        Error::from_inner(Inner::MissingExtension { name })
    }

    /// Did this error happen as a result of some state-based
    /// object entering an invalid state?
    #[must_use]
    pub fn invalid_state(&self) -> bool {
        #[allow(unused_mut)]
        let mut base = matches!(self.inner, Inner::InvalidState(_) | Inner::Poisoned { .. });

        cfg_if::cfg_if! {
            if #[cfg(feature = "std")] {
                base |= matches!(self.inner, Inner::Io(ref err) if err.kind() != ErrorKind::WouldBlock);
            }
        }

        base
    }

    /// Did this error occur during initialization of the X11
    /// connection?
    #[must_use]
    pub fn initialization(&self) -> bool {
        self.initialization
    }

    /// Get the inner setup failure that this error is a wrapper around.
    #[must_use]
    pub fn as_setup_failure(&self) -> Option<&SetupFailure> {
        match self.inner {
            Inner::SetupFailed(ref sf) => Some(sf),
            _ => None,
        }
    }

    /// Convert this error into a setup failure.
    pub fn into_setup_failure(self) -> Result<SetupFailure> {
        match self.inner {
            Inner::SetupFailed(sf) => Ok(sf),
            inner => Err(Self::from_inner(inner)),
        }
    }
}

// crate-private api
cfg_async! {
    impl Error {
        pub(crate) fn async_send_in_progress(seq: u64) -> Self {
            Error::from_inner(Inner::AsyncSendInProgress { seq })
        }
    }
}

// public API
cfg_std! {
    impl Error {
        /// Get the inner I/O error that this error is a wrapper around.
        #[must_use]
        pub fn as_io_error(&self) -> Option<&IoError> {
            match self.inner {
                Inner::Io(ref io) => Some(io),
                _ => None,
            }
        }

        /// Convert this error into an I/O error.
        pub fn into_io_error(self) -> core::result::Result<IoError, Self> {
            match self.inner {
                Inner::Io(io) => Ok(io),
                inner => Err(Self::from_inner(inner)),
            }
        }
    }
}

// crate-private API
cfg_std_unix! {
    impl Error {
        pub(crate) fn nix(errno: Errno) -> Self {
            Error::io(IoError::from_raw_os_error(errno as i32))
        }
    }
}

/* public trait impls */

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        struct InnerRepr<'a>(&'a Inner);

        impl<'a> fmt::Debug for InnerRepr<'a> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                match self.0 {
                    Inner::Unsupported(ref e) => write!(f, "Unsupported: {}", e),
                    Inner::Message(ref msg) => f.write_str(msg),
                    Inner::InvalidState(ref e) => write!(f, "InvalidState: {}", e),
                    Inner::CouldntParseDisplay { .. } => f.write_str("CouldntParseDisplay"),
                    Inner::Poisoned { type_name } => write!(f, "Poisoned({})", type_name),
                    Inner::ParseError(err) => write!(f, "ParseError: {}", err),
                    Inner::SetupFailed(SetupFailure::Authenticate(_)) => {
                        f.write_str("SetupFailed: could not authenticate")
                    }
                    Inner::SetupFailed(SetupFailure::Failed(fail)) => {
                        let reason = str::from_utf8(&fail.reason).unwrap_or("bad utf-8");
                        write!(f, "SetupFailed: {}", reason)
                    }
                    Inner::X11Error(x11) => fmt::Debug::fmt(x11, f),
                    Inner::MissingExtension { name } => {
                        write!(f, "MissingExtension: {}", name)
                    }
                    Inner::RequestTooLarge { x_len, max_len } => {
                        write!(f, "RequestTooLarge: {} > {}", x_len, max_len)
                    }
                    #[cfg(feature = "async")]
                    Inner::AsyncSendInProgress { seq } => {
                        write!(f, "AsyncSendInProgress: seq {}", seq)
                    }
                    #[cfg(feature = "std")]
                    Inner::Io(ref e) => write!(f, "{:?}", e),
                }
            }
        }

        f.debug_tuple("Error")
            .field(&InnerRepr(&self.inner))
            .finish()
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.inner {
            Inner::Unsupported(ref e) => write!(f, "attempted an unsupported operation: {}", e),
            Inner::InvalidState(ref e) => write!(f, "entered an invalid state: {}", e),
            Inner::CouldntParseDisplay { from_env: true } => {
                f.write_str("could not parse the $DISPLAY environment variable")
            }
            Inner::Poisoned { type_name } => write!(f, "object of type {} poisoned", type_name),
            Inner::CouldntParseDisplay { from_env: false } => {
                f.write_str("could not parse the display string")
            }
            Inner::ParseError(ref err) => fmt::Display::fmt(err, f),
            Inner::SetupFailed(SetupFailure::Authenticate(ref e)) => {
                let reason = str::from_utf8(&e.reason).unwrap_or("<invalid utf8>");
                write!(f, "could not authenticate to the X server: {}", reason)
            }
            Inner::SetupFailed(SetupFailure::Failed(ref e)) => {
                let reason = str::from_utf8(&e.reason).unwrap_or("<invalid utf8>");
                write!(f, "could not setup the X connection: {}", reason)
            }
            Inner::X11Error(ref x11) => {
                write!(
                    f,
                    "a {:?} error occurred on sequence number {}",
                    x11.error_kind, x11.sequence,
                )
            }
            Inner::MissingExtension { name } => {
                write!(f, "missing extension: {}", name)
            }
            Inner::Message(ref msg) => f.write_str(msg),
            Inner::RequestTooLarge { x_len, max_len } => {
                write!(
                    f,
                    "Request of size {} bytes exceeds maximum length of {} bytes",
                    x_len * 4,
                    max_len * 4
                )
            }
            #[cfg(feature = "async")]
            Inner::AsyncSendInProgress { seq } => {
                write!(f, "async send in progress for sequence {}", seq)
            }
            #[cfg(feature = "std")]
            Inner::Io(ref e) => write!(f, "{}", e),
        }
    }
}

impl fmt::Display for Unsupported {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Unsupported::Fds => f.write_str("file descriptors"),
            Unsupported::Socket => f.write_str("unix sockets"),
        }
    }
}

impl fmt::Display for InvalidState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            InvalidState::UnexpectedFds => f.write_str("unexpected file descriptors"),
            InvalidState::NotEnoughSetup => f.write_str("not enough data for setup"),
            InvalidState::ScreenOutOfRange => f.write_str("screen out of range"),
            InvalidState::ZeroIdMask => f.write_str("zero id mask"),
            InvalidState::BadError => f.write_str("misformatted error"),
            InvalidState::XidsExhausted => f.write_str("server ran out of xids"),
        }
    }
}

// public api
impl From<X11Error> for Error {
    fn from(x11: X11Error) -> Self {
        Self::from_inner(Inner::X11Error(x11))
    }
}

cfg_std! {
    impl From<IoError> for Error {
        fn from(e: IoError) -> Self {
            Self::from_inner(Inner::Io(e))
        }
    }

    impl StdError for Error {
        fn source(&self) -> Option<&(dyn StdError + 'static)> {
            match self.inner {
                Inner::ParseError(ref pe) => Some(pe),
                #[cfg(feature = "std")]
                Inner::Io(ref e) => Some(e),
                _ => None,
            }
        }
    }
}

/// Indicates that any errors that occur during execution of this function
/// are initialization errors.
pub(crate) fn initialization<R>(f: impl FnOnce() -> Result<R>) -> Result<R> {
    match f() {
        Ok(r) => Ok(r),
        Err(mut e) => {
            e.initialization = true;
            Err(e)
        }
    }
}

/// A convenience type that is equivalent to
/// `Result<T, [Error]>`.
pub type Result<T = ()> = core::result::Result<T, Error>;