Skip to main content

btree_network/error/
mod.rs

1#[cfg(feature = "fmt")]
2use core::fmt::{Display, Formatter, Result};
3
4mod test;
5
6#[cfg(any(feature = "serde_json", feature = "serde_yaml", feature = "serde_cbor"))]
7use try_encoding_from::Error as EncodingError;
8
9
10#[cfg(feature = "fmt")]
11static VERTEX_DOES_NOT_EXIST_ERROR: &str = "network Error: Vertex does not exist";
12
13/// Errors which may occur during normal usage of the library.
14#[derive(PartialEq, Debug)]
15pub enum Error {
16    VertexDoesNotExist,
17    #[cfg(any(feature = "serde_cbor", feature = "serde_json", feature = "serde_yaml"))]
18    EncodingError(EncodingError),
19}
20
21#[cfg(feature = "fmt")]
22impl Display for Error {
23    fn fmt(&self, f: &mut Formatter) -> Result {
24        match self {
25            Error::VertexDoesNotExist => write!(f, "{}", VERTEX_DOES_NOT_EXIST_ERROR),
26            #[cfg(any(feature = "serde_cbor", feature = "serde_json", feature = "serde_yaml"))]
27            Error::EncodingError(err) => write!(f, "{}", err),
28        }
29    }
30}
31
32#[cfg(any(feature = "serde_cbor", feature = "serde_json", feature = "serde_yaml"))]
33impl From<EncodingError> for Error {
34    fn from(e: EncodingError) -> Error {
35        Error::EncodingError(e)
36    }
37}
38
39#[cfg(feature = "serde_cbor")]
40impl From<try_encoding_from::serde_cbor::Error> for Error {
41    fn from(e: try_encoding_from::serde_cbor::Error) -> Error {
42        Error::EncodingError(EncodingError::from(e))
43    }
44}
45
46#[cfg(feature = "serde_json")]
47impl From<try_encoding_from::serde_json::Error> for Error {
48    fn from(e: try_encoding_from::serde_json::Error) -> Error {
49        Error::EncodingError(EncodingError::from(e))
50    }
51}
52
53#[cfg(feature = "serde_yaml")]
54impl From<try_encoding_from::serde_yaml::Error> for Error {
55    fn from(e: try_encoding_from::serde_yaml::Error) -> Error {
56        Error::EncodingError(EncodingError::from(e))
57    }
58}