From 3f968d87b19a386b4e980fb86514760e51568c69 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 19 Mar 2025 03:25:55 +0200 Subject: [PATCH] update error enum --- src/client/connection/response/input.rs | 29 ++++++++++--------- src/client/connection/response/input/error.rs | 16 +++++++--- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/client/connection/response/input.rs b/src/client/connection/response/input.rs index 37aa99f..78aa3a1 100644 --- a/src/client/connection/response/input.rs +++ b/src/client/connection/response/input.rs @@ -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 { - 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) } } diff --git a/src/client/connection/response/input/error.rs b/src/client/connection/response/input/error.rs index ae589e8..62f17d1 100644 --- a/src/client/connection/response/input/error.rs +++ b/src/client/connection/response/input/error.rs @@ -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") - } } } }