use bytes::Bytes;
use miette::Diagnostic;
use thiserror::Error;
use tokio::sync::mpsc::error::SendError as BlockingSendError;
#[derive(Diagnostic, Error, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SdioApiError {
#[error("The LBA provided was invalid, must be divisble by 512 bytes: {0}")]
#[diagnostic(code(cat_dev::api::fsemul::sdio::invalid_lba))]
InvalidLBA(u32),
#[error("The channel provided was invalid, (first byte: {0:02x}, should be <0xC), (full: {1})")]
#[diagnostic(code(cat_dev::api::fsemul::sdio::invalid_channel))]
InvalidChannel(u8, u32),
#[error("The telnet message provided was too long, (max 499 bytes), was: [{0:?}]")]
#[diagnostic(code(cat_dev::api::fsemul::sdio::telnet_message_too_long))]
TelnetMessageToLong(Bytes),
}
#[derive(Diagnostic, Error, Debug, PartialEq, Eq)]
pub enum SdioNetworkError {
#[error("Failed to request a read of: {0:?} bytes from the data stream.")]
#[diagnostic(code(cat_dev::net::fsemul::sdio::data_stream::cannot_request_read))]
CannotRequestReadFromDataStream(#[from] BlockingSendError<usize>),
#[error("Cannot queue a write to the data stream for SDIO: {0:?}")]
#[diagnostic(code(cat_dev::net::fsemul::sdio::data_stream::cannot_queue_write))]
CannotQueueWriteToDataStream(#[from] BlockingSendError<Bytes>),
#[error(
"The SDIO data stream did not respond with any data, this must mean it was closed previously in error."
)]
#[diagnostic(code(cat_dev::net::fsemul::sdio::data_stream::did_not_respond))]
DataStreamDidNotRespond,
#[error(
"Packet coming in on stream: {0} did not get a data stream allocated, internal developer error?"
)]
#[diagnostic(code(cat_dev::net::fsemul::sdio::data_stream::missing))]
DataStreamMissing(u64),
#[error(
"Packet coming in on stream: {0} did not get a buffer allocated, internal developer error?"
)]
#[diagnostic(code(cat_dev::net::fsemul::sdio::printf::missing_buffer))]
PrintfMissingBuffer(u64),
}
#[derive(Diagnostic, Error, Debug, PartialEq, Eq)]
pub enum SdioProtocolError {
#[error("The SDIO client requested address: {0:02X}, but max address was: {1:02X}")]
#[diagnostic(code(cat_dev::net::parse::fsemul::sdio::address_out_of_range))]
AddressOutOfRange(u128, u128),
#[error(
"Got an invalid channel to read/write from for sdio/printf, (first byte: {0:02x} should be <0xC), (full channel: {1})"
)]
#[diagnostic(code(cat_dev::net::parse::fsemul::sdio::printf::invalid_channel))]
PrintfInvalidChannel(u8, u32),
#[error("Packet headed for SDIO PRINTF/CONTROL was size {0}, but needs to be 512 bytes")]
#[diagnostic(code(cat_dev::net::parse::fsemul::sdio::printf::invalid_sized_packet))]
PrintfInvalidSize(usize),
#[error("Got an unexpected sdio/printf packet type: {0}, not sure how to handle.")]
#[diagnostic(code(cat_dev::net::parse::fsemul::sdio::printf::unknown_packet_type))]
UnknownPrintfPacketType(u16),
#[error(
"Got an unexpected message fragment from an sdio printf packet type: {0}, not sure how to handle."
)]
#[diagnostic(code(cat_dev::net::parse::fsemul::sdio::printf::unknown_message_type))]
UnknownPrintfTelnetChannel(u16),
}