1
0

better error messages for AssembleError

This commit is contained in:
EAimTY 2023-01-27 00:40:06 +09:00
parent e3760f4a01
commit fc6444f86e

View File

@ -333,20 +333,26 @@ where
addr: Address, addr: Address,
data: B, data: B,
) -> Result<Option<Assemblable<B>>, AssembleError> { ) -> Result<Option<Assemblable<B>>, AssembleError> {
if data.as_ref().len() != size as usize { assert_eq!(data.as_ref().len(), size as usize);
return Err(AssembleError::InvalidFragmentSize);
}
if frag_id >= frag_total { if frag_id >= frag_total {
return Err(AssembleError::InvalidFragmentId); return Err(AssembleError::InvalidFragmentId(frag_total, frag_id));
} }
if (frag_id == 0 && addr.is_none()) || (frag_id != 0 && !addr.is_none()) { if frag_id == 0 && addr.is_none() {
return Err(AssembleError::InvalidAddress); return Err(AssembleError::InvalidAddress(
"no address in first fragment",
));
}
if frag_id != 0 && !addr.is_none() {
return Err(AssembleError::InvalidAddress(
"address in non-first fragment",
));
} }
if self.buf[frag_id as usize].is_some() { if self.buf[frag_id as usize].is_some() {
return Err(AssembleError::DuplicatedFragment); return Err(AssembleError::DuplicatedFragment(frag_id));
} }
self.buf[frag_id as usize] = Some(data); self.buf[frag_id as usize] = Some(data);
@ -417,12 +423,10 @@ where
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum AssembleError { pub enum AssembleError {
#[error("invalid fragment size")] #[error("invalid fragment id {1} in total {0} fragments")]
InvalidFragmentSize, InvalidFragmentId(u8, u8),
#[error("invalid fragment id")] #[error("{0}")]
InvalidFragmentId, InvalidAddress(&'static str),
#[error("invalid address")] #[error("duplicated fragment: {0}")]
InvalidAddress, DuplicatedFragment(u8),
#[error("duplicated fragment")]
DuplicatedFragment,
} }