rseip_core/
error.rs

1// rseip
2//
3// rseip - Ethernet/IP (CIP) in pure Rust.
4// Copyright: 2021, Joylei <leingliu@gmail.com>
5// License: MIT
6
7use core::fmt;
8pub use std::error::Error as StdError;
9use std::io;
10
11pub trait Error: Sized + StdError + From<io::Error> {
12    fn with_kind(self, kind: &'static str) -> Self;
13
14    /// Raised when there is general error when decoding a type.
15    fn custom<T: fmt::Display>(msg: T) -> Self;
16
17    /// Raised when receives a type different from what it was expecting.
18    fn invalid_type<U: fmt::Display, E: fmt::Display>(unexp: U, exp: E) -> Self {
19        Self::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
20    }
21
22    /// Raised when receives a value of the right type but that
23    /// is wrong for some other reason.
24    fn invalid_value<U: fmt::Display, E: fmt::Display>(unexp: U, exp: E) -> Self {
25        Self::custom(format_args!("invalid value: {}, expected {}", unexp, exp))
26    }
27
28    /// Raised when the input data contains too many
29    /// or too few elements.
30    fn invalid_length<E: fmt::Display>(len: usize, exp: E) -> Self {
31        Self::custom(format_args!("invalid length: {}, expected {}", len, exp))
32    }
33}