1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! Error types for MSFT type library parsing.
//!
//! All errors are non-fatal data-validation failures. Because the parser
//! is zero-copy and does not allocate, I/O errors are the caller's
//! responsibility (load the file, then pass `&[u8]` to
//! [`TypeLib::parse`](crate::TypeLib::parse)).
use std::fmt;
/// Errors that can occur when parsing an MSFT type library.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
/// The input buffer is too short for the expected structure.
TooShort {
/// Minimum number of bytes required.
expected: usize,
/// Actual number of bytes available.
actual: usize,
/// Human-readable name of the structure being parsed.
context: &'static str,
},
/// The file does not start with the expected `"MSFT"` magic bytes.
BadMagic {
/// Expected magic value (`0x5446534D`).
expected: u32,
/// Actual value found at offset 0.
got: u32,
},
/// A TypeInfo index is outside the valid range `0..typeinfo_count`.
TypeInfoOutOfRange {
/// The requested index.
index: usize,
/// Total number of TypeInfo entries in this library.
count: usize,
},
/// A required field or offset could not be read from the binary data.
///
/// This typically indicates a truncated or corrupt type library.
InvalidData {
/// Human-readable description of what went wrong.
context: &'static str,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::TooShort {
expected,
actual,
context,
} => write!(
f,
"{context}: expected at least {expected} bytes, got {actual}"
),
Error::BadMagic { expected, got } => {
write!(f, "bad magic: expected 0x{expected:08X}, got 0x{got:08X}")
}
Error::TypeInfoOutOfRange { index, count } => {
write!(f, "typeinfo index {index} out of range (count: {count})")
}
Error::InvalidData { context } => {
write!(f, "invalid data: {context}")
}
}
}
}
impl std::error::Error for Error {}