use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScdcError<E> {
Transport(E),
Protocol(ProtocolError),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadModifyWriteError<E> {
Read(ScdcError<E>),
Write(ScdcError<E>),
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtocolError {
UnknownFrlRate(u8),
FfeLevelsOutOfRange {
rate: crate::FrlRate,
levels: u8,
},
}
impl<E: fmt::Display> fmt::Display for ScdcError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Transport(e) => write!(f, "SCDC transport error: {e}"),
Self::Protocol(p) => write!(f, "refused by the SCDC protocol: {p}"),
}
}
}
impl<E: core::error::Error + 'static> core::error::Error for ScdcError<E> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Transport(e) => Some(e),
Self::Protocol(p) => Some(p),
}
}
}
impl<E: fmt::Display> fmt::Display for ReadModifyWriteError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Read(e) => write!(f, "read failed, nothing written: {e}"),
Self::Write(e) => write!(f, "write failed after the read: {e}"),
}
}
}
impl<E: core::error::Error + 'static> core::error::Error for ReadModifyWriteError<E> {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Read(e) | Self::Write(e) => Some(e),
}
}
}
impl fmt::Display for ProtocolError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownFrlRate(raw) => {
write!(f, "the sink reported an undefined FRL rate value {raw:#x}")
}
Self::FfeLevelsOutOfRange { rate, levels } => {
write!(f, "{levels} FFE levels exceed the maximum for {rate:?}")
}
}
}
}
impl core::error::Error for ProtocolError {}
#[cfg(test)]
mod tests {
extern crate std;
use std::string::ToString;
use super::*;
#[derive(Debug, Clone, Copy, PartialEq)]
struct Bus;
impl fmt::Display for Bus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("bus gone")
}
}
impl core::error::Error for Bus {}
#[test]
fn errors_print_compare_and_chain() {
use core::error::Error;
let transport = ScdcError::Transport(Bus);
assert_eq!(transport.to_string(), "SCDC transport error: bus gone");
assert_eq!(transport.source().unwrap().to_string(), "bus gone");
let refused = ScdcError::<Bus>::Protocol(ProtocolError::UnknownFrlRate(0xF));
assert_eq!(
refused.to_string(),
"refused by the SCDC protocol: the sink reported an undefined FRL rate value 0xf"
);
assert!(refused.source().is_some());
let levels = ProtocolError::FfeLevelsOutOfRange {
rate: crate::FrlRate::Rate12Gbps4Lanes,
levels: 4,
};
assert!(
levels
.to_string()
.starts_with("4 FFE levels exceed the maximum for ")
);
assert!(levels.source().is_none());
let read = ReadModifyWriteError::Read(transport);
let write = ReadModifyWriteError::Write(transport);
assert_eq!(
read.to_string(),
"read failed, nothing written: SCDC transport error: bus gone"
);
assert_eq!(
write.to_string(),
"write failed after the read: SCDC transport error: bus gone"
);
assert!(write.source().is_some());
let copied = read;
assert_eq!(copied, read);
assert_ne!(read, write);
}
}