Skip to main content

tinyquant_io/
errors.rs

1//! I/O and serialization errors for `tinyquant-io`.
2
3use thiserror::Error;
4
5/// Errors produced by serialization and file I/O operations.
6#[non_exhaustive]
7#[derive(Debug, Error)]
8pub enum IoError {
9    /// The input slice is shorter than expected.
10    #[error("data too short: needed {needed} bytes, got {got}")]
11    Truncated {
12        /// Minimum bytes required.
13        needed: usize,
14        /// Bytes actually available.
15        got: usize,
16    },
17
18    /// The format version byte is not recognized.
19    #[error("unknown format version {got:#04x}")]
20    UnknownVersion {
21        /// The version byte found in the data.
22        got: u8,
23    },
24
25    /// The `bit_width` field in the serialized data is not in `{2, 4, 8}`.
26    #[error("invalid bit_width {got} in serialized data")]
27    InvalidBitWidth {
28        /// The invalid bit-width value.
29        got: u8,
30    },
31
32    /// The `config_hash` field contains bytes that are not valid UTF-8.
33    #[error("invalid UTF-8 in config hash")]
34    InvalidUtf8,
35
36    /// A length in the serialized data does not match the expected value.
37    #[error("input/output length mismatch")]
38    LengthMismatch,
39
40    /// The file magic bytes are not `TQCV`.
41    #[error("bad magic bytes: expected TQCV, got {got:?}")]
42    BadMagic {
43        /// The actual magic bytes found in the file.
44        got: [u8; 4],
45    },
46
47    /// A header field value is outside its valid range.
48    #[error("invalid header field")]
49    InvalidHeader,
50
51    /// An inner codec operation failed during decode.
52    #[error("decode error: {0}")]
53    Decode(#[from] tinyquant_core::errors::CodecError),
54
55    /// A file system I/O error (used in Phase 17+ file-path APIs).
56    #[error("I/O error: {0}")]
57    Io(#[from] std::io::Error),
58}