Skip to main content

segb/
error.rs

1//! Error type for the SEGB reader.
2
3use thiserror::Error;
4
5/// All errors that the SEGB reader can produce.
6#[derive(Debug, Error)]
7pub enum SegbError {
8    /// The file / stream produced an I/O error.
9    #[error("I/O error: {0}")]
10    Io(#[from] std::io::Error),
11
12    /// The magic bytes at the expected position do not match `SEGB`.
13    #[error("invalid SEGB magic: expected 53455342 (\"SEGB\"), got {found}")]
14    BadMagic { found: String },
15
16    /// The file header is shorter than expected.
17    #[error("truncated header: need {need} bytes, got {got}")]
18    TruncatedHeader { need: usize, got: usize },
19
20    /// A record header is shorter than expected.
21    #[error("truncated record header at offset {offset}: need {need} bytes, got {got}")]
22    TruncatedRecordHeader {
23        offset: u64,
24        need: usize,
25        got: usize,
26    },
27
28    /// A record payload is shorter than the length field claims.
29    #[error("truncated record payload at offset {offset}: need {need} bytes, got {got}")]
30    TruncatedPayload {
31        offset: u64,
32        need: usize,
33        got: usize,
34    },
35
36    /// A `record_length` value is negative, which is not valid.
37    #[error("invalid record length {length} at offset {offset}")]
38    InvalidLength { offset: u64, length: i32 },
39
40    /// `entries_count` in a SEGB v2 header is negative.
41    #[error("invalid SEGB v2 entry count {count}")]
42    InvalidEntryCount { count: i32 },
43
44    /// The trailer size overflows the file size in SEGB v2.
45    #[error(
46        "SEGB v2 trailer ({trailer_bytes} bytes) exceeds stream length ({stream_bytes} bytes)"
47    )]
48    TrailerOverflow {
49        trailer_bytes: u64,
50        stream_bytes: u64,
51    },
52
53    /// An `EntryState` byte did not map to a known variant.
54    #[error("unknown entry state value {0}")]
55    UnknownState(i32),
56
57    /// A seek operation was asked to go to a negative or overflow position.
58    #[error("invalid seek offset: {0}")]
59    InvalidSeek(String),
60
61    /// A protobuf varint was longer than 10 bytes (malformed).
62    #[error("malformed protobuf varint at byte offset {offset}")]
63    MalformedVarint { offset: usize },
64
65    /// A protobuf length-delimited field extends past the buffer end.
66    #[error("protobuf length-delimited field at offset {offset} claims {length} bytes but only {remaining} remain")]
67    ProtobufOverflow {
68        offset: usize,
69        length: usize,
70        remaining: usize,
71    },
72}
73
74/// Convenience alias.
75pub type Result<T> = std::result::Result<T, SegbError>;