ex3_ic_agent/request_id/
error.rs

1//! Error type for the RequestId calculation.
2use thiserror::Error;
3
4/// Errors from reading a RequestId from a string. This is not the same as
5/// deserialization.
6#[derive(Error, Debug)]
7pub enum RequestIdFromStringError {
8    /// The string was not of a valid length.
9    #[error("Invalid string size: {0}. Must be even.")]
10    InvalidSize(usize),
11
12    /// The string was not in a valid hexadecimal format.
13    #[error("Error while decoding hex: {0}")]
14    FromHexError(hex::FromHexError),
15}
16
17/// An error during the calculation of the RequestId.
18///
19/// Since we use serde for serializing a data type into a hash, this has to support traits that
20/// serde expects, such as Display
21#[derive(Error, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
22pub enum RequestIdError {
23    /// An unknown error occurred inside `serde`.
24    #[error("A custom error happened inside Serde: {0}")]
25    CustomSerdeError(String),
26    /// The serializer was not given any data.
27    #[error("Need to provide data to serialize")]
28    EmptySerializer,
29    /// The serializer was in an invalid state.
30    #[error("RequestId Serializer was in an invalid state")]
31    InvalidState,
32    /// The serializer received a nested struct, which it does not support.
33    #[error("RequestId does not support struct inside other structs")]
34    UnsupportedStructInsideStruct,
35    /// The serializer received a `bool`, which it does not support.
36    #[error("Unsupported type: Bool")]
37    UnsupportedTypeBool,
38    /// The serializer received a `i8`, which it does not support.
39    #[error("Unsupported type: I8")]
40    UnsupportedTypeI8,
41    /// The serializer received a `i16`, which it does not support.
42    #[error("Unsupported type: I16")]
43    UnsupportedTypeI16,
44    /// The serializer received a `i32`, which it does not support.
45    #[error("Unsupported type: I32")]
46    UnsupportedTypeI32,
47    /// The serializer received a `i64`, which it does not support.
48    #[error("Unsupported type: I64")]
49    UnsupportedTypeI64,
50    /// The serializer received a `f32`, which it does not support.
51    #[error("Unsupported type: F32")]
52    UnsupportedTypeF32,
53    /// The serializer received a `f64`, which it does not support.
54    #[error("Unsupported type: F64")]
55    UnsupportedTypeF64,
56    /// The serializer received a `char`, which it does not support.
57    #[error("Unsupported type: Char")]
58    UnsupportedTypeChar,
59    // UnsupportedTypeStr, // Supported
60    // UnsupportedTypeNone, // Supported
61    // UnsupportedTypeSome, // Supported
62    /// The serializer received a `()`, which it does not support.
63    #[error("Unsupported type: Unit")]
64    UnsupportedTypeUnit,
65    /// The serializer received a `PhantomData`, which it does not support.
66    #[error("Unsupported type: PhantomData")]
67    UnsupportedTypePhantomData,
68
69    // Variants and complex types.
70    /// The serializer received an enum unit variant, which it does not support.
71    #[error("Unsupported type: UnitVariant")]
72    UnsupportedTypeUnitVariant,
73    /// The serializer received a newtype struct, which it does not support.
74    #[error("Unsupported type: NewtypeStruct")]
75    UnsupportedTypeNewtypeStruct(String),
76    /// The serializer received an enum newtype variant, which it does not support.
77    #[error("Unsupported type: NewTypeVariant")]
78    UnsupportedTypeNewTypeVariant,
79    /// The serializer received a tuple, which it does not support.
80    #[error("Unsupported type: Tuple")]
81    UnsupportedTypeTuple,
82    /// The serializer received a tuple struct, which it does not support.
83    #[error("Unsupported type: TupleStruct")]
84    UnsupportedTypeTupleStruct,
85    /// The serializer received an enum tuple variant, which it does not support.
86    #[error("Unsupported type: TupleVariant")]
87    UnsupportedTypeTupleVariant,
88    /// The serializer received a map, which it does not support.
89    #[error("Unsupported type: Map")]
90    UnsupportedTypeMap,
91    // UnsupportedTypeStruct, // Supported
92    /// The serializer received an enum struct variant, which it does not support.
93    #[error("Unsupported type: StructVariant")]
94    UnsupportedTypeStructVariant,
95}
96
97impl serde::ser::Error for RequestIdError {
98    fn custom<T>(msg: T) -> Self
99    where
100        T: std::fmt::Display,
101    {
102        RequestIdError::CustomSerdeError(msg.to_string())
103    }
104}