diff --git a/s2energy-connection/src/pairing/client.rs b/s2energy-connection/src/pairing/client.rs index 779d5a7..f49dfe5 100644 --- a/s2energy-connection/src/pairing/client.rs +++ b/s2energy-connection/src/pairing/client.rs @@ -201,18 +201,7 @@ impl<'a> V1Session<'a> { if response.status() == StatusCode::BAD_REQUEST { let response_error: PairingResponseErrorMessage = response.json().await.map_err(|e| Error::new(ErrorKind::ProtocolError, e))?; - return Err(Error::new( - match response_error { - PairingResponseErrorMessage::InvalidCombinationOfRoles => ErrorKind::RemoteOfSameType, - PairingResponseErrorMessage::IncompatibleS2MessageVersions - | PairingResponseErrorMessage::IncompatibleHMACHashingAlgorithms - | PairingResponseErrorMessage::IncompatibleCommunicationProtocols => ErrorKind::NoSupportedVersion, - PairingResponseErrorMessage::S2NodeNotFound | PairingResponseErrorMessage::S2NodeNotProvided => ErrorKind::UnknownNode, - PairingResponseErrorMessage::InvalidPairingToken => ErrorKind::InvalidToken, - PairingResponseErrorMessage::ParsingError | PairingResponseErrorMessage::Other => ErrorKind::ProtocolError, - }, - response_error, - )); + return Err(Error::from(response_error)); } if response.status() != StatusCode::NO_CONTENT { return Err(ErrorKind::ProtocolError.into()); @@ -434,23 +423,7 @@ impl<'a> V1Session<'a> { .json::() .await .map_err(|e| Error::new(ErrorKind::ProtocolError, e))?; - match error_response { - PairingResponseErrorMessage::InvalidCombinationOfRoles => { - return Err(Error::new(ErrorKind::RemoteOfSameType, error_response)); - } - PairingResponseErrorMessage::IncompatibleS2MessageVersions - | PairingResponseErrorMessage::IncompatibleHMACHashingAlgorithms - | PairingResponseErrorMessage::IncompatibleCommunicationProtocols => { - return Err(Error::new(ErrorKind::NoSupportedVersion, error_response)); - } - PairingResponseErrorMessage::S2NodeNotFound | PairingResponseErrorMessage::S2NodeNotProvided => { - return Err(Error::new(ErrorKind::UnknownNode, error_response)); - } - PairingResponseErrorMessage::InvalidPairingToken => return Err(Error::new(ErrorKind::InvalidToken, error_response)), - PairingResponseErrorMessage::ParsingError | PairingResponseErrorMessage::Other => { - return Err(Error::new(ErrorKind::ProtocolError, error_response)); - } - } + return Err(Error::from(error_response)); } if response.status() != StatusCode::OK { debug!(status = ?response.status(), "Unexpected status code in response to requestPairing."); diff --git a/s2energy-connection/src/pairing/server.rs b/s2energy-connection/src/pairing/server.rs index 4ce7ee7..ef9d5c5 100644 --- a/s2energy-connection/src/pairing/server.rs +++ b/s2energy-connection/src/pairing/server.rs @@ -151,27 +151,27 @@ pub enum PrePairingResponse { /// Handler for pre-pairing requests. /// -/// This allows notification of other components when a prepairing request +/// This allows notification of other components when a pre-pairing request /// comes in. The methods of this trait are called during request handling, /// and should thus return rapidly. -// -// Note: As these methods are called during request handling, we conciously -// make them not async to encourage not doing long-lasting operations. +// Note: Because these methods are called during request handling, we +// deliberately make them not async to encourage not doing long-lasting +// operations. pub trait PrePairingHandler: Send + Sync + 'static { - /// Handle a request for prepairing, and indicate our willingness. + /// Handle a request for pre-pairing, and indicate our willingness. fn prepairing_requested( &self, endpoint: S2EndpointDescription, node: S2NodeDescription, target_node: Option, ) -> PrePairingResponse; - /// Handle a cancel event for prepairing. Note that not every pre-pairing + /// Handle a cancel event for pre-pairing. Note that not every pre-pairing /// request will result in a cancel or a pairing interaction, so timeouts /// may be needed. fn prepairing_cancelled(&self, id: S2NodeId, target_node: Option); } -/// A pre-pairing handler that does nothing on receiving a prepairing request. +/// A pre-pairing handler that does nothing on receiving a pre-pairing request. /// /// The requests will always indicate willingness to pair to the client. pub struct NoopPrePairingHandler; diff --git a/s2energy-connection/src/pairing/wire.rs b/s2energy-connection/src/pairing/wire.rs index 66ef0fb..c2daf7d 100644 --- a/s2energy-connection/src/pairing/wire.rs +++ b/s2energy-connection/src/pairing/wire.rs @@ -12,7 +12,7 @@ use crate::{ common::wire::{AccessToken, CommunicationProtocol, MessageVersion, S2EndpointDescription, S2NodeDescription}, }; -#[derive(Error, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[derive(Error, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy)] pub(crate) enum PairingResponseErrorMessage { #[error("Invalid combination of roles")] InvalidCombinationOfRoles, @@ -34,6 +34,25 @@ pub(crate) enum PairingResponseErrorMessage { Other, } +impl From for super::Error { + fn from(value: PairingResponseErrorMessage) -> Self { + use super::ErrorKind; + use PairingResponseErrorMessage::*; + + let error_kind = match value { + InvalidCombinationOfRoles => ErrorKind::RemoteOfSameType, + IncompatibleS2MessageVersions | IncompatibleHMACHashingAlgorithms | IncompatibleCommunicationProtocols => { + ErrorKind::NoSupportedVersion + } + S2NodeNotFound | S2NodeNotProvided => ErrorKind::UnknownNode, + InvalidPairingToken => ErrorKind::InvalidToken, + ParsingError | PairingResponseErrorMessage::Other => ErrorKind::ProtocolError, + }; + + super::Error::new(error_kind, value) + } +} + impl IntoResponse for PairingResponseErrorMessage { fn into_response(self) -> axum::response::Response { (StatusCode::BAD_REQUEST, Json(self)).into_response()