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
use serde_json::Error as JsonError;
use thiserror::Error;

/// Result type for state resolution.
pub type Result<T> = std::result::Result<T, Error>;

/// Represents the various errors that arise when resolving state.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
    /// A deserialization error.
    #[error(transparent)]
    SerdeJson(#[from] JsonError),

    /// The given option or version is unsupported.
    #[error("Unsupported room version: {0}")]
    Unsupported(String),

    /// The given event was not found.
    #[error("Not found error: {0}")]
    NotFound(String),

    /// Invalid fields in the given PDU.
    #[error("Invalid PDU: {0}")]
    InvalidPdu(String),

    /// A custom error.
    #[error("{0}")]
    Custom(Box<dyn std::error::Error>),
}

impl Error {
    pub fn custom<E: std::error::Error + 'static>(e: E) -> Self {
        Self::Custom(Box::new(e))
    }
}