update error enum

This commit is contained in:
yggverse 2025-03-19 03:25:55 +02:00
parent ab8eb402a8
commit 3f968d87b1
2 changed files with 27 additions and 18 deletions

View file

@ -76,21 +76,22 @@ impl std::fmt::Display for Input {
impl std::str::FromStr for Input {
type Err = Error;
fn from_str(header: &str) -> Result<Self, Self::Err> {
if header.len() <= super::HEADER_LEN {
if let Some(postfix) = header.strip_prefix("10") {
return Ok(Self::Default {
header: header.to_string(),
message: message(postfix),
});
}
if let Some(postfix) = header.strip_prefix("11") {
return Ok(Self::Sensitive {
header: header.to_string(),
message: message(postfix),
});
}
if header.len() > super::HEADER_LEN {
return Err(Error::HeaderLen(header.len()));
}
Err(Error::Protocol)
if let Some(postfix) = header.strip_prefix("10") {
return Ok(Self::Default {
header: header.to_string(),
message: message(postfix),
});
}
if let Some(postfix) = header.strip_prefix("11") {
return Ok(Self::Sensitive {
header: header.to_string(),
message: message(postfix),
});
}
Err(Error::Code)
}
}

View file

@ -5,19 +5,27 @@ use std::{
#[derive(Debug)]
pub enum Error {
Protocol,
Code,
HeaderLen(usize),
Utf8Error(Utf8Error),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::Code => {
write!(f, "Unexpected status code")
}
Self::HeaderLen(l) => {
write!(
f,
"Header length reached protocol limit ({l} of {} bytes max)",
super::super::HEADER_LEN
)
}
Self::Utf8Error(e) => {
write!(f, "UTF-8 error: {e}")
}
Self::Protocol => {
write!(f, "Protocol error")
}
}
}
}