From 0860b3ba20a847d7ee90022e2efc8f4fdfe3fa93 Mon Sep 17 00:00:00 2001 From: Aleksander Krauze Date: Mon, 12 Jun 2023 19:33:30 +0200 Subject: [PATCH 1/2] Implement std::error::Error for birdc::Error --- src/error.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 8e19df3..6c4ac29 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,4 @@ -use std::{num::ParseIntError, str::Utf8Error}; +use std::{fmt, num::ParseIntError, str::Utf8Error}; use super::Message; @@ -22,6 +22,43 @@ pub enum Error { ParseError(Vec), } +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Error::IoError(_) => write!(f, "IO operation failed"), + Error::ProtocolError(msg) => { + write!(f, "received an error message from server")?; + match msg { + Message::ReplyTooLong(_) => write!(f, ": reply too long"), + Message::RouteNotFound(_) => write!(f, ": route not found"), + Message::ConfigurationFileError(_) => write!(f, ": configuration file error"), + Message::NoProtocolsMatch(_) => write!(f, ": no protocols match"), + Message::StoppedDueToReconfiguration(_) => { + write!(f, ": stopped due to reconfiguration") + } + Message::ProtocolDown(_) => write!(f, ": protocol is down => connot dump"), + Message::ReloadFailed(_) => write!(f, ": reload failed"), + Message::AccessDenied(_) => write!(f, ": access denied"), + Message::RuntimeError(..) => write!(f, ": evaluation runtime error"), + _ => Ok(()), + } + } + Error::OperationInProgress => write!(f, "another request is already in progress"), + Error::InvalidToken(_) => write!(f, "received invalid token"), + Error::ParseError(_) => write!(f, "failed to parse server response"), + } + } +} + +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::IoError(err) => Some(err), + _ => None, + } + } +} + impl Error { pub fn eof(err: &str) -> Self { Self::IoError(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, err)) From 6028be14986aeff70ffb7ccc0b128cb9aa2aa00c Mon Sep 17 00:00:00 2001 From: Aleksander Krauze Date: Mon, 12 Jun 2023 20:14:35 +0200 Subject: [PATCH 2/2] Fix clippy "needless borrow" lint --- src/connection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index ea6a431..a1f4f34 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -137,7 +137,7 @@ impl Connection { // process only if we find the first entry to be a 1001 if let Some(msg_1001) = Interface::from_enum(first_msg) { // get the position of the next 1001 - let next_1001_idx = (&messages[idx..]) + let next_1001_idx = messages[idx..] .iter() .position(|x| matches!(x, Message::InterfaceList(_))) .unwrap_or(messages.len() - idx) @@ -619,7 +619,7 @@ fn parse_message(code: u32, buffer: &[u8], start_pos: usize, msg_size: usize) -> } idx += 1; - if let Some(nl_pos) = (&buffer[pos..]).iter().position(|it| *it == b'\n') { + if let Some(nl_pos) = buffer[pos..].iter().position(|it| *it == b'\n') { let src = &buffer[pos..(pos + nl_pos)]; v.extend_from_slice(src); pos += src.len() + 1;