use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("I/O error: {0:?}")]
Io(embedded_io::ErrorKind),
#[error("incomplete: need {} bytes, have {}", .0.needed, .0.available)]
Incomplete(#[from] automotive_wire_codec::Incomplete),
#[error("trailing bytes: {} left over", .0.0)]
Trailing(#[from] automotive_wire_codec::TrailingBytes),
#[error("insufficient buffer: need {} bytes, have {}", .0.needed, .0.available)]
InsufficientBuffer(#[from] automotive_wire_codec::InsufficientBuffer),
#[error("Invalid protocol version: {0:X}")]
InvalidProtocolVersion(u8),
#[error("Invalid value in MessageType field: {0:X}")]
InvalidMessageTypeField(u8),
#[error("Invalid value in ReturnCode field: {0:X}")]
InvalidReturnCode(u8),
#[error("Invalid SOME/IP length field: {0} (minimum 8)")]
InvalidLength(u32),
#[error("Unsupported MessageID {0:X?}")]
UnsupportedMessageID(super::MessageId),
#[error(transparent)]
Sd(#[from] super::sd::Error),
}
impl From<embedded_io::ErrorKind> for Error {
fn from(k: embedded_io::ErrorKind) -> Self {
Error::Io(k)
}
}
impl From<automotive_wire_codec::EncodeToSliceError<Error>> for Error {
fn from(e: automotive_wire_codec::EncodeToSliceError<Error>) -> Self {
use automotive_wire_codec::EncodeToSliceError::{Encode, InsufficientBuffer};
match e {
InsufficientBuffer(ib) => Error::InsufficientBuffer(ib),
Encode(inner) => inner,
}
}
}
impl From<crate::e2e::Error> for Error {
fn from(err: crate::e2e::Error) -> Self {
match err {
crate::e2e::Error::BufferTooSmall { needed, actual } => {
Error::InsufficientBuffer(automotive_wire_codec::InsufficientBuffer {
needed,
available: actual,
})
}
}
}
}
#[cfg(test)]
mod e2e_bridge_tests {
use super::Error;
#[test]
fn buffer_too_small_maps_to_insufficient_buffer() {
let e2e_err = crate::e2e::Error::BufferTooSmall {
needed: 16,
actual: 10,
};
let mapped: Error = e2e_err.into();
match mapped {
Error::InsufficientBuffer(ib) => {
assert_eq!(ib.needed, 16);
assert_eq!(ib.available, 10);
}
other => panic!("expected Error::InsufficientBuffer, got {other:?}"),
}
}
}