drmem_api/types/
mod.rs

1//! Defines fundamental types used throughout the DrMem codebase.
2
3use std::fmt;
4use tokio::sync::{mpsc, oneshot};
5
6/// Enumerates all the errors that can be reported in DrMem. Authors
7/// for new drivers or storage backends should try to map their errors
8/// into one of these values. If no current value is appropriate, a
9/// new one could be added (requiring a new release of this crate) but
10/// make sure the new error code is generic enough that it may be
11/// useful for other drivers or backends. For instance, don't add an
12/// error value that is specific to Redis. Add a more general value
13/// and use the associated description string to explain the details.
14
15#[derive(Debug, PartialEq, Eq, Clone)]
16pub enum Error {
17    /// Returned whenever a resource cannot be found.
18    NotFound,
19
20    /// A resource is already in use.
21    InUse,
22
23    /// The device name is already registered to another driver.
24    DeviceDefined(String),
25
26    /// Reported when the peer of a communication channel has closed
27    /// its handle.
28    MissingPeer(String),
29
30    /// A type mismatch is preventing the operation from continuing.
31    TypeError,
32
33    /// An invalid value was provided.
34    InvArgument(String),
35
36    /// A general error returned by the backend storage. The string
37    /// will have more information about the error.
38    BackendError(String),
39
40    /// Communication was disrupted due to one end not following a
41    /// protocol.
42    ProtocolError(String),
43
44    /// The requested operation cannot complete because the process
45    /// hasn't provided proper authentication credentials.
46    AuthenticationError,
47
48    /// An operation didn't complete in a timely fashion.
49    TimeoutError,
50
51    /// The requested operation couldn't complete. The description
52    /// field will have more information for the user.
53    OperationError(String),
54
55    /// A bad parameter was given in a configuration or a
56    /// configuration was missing a required parameter.
57    ConfigError(String),
58
59    /// There was a problem parsing a string. The associated string
60    /// will describe how the parsing failed.
61    ParseError(String),
62}
63
64impl std::error::Error for Error {}
65
66impl fmt::Display for Error {
67    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
68        match self {
69            Error::NotFound => write!(f, "item not found"),
70            Error::InUse => write!(f, "item is in use"),
71            Error::DeviceDefined(name) => {
72                write!(f, "device {} is already defined", &name)
73            }
74            Error::MissingPeer(detail) => {
75                write!(f, "{} is missing peer", detail)
76            }
77            Error::TypeError => write!(f, "incorrect type"),
78            Error::InvArgument(v) => write!(f, "{}", &v),
79            Error::BackendError(v) => {
80                write!(f, "backend error: {}", &v)
81            }
82            Error::ProtocolError(v) => write!(f, "protocol error: {}", &v),
83            Error::AuthenticationError => write!(f, "permission error"),
84            Error::TimeoutError => write!(f, "timeout"),
85            Error::OperationError(v) => {
86                write!(f, "couldn't complete operation: {}", &v)
87            }
88            Error::ConfigError(v) => write!(f, "config error: {}", &v),
89            Error::ParseError(v) => write!(f, "parse error: {}", &v),
90        }
91    }
92}
93
94// Defining these trait implementations allows any code that sends
95// requests over an `mpsc` channel and expects the reply in a
96// `oneshot` to easily translate the channel errors into a DrMem
97// error.
98
99impl<T> From<mpsc::error::SendError<T>> for Error {
100    fn from(_error: mpsc::error::SendError<T>) -> Self {
101        Error::MissingPeer(String::from("request channel is closed"))
102    }
103}
104
105impl From<oneshot::error::RecvError> for Error {
106    fn from(_error: oneshot::error::RecvError) -> Self {
107        Error::MissingPeer(String::from("request dropped"))
108    }
109}
110
111pub mod device;