rm2k/error.rs
1//! Fatal errors.
2//!
3//! A [`ReadError`] means parsing *stopped* (input too short for even a header); a
4//! [`Diagnostic`](crate::diag::Diagnostic) means parsing recovered and went on. Both are `Copy`
5//! and hold only offsets and `&'static str`, so they work in the core tier with no allocator.
6
7use thiserror::Error;
8
9/// Something went wrong badly enough that parsing could not continue.
10#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
11pub enum ReadError {
12 /// The buffer ended in the middle of a value that must be present.
13 #[error("unexpected end of input at offset {offset}")]
14 UnexpectedEof {
15 /// File-absolute offset where the input ran out.
16 offset: u32,
17 },
18 /// The file's magic string is not the one this loader expects.
19 ///
20 /// Carries the expected magic; the observed bytes stay in the caller's buffer.
21 #[error("bad magic at offset {offset}: expected {expected:?}")]
22 BadMagic {
23 /// The magic string this loader requires.
24 expected: &'static str,
25 /// File-absolute offset the magic was read from.
26 offset: u32,
27 },
28 /// A compressed integer ran past the format's 5-byte cap.
29 #[error("compressed integer at offset {offset} exceeds 5 bytes")]
30 VarintOverflow {
31 /// File-absolute offset of the integer's first byte.
32 offset: u32,
33 },
34 /// A chunk declared a length extending past the end of its parent.
35 #[error("chunk 0x{chunk_id:02x} at offset {offset} declares {len} bytes, past end of parent")]
36 ChunkOverrun {
37 /// File-absolute offset of the chunk's payload.
38 offset: u32,
39 /// The chunk's field id.
40 chunk_id: u32,
41 /// The length the chunk declared.
42 len: u32,
43 },
44 /// Strict mode was requested and a recoverable problem was found.
45 ///
46 /// The payload is deliberately flattened to the diagnostic's own fields so this stays `Copy`.
47 #[error("strict mode: {0}")]
48 Strict(crate::diag::Diagnostic),
49}
50
51/// Something went wrong while serialising.
52///
53/// One variant, unreachable when writing into a `Vec<u8>` (its [`Sink`](crate::sink::Sink) impl
54/// uses `Infallible`).
55#[derive(Clone, Copy, Debug, Eq, Error, PartialEq)]
56pub enum WriteError {
57 /// A fixed-size [`SliceSink`](crate::sink::SliceSink) ran out of room.
58 #[error("output buffer full")]
59 BufferFull,
60}