use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum ErrorKind {
Truncated,
Malformed,
Unsupported,
OutOfRange,
RecursionLimit,
InvalidUtf8,
InvalidPrefixTarget,
UnknownOpcode,
}
impl ErrorKind {
pub const fn as_str(self) -> &'static str {
match self {
ErrorKind::Truncated => "truncated input",
ErrorKind::Malformed => "malformed structure",
ErrorKind::Unsupported => "unsupported structure",
ErrorKind::OutOfRange => "value out of range",
ErrorKind::RecursionLimit => "recursion limit exceeded",
ErrorKind::InvalidUtf8 => "invalid UTF-8",
ErrorKind::InvalidPrefixTarget => "invalid prefix target",
ErrorKind::UnknownOpcode => "unknown opcode",
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Error {
pub kind: ErrorKind,
pub offset: Option<usize>,
pub context: &'static str,
}
impl Error {
pub const fn new(kind: ErrorKind, offset: usize, context: &'static str) -> Self {
Error { kind, offset: Some(offset), context }
}
pub const fn detached(kind: ErrorKind, context: &'static str) -> Self {
Error { kind, offset: None, context }
}
#[must_use]
pub const fn rebase(self, base: usize) -> Self {
match self.offset {
Some(o) => Error {
kind: self.kind,
offset: Some(o.saturating_add(base)),
context: self.context,
},
None => Error { kind: self.kind, offset: Some(base), context: self.context },
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.context, self.kind.as_str())?;
if let Some(offset) = self.offset {
write!(f, " at offset {offset:#x}")?;
}
Ok(())
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Strictness {
Strict,
#[default]
Permissive,
}
impl Strictness {
pub const fn is_strict(self) -> bool {
matches!(self, Strictness::Strict)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Diagnostic {
pub code: DiagnosticCode,
pub offset: Option<usize>,
pub detail: u64,
}
impl Diagnostic {
pub(crate) const fn new(code: DiagnosticCode, offset: usize, detail: u64) -> Self {
Diagnostic { code, offset: Some(offset), detail }
}
}
impl fmt::Display for Diagnostic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (detail {:#x})", self.code.as_str(), self.detail)?;
if let Some(offset) = self.offset {
write!(f, " at offset {offset:#x}")?;
}
Ok(())
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum DiagnosticCode {
FatHeaderSize,
StreamNamePadding,
DuplicateStreamName,
StreamRangeClamped,
ReservedTablePresent,
TableNotSorted,
VersionStringPadding,
CliHeaderSize,
SectionRangeClamped,
EhSectionSize,
BitnessFlagsInconsistent,
UnknownHeapSizeBits,
}
impl DiagnosticCode {
pub const fn as_str(self) -> &'static str {
match self {
DiagnosticCode::FatHeaderSize => "fat method header size is not 3 dwords",
DiagnosticCode::StreamNamePadding => "stream name is not NUL-padded",
DiagnosticCode::DuplicateStreamName => "duplicate stream name",
DiagnosticCode::StreamRangeClamped => "stream range clamped to metadata region",
DiagnosticCode::ReservedTablePresent => "reserved table id flagged present",
DiagnosticCode::TableNotSorted => "table declared sorted is not sorted",
DiagnosticCode::VersionStringPadding => "version string is not NUL-terminated",
DiagnosticCode::CliHeaderSize => "CLI header size is not 72",
DiagnosticCode::SectionRangeClamped => "section raw range clamped to file",
DiagnosticCode::EhSectionSize => "exception section size is inconsistent",
DiagnosticCode::BitnessFlagsInconsistent => "32BITPREFERRED without 32BITREQUIRED",
DiagnosticCode::UnknownHeapSizeBits => "unknown HeapSizes bits",
}
}
}