use core::fmt;
use crate::types::branch;
#[derive(Debug, PartialEq, Eq)]
pub enum Error<I> {
StartOfTrace,
UnsupportedFeature(&'static str),
UnprocessedInstructions,
CannotAddBranches(branch::Error),
UnprocessedBranches(core::num::NonZeroU8),
UnexpectedUninferableDiscon,
UnresolvableBranch,
CannotConstructIrStack(usize),
CannotGetInstruction(I, u64),
}
impl<I> core::error::Error for Error<I>
where
I: fmt::Debug + core::error::Error + 'static,
{
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::CannotAddBranches(inner) => Some(inner),
Self::CannotGetInstruction(inner, _) => Some(inner),
_ => None,
}
}
}
impl<I> fmt::Display for Error<I> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::StartOfTrace => write!(f, "expected sync packet"),
Self::UnsupportedFeature(feat) => write!(f, "feature \"{feat}\" not supported"),
Self::UnprocessedInstructions => write!(f, "unprocessed instructions"),
Self::CannotAddBranches(_) => write!(f, "cannot add branches to branch map"),
Self::UnprocessedBranches(c) => write!(f, "{c} unprocessed branches"),
Self::UnexpectedUninferableDiscon => write!(f, "unexpected uninferable discontinuity"),
Self::UnresolvableBranch => write!(f, "unresolvable branch"),
Self::CannotConstructIrStack(size) => {
write!(f, "Cannot construct return stack of size {size}")
}
Self::CannotGetInstruction(_, addr) => {
write!(f, "Cannot get the instruction at {addr:#0x}")
}
}
}
}