libipld_raw_cbor/
error.rs

1//! CBOR error types.
2use std::any::type_name;
3use thiserror::Error;
4
5/// Number larger than u64.
6#[derive(Debug, Error)]
7#[error("Number larger than {ty}.")]
8pub struct NumberOutOfRange {
9    /// Type.
10    pub ty: &'static str,
11}
12
13impl NumberOutOfRange {
14    /// Creates a new `NumberOutOfRange` error.
15    pub fn new<T>() -> Self {
16        Self {
17            ty: type_name::<T>(),
18        }
19    }
20}
21
22/// Length larger than usize or too small, for example zero length cid field.
23#[derive(Debug, Error)]
24#[error("Length out of range when decoding {ty}.")]
25pub struct LengthOutOfRange {
26    /// Type.
27    pub ty: &'static str,
28}
29
30impl LengthOutOfRange {
31    /// Creates a new `LengthOutOfRange` error.
32    pub fn new<T>() -> Self {
33        Self {
34            ty: type_name::<T>(),
35        }
36    }
37}
38
39/// Unexpected cbor code.
40#[derive(Debug, Error)]
41#[error("Unexpected cbor code `0x{code:x}` when decoding `{ty}`.")]
42pub struct UnexpectedCode {
43    /// Code.
44    pub code: u8,
45    /// Type.
46    pub ty: &'static str,
47}
48
49impl UnexpectedCode {
50    /// Creates a new `UnexpectedCode` error.
51    pub fn new<T>(code: u8) -> Self {
52        Self {
53            code,
54            ty: type_name::<T>(),
55        }
56    }
57}
58
59/// Unexpected key.
60#[derive(Debug, Error)]
61#[error("Unexpected key `{key}` when decoding `{ty}`.")]
62pub struct UnexpectedKey {
63    /// Key.
64    pub key: String,
65    /// Type.
66    pub ty: &'static str,
67}
68
69impl UnexpectedKey {
70    /// Creates a new `UnexpectedKey` error.
71    pub fn new<T>(key: String) -> Self {
72        Self {
73            key,
74            ty: type_name::<T>(),
75        }
76    }
77}
78
79/// Missing key.
80#[derive(Debug, Error)]
81#[error("Missing key `{key}` for decoding `{ty}`.")]
82pub struct MissingKey {
83    /// Key.
84    pub key: &'static str,
85    /// Type.
86    pub ty: &'static str,
87}
88
89impl MissingKey {
90    /// Creates a new `MissingKey` error.
91    pub fn new<T>(key: &'static str) -> Self {
92        Self {
93            key,
94            ty: type_name::<T>(),
95        }
96    }
97}
98
99/// Unknown cbor tag.
100#[derive(Debug, Error)]
101#[error("Unkown cbor tag `{0}`.")]
102pub struct UnknownTag(pub u8);
103
104/// Unexpected eof.
105#[derive(Debug, Error)]
106#[error("Unexpected end of file.")]
107pub struct UnexpectedEof;
108
109/// The byte before Cid was not multibase identity prefix.
110#[derive(Debug, Error)]
111#[error("Invalid Cid prefix: {0}")]
112pub struct InvalidCidPrefix(pub u8);