From 7ba90e513fcd0f756f32d9a009579b67c40998ea Mon Sep 17 00:00:00 2001 From: Julius Lehmann Date: Thu, 18 Sep 2025 22:24:08 +0200 Subject: [PATCH] feat: implement Error for StunAgentError Fixes #50 --- stun-agent/src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/stun-agent/src/lib.rs b/stun-agent/src/lib.rs index 110665b..5a7b185 100644 --- a/stun-agent/src/lib.rs +++ b/stun-agent/src/lib.rs @@ -21,6 +21,8 @@ //! - [`StunClient`](`crate::StunClient`): The STUN client that sends STUN requests and indications to a STUN server. #![deny(missing_docs)] +use std::error::Error; +use std::fmt::Display; use std::{ops::Deref, slice::Iter, sync::Arc}; use stun_rs::MessageHeader; @@ -62,6 +64,30 @@ pub enum StunAgentError { InternalError(String), } +impl Display for StunAgentError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + StunAgentError::Discarded => write!(f, "STUN agent has discarded the buffer"), + StunAgentError::FingerPrintValidationFailed => { + write!(f, "STUN agent has received an invalid STUN packet") + } + StunAgentError::Ignored => write!(f, "STUN agent has ignored the operation"), + StunAgentError::MaxOutstandingRequestsReached => write!( + f, + "STUN agent has reached the maximum number of outstanding requests" + ), + StunAgentError::StunCheckFailed => { + write!(f, "STUN agent has received an invalid STUN packet") + } + StunAgentError::InternalError(msg) => { + write!(f, "STUN agent has detected an internal error: {}", msg) + } + } + } +} + +impl Error for StunAgentError {} + /// Describes the kind of integrity protection that can be used. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Integrity {