1use core::fmt;
12
13#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
15#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[non_exhaustive]
17pub enum ErrorKind {
18 Truncated,
20 Malformed,
22 Unsupported,
24 OutOfRange,
26 RecursionLimit,
28 InvalidUtf8,
30 InvalidPrefixTarget,
32 UnknownOpcode,
34}
35
36impl ErrorKind {
37 pub const fn as_str(self) -> &'static str {
39 match self {
40 ErrorKind::Truncated => "truncated input",
41 ErrorKind::Malformed => "malformed structure",
42 ErrorKind::Unsupported => "unsupported structure",
43 ErrorKind::OutOfRange => "value out of range",
44 ErrorKind::RecursionLimit => "recursion limit exceeded",
45 ErrorKind::InvalidUtf8 => "invalid UTF-8",
46 ErrorKind::InvalidPrefixTarget => "invalid prefix target",
47 ErrorKind::UnknownOpcode => "unknown opcode",
48 }
49 }
50}
51
52#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
57#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
58pub struct Error {
59 pub kind: ErrorKind,
61 pub offset: Option<usize>,
63 pub context: &'static str,
65}
66
67impl Error {
68 pub const fn new(kind: ErrorKind, offset: usize, context: &'static str) -> Self {
70 Error { kind, offset: Some(offset), context }
71 }
72
73 pub const fn detached(kind: ErrorKind, context: &'static str) -> Self {
75 Error { kind, offset: None, context }
76 }
77
78 #[must_use]
83 pub const fn rebase(self, base: usize) -> Self {
84 match self.offset {
85 Some(o) => Error {
86 kind: self.kind,
87 offset: Some(o.saturating_add(base)),
88 context: self.context,
89 },
90 None => Error { kind: self.kind, offset: Some(base), context: self.context },
91 }
92 }
93}
94
95impl fmt::Display for Error {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(f, "{}: {}", self.context, self.kind.as_str())?;
98 if let Some(offset) = self.offset {
99 write!(f, " at offset {offset:#x}")?;
100 }
101 Ok(())
102 }
103}
104
105#[cfg(feature = "std")]
106impl std::error::Error for Error {}
107
108pub type Result<T> = core::result::Result<T, Error>;
110
111#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
113#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
114pub enum Strictness {
115 Strict,
117 #[default]
120 Permissive,
121}
122
123impl Strictness {
124 pub const fn is_strict(self) -> bool {
126 matches!(self, Strictness::Strict)
127 }
128}
129
130#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
132#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
133pub struct Diagnostic {
134 pub code: DiagnosticCode,
136 pub offset: Option<usize>,
138 pub detail: u64,
140}
141
142impl Diagnostic {
143 pub(crate) const fn new(code: DiagnosticCode, offset: usize, detail: u64) -> Self {
144 Diagnostic { code, offset: Some(offset), detail }
145 }
146}
147
148impl fmt::Display for Diagnostic {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 write!(f, "{} (detail {:#x})", self.code.as_str(), self.detail)?;
151 if let Some(offset) = self.offset {
152 write!(f, " at offset {offset:#x}")?;
153 }
154 Ok(())
155 }
156}
157
158#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
164#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
165#[non_exhaustive]
166pub enum DiagnosticCode {
167 FatHeaderSize,
169 StreamNamePadding,
171 DuplicateStreamName,
173 StreamRangeClamped,
175 ReservedTablePresent,
177 TableNotSorted,
179 VersionStringPadding,
181 CliHeaderSize,
183 SectionRangeClamped,
185 EhSectionSize,
187 BitnessFlagsInconsistent,
189 UnknownHeapSizeBits,
191}
192
193impl DiagnosticCode {
194 pub const fn as_str(self) -> &'static str {
196 match self {
197 DiagnosticCode::FatHeaderSize => "fat method header size is not 3 dwords",
198 DiagnosticCode::StreamNamePadding => "stream name is not NUL-padded",
199 DiagnosticCode::DuplicateStreamName => "duplicate stream name",
200 DiagnosticCode::StreamRangeClamped => "stream range clamped to metadata region",
201 DiagnosticCode::ReservedTablePresent => "reserved table id flagged present",
202 DiagnosticCode::TableNotSorted => "table declared sorted is not sorted",
203 DiagnosticCode::VersionStringPadding => "version string is not NUL-terminated",
204 DiagnosticCode::CliHeaderSize => "CLI header size is not 72",
205 DiagnosticCode::SectionRangeClamped => "section raw range clamped to file",
206 DiagnosticCode::EhSectionSize => "exception section size is inconsistent",
207 DiagnosticCode::BitnessFlagsInconsistent => "32BITPREFERRED without 32BITREQUIRED",
208 DiagnosticCode::UnknownHeapSizeBits => "unknown HeapSizes bits",
209 }
210 }
211}