From 07f18c7923e55981078842521abdfc40aab81186 Mon Sep 17 00:00:00 2001 From: jbesraa Date: Thu, 11 Jul 2024 13:18:04 +0300 Subject: [PATCH] Move `handshake_message_to_frame` to.. `HandShakeFrame` as `HandShake::from_message` --- protocols/v2/codec-sv2/src/lib.rs | 12 +++++++++--- protocols/v2/framing-sv2/src/framing.rs | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/protocols/v2/codec-sv2/src/lib.rs b/protocols/v2/codec-sv2/src/lib.rs index 0a2492890f..7da4ff885b 100644 --- a/protocols/v2/codec-sv2/src/lib.rs +++ b/protocols/v2/codec-sv2/src/lib.rs @@ -30,7 +30,7 @@ pub use noise_sv2::{self, Initiator, NoiseCodec, Responder}; pub use buffer_sv2; -pub use framing_sv2::{self, framing::handshake_message_to_frame as h2f}; +pub use framing_sv2::{self}; #[cfg(feature = "noise_sv2")] #[derive(Debug)] @@ -49,7 +49,10 @@ impl State { pub fn step_0(&mut self) -> core::result::Result { match self { Self::HandShake(h) => match h { - HandshakeRole::Initiator(i) => i.step_0().map_err(|e| e.into()).map(h2f), + HandshakeRole::Initiator(i) => i + .step_0() + .map_err(|e| e.into()) + .map(HandShakeFrame::from_message), HandshakeRole::Responder(_) => Err(Error::InvalidStepForResponder), }, _ => Err(Error::NotInHandShakeState), @@ -64,7 +67,10 @@ impl State { Self::HandShake(h) => match h { HandshakeRole::Responder(r) => { let (message, codec) = r.step_1(re_pub)?; - Ok((h2f(message), Self::Transport(codec))) + Ok(( + HandShakeFrame::from_message(message), + Self::Transport(codec), + )) } HandshakeRole::Initiator(_) => Err(Error::InvalidStepForInitiator), }, diff --git a/protocols/v2/framing-sv2/src/framing.rs b/protocols/v2/framing-sv2/src/framing.rs index 67da26fe32..ce7d2001d6 100644 --- a/protocols/v2/framing-sv2/src/framing.rs +++ b/protocols/v2/framing-sv2/src/framing.rs @@ -211,6 +211,16 @@ impl HandShakeFrame { Ok(Self::from_bytes_unchecked(bytes)) } + /// Returns a `HandShakeFrame` from a generic byte array + #[allow(clippy::useless_conversion)] + pub fn from_message>(message: T) -> HandShakeFrame { + let mut payload = Vec::new(); + payload.extend_from_slice(message.as_ref()); + HandShakeFrame { + payload: payload.into(), + } + } + #[inline] pub fn from_bytes_unchecked(bytes: Slice) -> Self { Self { payload: bytes } @@ -239,16 +249,6 @@ impl + AsRef<[u8]>> TryFrom> } } -/// Returns a `HandShakeFrame` from a generic byte array -#[allow(clippy::useless_conversion)] -pub fn handshake_message_to_frame>(message: T) -> HandShakeFrame { - let mut payload = Vec::new(); - payload.extend_from_slice(message.as_ref()); - HandShakeFrame { - payload: payload.into(), - } -} - /// Basically a boolean bit filter for `extension_type`. /// Takes an `extension_type` represented as a `u16` and a boolean flag (`channel_msg`). /// If `channel_msg` is true, it sets the most significant bit of `extension_type` to 1,