use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadingType {
U8,
U16,
U32,
U64,
Array {
size: usize,
},
Slice {
len: usize,
},
}
impl core::fmt::Display for ReadingType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::U8 => write!(f, "u8"),
Self::U16 => write!(f, "u16"),
Self::U32 => write!(f, "u32"),
Self::U64 => write!(f, "u64"),
Self::Array { size } => write!(f, "[u8; {size}]"),
Self::Slice { len } => write!(f, "&[u8] (len {len})"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("buffer too short reading {reading} at offset {offset}: need {need} bytes, have {have}")]
pub struct BufferTooShort {
pub reading: ReadingType,
pub offset: usize,
pub need: usize,
pub have: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("invalid schema: expected {expected:?}, got {got:?}")]
pub struct InvalidSchema {
pub expected: [u8; 4],
pub got: [u8; 4],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("array not sorted at index {index}")]
pub struct UnsortedArray {
pub index: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("invalid enum tag {tag:#04x} for {type_name}")]
pub struct InvalidEnumTag {
pub tag: u8,
pub type_name: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("size mismatch: declared {declared}, actual {actual}")]
pub struct SizeMismatch {
pub declared: usize,
pub actual: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("blob too large: {size} bytes, max {max}")]
pub struct BlobTooLarge {
pub size: u64,
pub max: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("{field} has too many elements: {count}, max {max}")]
pub struct ArrayTooLarge {
pub count: usize,
pub max: usize,
pub field: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("duplicate element in {field} at index {index}")]
pub struct DuplicateElement {
pub index: usize,
pub field: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("invalid discriminant: expected {expected:#04x}, got {got:#04x}")]
pub struct InvalidDiscriminant {
pub expected: u8,
pub got: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[error("bijou64 decode error at offset {offset}: {kind}")]
pub struct Bijou64Error {
pub offset: usize,
pub kind: bijoux::u64::DecodeError,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
pub enum DecodeError {
#[error(transparent)]
BufferTooShort(#[from] BufferTooShort),
#[error("{type_name} too short: need {need} bytes, have {have}")]
MessageTooShort {
type_name: &'static str,
need: usize,
have: usize,
},
#[error(transparent)]
InvalidSchema(#[from] InvalidSchema),
#[error("unsupported protocol version: {0}")]
UnsupportedVersion(u8),
#[error("invalid signature")]
InvalidSignature,
#[error("invalid verifying key")]
InvalidVerifyingKey,
#[error(transparent)]
UnsortedArray(#[from] UnsortedArray),
#[error(transparent)]
InvalidEnumTag(#[from] InvalidEnumTag),
#[error(transparent)]
InvalidDiscriminant(#[from] InvalidDiscriminant),
#[error(transparent)]
SizeMismatch(#[from] SizeMismatch),
#[error(transparent)]
BlobTooLarge(#[from] BlobTooLarge),
#[error(transparent)]
ArrayTooLarge(#[from] ArrayTooLarge),
#[error(transparent)]
DuplicateElement(#[from] DuplicateElement),
#[error(transparent)]
Bijou64(#[from] Bijou64Error),
}