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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! CBOR error types.
use std::any::type_name;
use thiserror::Error;

/// Number larger than u64.
#[derive(Debug, Error)]
#[error("Number larger than {ty}.")]
pub struct NumberOutOfRange {
    /// Type.
    pub ty: &'static str,
}

impl NumberOutOfRange {
    /// Creates a new `NumberOutOfRange` error.
    pub fn new<T>() -> Self {
        Self {
            ty: type_name::<T>(),
        }
    }
}

/// Number is not minimally encoded.
#[derive(Debug, Error)]
#[error("Number not minimally encoded.")]
pub struct NumberNotMinimal;

/// Length larger than usize or too small, for example zero length cid field.
#[derive(Debug, Error)]
#[error("Length out of range when decoding {ty}.")]
pub struct LengthOutOfRange {
    /// Type.
    pub ty: &'static str,
}

impl LengthOutOfRange {
    /// Creates a new `LengthOutOfRange` error.
    pub fn new<T>() -> Self {
        Self {
            ty: type_name::<T>(),
        }
    }
}

/// Unexpected cbor code.
#[derive(Debug, Error)]
#[error("Unexpected cbor code `0x{code:x}` when decoding `{ty}`.")]
pub struct UnexpectedCode {
    /// Code.
    pub code: u8,
    /// Type.
    pub ty: &'static str,
}

impl UnexpectedCode {
    /// Creates a new `UnexpectedCode` error.
    pub fn new<T>(code: u8) -> Self {
        Self {
            code,
            ty: type_name::<T>(),
        }
    }
}

/// Unexpected key.
#[derive(Debug, Error)]
#[error("Unexpected key `{key}` when decoding `{ty}`.")]
pub struct UnexpectedKey {
    /// Key.
    pub key: String,
    /// Type.
    pub ty: &'static str,
}

impl UnexpectedKey {
    /// Creates a new `UnexpectedKey` error.
    pub fn new<T>(key: String) -> Self {
        Self {
            key,
            ty: type_name::<T>(),
        }
    }
}

/// Missing key.
#[derive(Debug, Error)]
#[error("Missing key `{key}` for decoding `{ty}`.")]
pub struct MissingKey {
    /// Key.
    pub key: &'static str,
    /// Type.
    pub ty: &'static str,
}

impl MissingKey {
    /// Creates a new `MissingKey` error.
    pub fn new<T>(key: &'static str) -> Self {
        Self {
            key,
            ty: type_name::<T>(),
        }
    }
}

/// Unknown cbor tag.
#[derive(Debug, Error)]
#[error("Unknown cbor tag `{0}`.")]
pub struct UnknownTag(pub u64);

/// Unexpected eof.
#[derive(Debug, Error)]
#[error("Unexpected end of file.")]
pub struct UnexpectedEof;

/// The byte before Cid was not multibase identity prefix.
#[derive(Debug, Error)]
#[error("Invalid Cid prefix: {0}")]
pub struct InvalidCidPrefix(pub u8);

/// A duplicate key within a map.
#[derive(Debug, Error)]
#[error("Duplicate map key.")]
pub struct DuplicateKey;