mirror of
https://github.com/YGGverse/ggemini.git
synced 2026-03-31 09:05:45 +00:00
update error enum
This commit is contained in:
parent
ab8eb402a8
commit
3f968d87b1
2 changed files with 27 additions and 18 deletions
|
|
@ -76,21 +76,22 @@ impl std::fmt::Display for Input {
|
||||||
impl std::str::FromStr for Input {
|
impl std::str::FromStr for Input {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
fn from_str(header: &str) -> Result<Self, Self::Err> {
|
fn from_str(header: &str) -> Result<Self, Self::Err> {
|
||||||
if header.len() <= super::HEADER_LEN {
|
if header.len() > super::HEADER_LEN {
|
||||||
if let Some(postfix) = header.strip_prefix("10") {
|
return Err(Error::HeaderLen(header.len()));
|
||||||
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::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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,27 @@ use std::{
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
Protocol,
|
Code,
|
||||||
|
HeaderLen(usize),
|
||||||
Utf8Error(Utf8Error),
|
Utf8Error(Utf8Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||||
match self {
|
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) => {
|
Self::Utf8Error(e) => {
|
||||||
write!(f, "UTF-8 error: {e}")
|
write!(f, "UTF-8 error: {e}")
|
||||||
}
|
}
|
||||||
Self::Protocol => {
|
|
||||||
write!(f, "Protocol error")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue