Debian Codegraph @

src/error.rs

raw symbols SCIP
1 //! Error types used and generated by Calloop.
2 //!
3 //! This module contains error types for Calloop's operations. They are designed
4 //! to make it easy to deal with errors arising from Calloop's internal I/O and
5 //! other operations.
6 //!
7 //! There are two top-level error types:
8 //!
9 //! - [`Error`]: used by callback functions, internal operations, and some event
10 //! loop API calls
11 //!
12 //! - [`InsertError`]: used primarily by the [`insert_source()`] method when an
13 //! event source cannot be added to the loop and needs to be given back to the
14 //! caller
15 //!
16 //! [`insert_source()`]: crate::LoopHandle::insert_source()
17
18 use std::fmt::{self, Debug, Formatter};
19
20 /// The primary error type used by Calloop covering internal errors and I/O
21 /// errors that arise during loop operations such as source registration or
22 /// event dispatching.
23 #[derive(thiserror::Error, Debug)]
24 pub enum Error {
25 /// When an event source is registered (or re- or un-registered) with the
26 /// event loop, this error variant will occur if the token Calloop uses to
27 /// keep track of the event source is not valid.
28 #[error("invalid token provided to internal function")]
29 InvalidToken,
30
31 /// This variant wraps a [`std::io::Error`], which might arise from
32 /// Calloop's internal operations.
33 #[error("underlying IO error")]
34 IoError(#[from] std::io::Error),
35
36 /// Any other unexpected error kind (most likely from a user implementation of
37 /// [`EventSource::process_events()`]) will be wrapped in this.
38 ///
39 /// [`EventSource::process_events()`]: crate::EventSource::process_events()
40 #[error("other error during loop operation")]
41 OtherError(#[from] Box<dyn std::error::Error + Sync + Send>),
42 }
43
44 impl From<nix::errno::Errno> for Error {
45 /// Converts a [`nix::Error`] into a wrapped version of the equivalent
46 /// [`std::io::Error`].
47 fn from(err: nix::errno::Errno) -> Self {
48 Into::<std::io::Error>::into(err).into()
49 }
50 }
51
52 impl From<Error> for std::io::Error {
53 /// Converts Calloop's error type into a [`std::io::Error`].
54 fn from(err: Error) -> Self {
55 match err {
56 Error::IoError(source) => source,
57 Error::InvalidToken => Self::new(std::io::ErrorKind::InvalidInput, err.to_string()),
58 Error::OtherError(source) => Self::new(std::io::ErrorKind::Other, source),
59 }
60 }
61 }
62
63 /// [`Result`] alias using Calloop's error type.
64 pub type Result<T> = core::result::Result<T, Error>;
65
66 /// An error generated when trying to insert an event source
67 #[derive(thiserror::Error)]
68 #[error("error inserting event source")]
69 pub struct InsertError<T> {
70 /// The source that could not be inserted
71 pub inserted: T,
72 /// The generated error
73 #[source]
74 pub error: Error,
75 }
76
77 impl<T> Debug for InsertError<T> {
78 #[cfg_attr(coverage, no_coverage)]
79 fn fmt(&self, formatter: &mut Formatter) -> core::result::Result<(), fmt::Error> {
80 write!(formatter, "{:?}", self.error)
81 }
82 }
83
84 impl<T> From<InsertError<T>> for crate::Error {
85 /// Converts the [`InsertError`] into Calloop's error type, throwing away
86 /// the contained source.
87 #[cfg_attr(coverage, no_coverage)]
88 fn from(e: InsertError<T>) -> crate::Error {
89 e.error
90 }
91 }