wapc_codec/
errors.rs

1//! # Errors
2//!
3//! This module generalizes errors for all the included codec functions.
4
5use std::error::Error as StdError;
6use std::fmt;
7
8/// This crate's Error type
9#[derive(Debug)]
10pub struct Error(Box<ErrorKind>);
11
12/// Create a new [Error] of the passed kind.
13#[must_use]
14pub fn new(kind: ErrorKind) -> Error {
15  Error(Box::new(kind))
16}
17
18/// The kinds of errors this crate returns.
19#[derive(Debug)]
20pub enum ErrorKind {
21  /// Error serializing into MessagePack bytes.
22  #[cfg(feature = "messagepack")]
23  MessagePackSerialization(rmp_serde::encode::Error),
24  /// Error deserializing from MessagePack bytes.
25  #[cfg(feature = "messagepack")]
26  MessagePackDeserialization(rmp_serde::decode::Error),
27}
28
29impl StdError for Error {}
30
31impl fmt::Display for Error {
32  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33    let errstr = match self.0.as_ref() {
34      #[cfg(feature = "messagepack")]
35      ErrorKind::MessagePackSerialization(e) => e.to_string(),
36      #[cfg(feature = "messagepack")]
37      ErrorKind::MessagePackDeserialization(e) => e.to_string(),
38    };
39    f.write_str(&errstr)
40  }
41}