Skip to main content

pamoja_core/
error.rs

1//! The error model shared by every pamoja crate.
2//!
3//! A single [`Error`] type keeps failure handling uniform across capabilities and
4//! maps cleanly onto each language binding's native error idiom, such as
5//! exceptions or rejected promises.
6
7use core::fmt;
8
9/// The error type returned by all fallible pamoja operations.
10///
11/// This enum is `#[non_exhaustive]`: new variants may be added in future releases
12/// without a breaking change, so downstream `match` expressions must include a
13/// wildcard arm.
14#[derive(Debug)]
15#[non_exhaustive]
16pub enum Error {
17    /// A transport-level failure while connecting, sending, or receiving.
18    ///
19    /// The payload is a human-readable description provided by the transport.
20    Transport(String),
21
22    /// A device or peripheral input/output operation failed.
23    ///
24    /// The payload is a human-readable description of the I/O fault.
25    Io(String),
26
27    /// A payload could not be encoded or decoded.
28    ///
29    /// The payload describes the encoding or decoding fault.
30    Codec(String),
31
32    /// The operation targeted a resource that is closed or disconnected.
33    Closed,
34
35    /// The requested capability is not compiled into this build.
36    ///
37    /// The payload names the missing capability, for example `"mqtt"`.
38    Unsupported(&'static str),
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Self::Transport(message) => write!(f, "transport error: {message}"),
45            Self::Io(message) => write!(f, "io error: {message}"),
46            Self::Codec(message) => write!(f, "codec error: {message}"),
47            Self::Closed => f.write_str("resource is closed"),
48            Self::Unsupported(capability) => {
49                write!(f, "unsupported capability: {capability}")
50            }
51        }
52    }
53}
54
55impl std::error::Error for Error {}
56
57/// A specialized [`core::result::Result`] whose error type is fixed to [`Error`].
58pub type Result<T> = core::result::Result<T, Error>;