use core::fmt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SnapshotError {
TruncatedHeader {
needed: usize,
actual: usize,
},
MalformedHeader,
BadMagic {
actual: [u8; 8],
},
FormatMajorMismatch {
actual: u32,
supported: u32,
},
FormatMinorTooNew {
actual: u32,
max_supported: u32,
},
HeaderSizeMismatch {
actual: u32,
expected: u32,
},
NonZeroHeaderReserved,
SectionCountTooLarge {
count: u32,
max: u32,
},
TruncatedSectionTable {
needed: usize,
actual: usize,
},
MalformedSectionTable,
NonZeroEntryChecksum {
kind: u32,
},
NonZeroEntryReserved {
kind: u32,
},
UnsupportedFlags {
kind: u32,
flags: u8,
},
AlignmentLog2TooLarge {
kind: u32,
alignment_log2: u8,
},
SectionRangeOverflow {
kind: u32,
},
SectionOutOfBounds {
kind: u32,
offset: u64,
length: u64,
snapshot_len: u64,
},
UnsortedSectionTable {
index: usize,
},
DuplicateKind {
kind: u32,
},
UsizeOverflow {
value: u64,
},
}
impl fmt::Display for SnapshotError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TruncatedHeader { needed, actual } => write!(
formatter,
"snapshot header is truncated: needed {needed} bytes, got {actual}"
),
Self::MalformedHeader => formatter.write_str("snapshot header is malformed"),
Self::BadMagic { actual } => write!(formatter, "bad snapshot magic: {actual:?}"),
Self::FormatMajorMismatch { actual, supported } => write!(
formatter,
"unsupported snapshot format major: snapshot is {actual}, this reader supports {supported}"
),
Self::FormatMinorTooNew {
actual,
max_supported,
} => write!(
formatter,
"snapshot format minor {actual} is newer than this reader's maximum {max_supported}"
),
Self::HeaderSizeMismatch { actual, expected } => write!(
formatter,
"header_size mismatch: snapshot reports {actual}, this reader expects {expected}"
),
Self::NonZeroHeaderReserved => {
formatter.write_str("snapshot header reserved bytes are not all zero")
}
Self::SectionCountTooLarge { count, max } => {
write!(formatter, "section count {count} exceeds maximum {max}")
}
Self::TruncatedSectionTable { needed, actual } => write!(
formatter,
"section table is truncated: needed {needed} bytes, got {actual}"
),
Self::MalformedSectionTable => formatter.write_str("section table bytes are malformed"),
Self::NonZeroEntryChecksum { kind } => write!(
formatter,
"section {kind} entry reserved checksum bytes are not all zero"
),
Self::NonZeroEntryReserved { kind } => write!(
formatter,
"section {kind} entry trailing reserved bytes are not all zero"
),
Self::UnsupportedFlags { kind, flags } => write!(
formatter,
"section {kind} entry has unsupported flags byte {flags:#04x}"
),
Self::AlignmentLog2TooLarge {
kind,
alignment_log2,
} => write!(
formatter,
"section {kind} alignment_log2 {alignment_log2} exceeds maximum"
),
Self::SectionRangeOverflow { kind } => {
write!(formatter, "section {kind} offset + length overflows u64")
}
Self::SectionOutOfBounds {
kind,
offset,
length,
snapshot_len,
} => write!(
formatter,
"section {kind} is out of bounds: offset {offset}, length {length}, snapshot length {snapshot_len}"
),
Self::UnsortedSectionTable { index } => write!(
formatter,
"section table entry at index {index} is unsorted or overlaps its predecessor"
),
Self::DuplicateKind { kind } => write!(formatter, "duplicate section kind {kind}"),
Self::UsizeOverflow { value } => {
write!(formatter, "u64 value {value} does not fit usize")
}
}
}
}
impl core::error::Error for SnapshotError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SectionViewError {
LengthNotMultipleOfSize {
length: usize,
elem_size: usize,
},
AlignmentMismatch {
ptr_addr: usize,
required: usize,
},
}
impl fmt::Display for SectionViewError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::LengthNotMultipleOfSize { length, elem_size } => write!(
formatter,
"section length {length} is not a multiple of element size {elem_size}"
),
Self::AlignmentMismatch { ptr_addr, required } => write!(
formatter,
"section payload at address {ptr_addr:#x} is not aligned to {required}"
),
}
}
}
impl core::error::Error for SectionViewError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PlanError {
BufferTooSmall {
needed: usize,
actual: usize,
},
AlignmentTooLarge {
alignment_log2: u8,
},
PayloadOverflow,
TooManySections {
count: usize,
},
DuplicateKind {
kind: u32,
},
}
impl fmt::Display for PlanError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BufferTooSmall { needed, actual } => write!(
formatter,
"output buffer too small: needed {needed} bytes, got {actual}"
),
Self::AlignmentTooLarge { alignment_log2 } => write!(
formatter,
"alignment_log2 {alignment_log2} exceeds the v1 maximum"
),
Self::PayloadOverflow => {
formatter.write_str("snapshot payload arithmetic overflowed u64 or usize")
}
Self::TooManySections { count } => {
write!(formatter, "section count {count} exceeds the v1 maximum")
}
Self::DuplicateKind { kind } => write!(formatter, "duplicate section kind {kind}"),
}
}
}
impl core::error::Error for PlanError {}