1use core::fmt;
2
3#[derive(Debug)]
4pub enum CarError {
5 UnexpectedEnd,
7 VarintOverflow,
8 InvalidHeader(neco_cbor::DecodeErrorKind),
9 HeaderNotMap,
10 MissingHeaderField(&'static str),
11 UnsupportedVersion(u64),
12 RootsNotArray,
13 InvalidRootCid(neco_cid::CidError),
14 InvalidBlockCid(neco_cid::CidError),
15 BlockLengthMismatch,
16 EmptySection,
17 InvalidCidLink,
18 HeaderEncode(neco_cbor::EncodeError),
20}
21
22impl fmt::Display for CarError {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Self::UnexpectedEnd => f.write_str("unexpected end of input"),
26 Self::VarintOverflow => f.write_str("varint exceeds 64-bit range"),
27 Self::InvalidHeader(kind) => write!(f, "invalid header: {kind}"),
28 Self::HeaderNotMap => f.write_str("header is not a map"),
29 Self::MissingHeaderField(field) => write!(f, "missing header field: {field}"),
30 Self::UnsupportedVersion(version) => {
31 write!(f, "unsupported CAR version: {version}")
32 }
33 Self::RootsNotArray => f.write_str("roots field is not an array"),
34 Self::InvalidRootCid(err) => write!(f, "invalid root CID: {err}"),
35 Self::InvalidBlockCid(err) => write!(f, "invalid block CID: {err}"),
36 Self::BlockLengthMismatch => f.write_str("block length mismatch"),
37 Self::EmptySection => f.write_str("empty section"),
38 Self::InvalidCidLink => {
39 f.write_str("invalid CID link (expected tag 42 with 0x00 prefix)")
40 }
41 Self::HeaderEncode(err) => write!(f, "header encode error: {err}"),
42 }
43 }
44}
45
46impl std::error::Error for CarError {}
47
48impl PartialEq for CarError {
49 fn eq(&self, other: &Self) -> bool {
50 match (self, other) {
51 (Self::UnexpectedEnd, Self::UnexpectedEnd) => true,
52 (Self::VarintOverflow, Self::VarintOverflow) => true,
53 (Self::InvalidHeader(a), Self::InvalidHeader(b)) => a == b,
54 (Self::HeaderNotMap, Self::HeaderNotMap) => true,
55 (Self::MissingHeaderField(a), Self::MissingHeaderField(b)) => a == b,
56 (Self::UnsupportedVersion(a), Self::UnsupportedVersion(b)) => a == b,
57 (Self::RootsNotArray, Self::RootsNotArray) => true,
58 (Self::InvalidRootCid(a), Self::InvalidRootCid(b)) => a == b,
59 (Self::InvalidBlockCid(a), Self::InvalidBlockCid(b)) => a == b,
60 (Self::BlockLengthMismatch, Self::BlockLengthMismatch) => true,
61 (Self::EmptySection, Self::EmptySection) => true,
62 (Self::InvalidCidLink, Self::InvalidCidLink) => true,
63 (Self::HeaderEncode(a), Self::HeaderEncode(b)) => a == b,
64 _ => false,
65 }
66 }
67}