Skip to content

Commit

Permalink
Rename framing::EitherFrame to ::Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa authored and plebhash committed Jul 5, 2024
1 parent 4368dbd commit 5a2b01b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
13 changes: 5 additions & 8 deletions protocols/v2/codec-sv2/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use framing_sv2::framing::HandShakeFrame;
#[cfg(feature = "noise_sv2")]
use framing_sv2::header::{NOISE_HEADER_ENCRYPTED_SIZE, NOISE_HEADER_SIZE};
use framing_sv2::{
framing::{EitherFrame, Sv2Frame},
framing::{Frame, Sv2Frame},
header::Header,
};
#[cfg(feature = "noise_sv2")]
Expand All @@ -36,7 +36,7 @@ use crate::State;

#[cfg(feature = "noise_sv2")]
pub type StandardNoiseDecoder<T> = WithNoise<Buffer, T>;
pub type StandardEitherFrame<T> = EitherFrame<T, <Buffer as IsBuffer>::Slice>;
pub type StandardEitherFrame<T> = Frame<T, <Buffer as IsBuffer>::Slice>;
pub type StandardSv2Frame<T> = Sv2Frame<T, <Buffer as IsBuffer>::Slice>;
pub type StandardDecoder<T> = WithoutNoise<Buffer, T>;

Expand All @@ -51,7 +51,7 @@ pub struct WithNoise<B: IsBuffer, T: Serialize + binary_sv2::GetSize> {
#[cfg(feature = "noise_sv2")]
impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> WithNoise<B, T> {
#[inline]
pub fn next_frame(&mut self, state: &mut State) -> Result<EitherFrame<T, B::Slice>> {
pub fn next_frame(&mut self, state: &mut State) -> Result<Frame<T, B::Slice>> {
match state {
State::HandShake(_) => unreachable!(),
State::NotInitialized(msg_len) => {
Expand Down Expand Up @@ -97,10 +97,7 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}

#[inline]
fn decode_noise_frame(
&mut self,
noise_codec: &mut NoiseCodec,
) -> Result<EitherFrame<T, B::Slice>> {
fn decode_noise_frame(&mut self, noise_codec: &mut NoiseCodec) -> Result<Frame<T, B::Slice>> {
match (
IsBuffer::len(&self.noise_buffer),
IsBuffer::len(&self.sv2_buffer),
Expand Down Expand Up @@ -148,7 +145,7 @@ impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: IsBuffer + AeadBuffer> Wit
}
}

fn while_handshaking(&mut self) -> EitherFrame<T, B::Slice> {
fn while_handshaking(&mut self) -> Frame<T, B::Slice> {
let src = self.noise_buffer.get_data_owned().as_mut().to_vec();

// below is inffalible as noise frame length has been already checked
Expand Down
4 changes: 2 additions & 2 deletions protocols/v2/codec-sv2/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::convert::TryInto;
use core::marker::PhantomData;
use framing_sv2::framing::Sv2Frame;
#[cfg(feature = "noise_sv2")]
use framing_sv2::framing::{EitherFrame, HandShakeFrame};
use framing_sv2::framing::{Frame, HandShakeFrame};
#[allow(unused_imports)]
pub use framing_sv2::header::NOISE_HEADER_ENCRYPTED_SIZE;

Expand Down Expand Up @@ -43,7 +43,7 @@ pub struct NoiseEncoder<T: Serialize + binary_sv2::GetSize> {
}

#[cfg(feature = "noise_sv2")]
type Item<T> = EitherFrame<T, Slice>;
type Item<T> = Frame<T, Slice>;

#[cfg(feature = "noise_sv2")]
impl<T: Serialize + GetSize> NoiseEncoder<T> {
Expand Down
26 changes: 12 additions & 14 deletions protocols/v2/framing-sv2/src/framing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ type Slice = buffer_sv2::Slice;
/// A wrapper to be used in a context we need a generic reference to a frame
/// but it doesn't matter which kind of frame it is (`Sv2Frame` or `HandShakeFrame`)
#[derive(Debug)]
pub enum EitherFrame<T, B> {
pub enum Frame<T, B> {
HandShake(HandShakeFrame),
Sv2(Sv2Frame<T, B>),
}

impl<T: Serialize + GetSize, B: AsMut<[u8]> + AsRef<[u8]>> EitherFrame<T, B> {
impl<T: Serialize + GetSize, B: AsMut<[u8]> + AsRef<[u8]>> Frame<T, B> {
pub fn encoded_length(&self) -> usize {
match &self {
Self::HandShake(frame) => frame.encoded_length(),
Expand All @@ -26,13 +26,13 @@ impl<T: Serialize + GetSize, B: AsMut<[u8]> + AsRef<[u8]>> EitherFrame<T, B> {
}
}

impl<T, B> From<HandShakeFrame> for EitherFrame<T, B> {
impl<T, B> From<HandShakeFrame> for Frame<T, B> {
fn from(v: HandShakeFrame) -> Self {
Self::HandShake(v)
}
}

impl<T, B> From<Sv2Frame<T, B>> for EitherFrame<T, B> {
impl<T, B> From<Sv2Frame<T, B>> for Frame<T, B> {
fn from(v: Sv2Frame<T, B>) -> Self {
Self::Sv2(v)
}
Expand Down Expand Up @@ -190,18 +190,17 @@ impl<A, B> Sv2Frame<A, B> {
}
}

impl<T, B> TryFrom<EitherFrame<T, B>> for Sv2Frame<T, B> {
impl<T, B> TryFrom<Frame<T, B>> for Sv2Frame<T, B> {
type Error = Error;

fn try_from(v: EitherFrame<T, B>) -> Result<Self, Error> {
fn try_from(v: Frame<T, B>) -> Result<Self, Error> {
match v {
EitherFrame::Sv2(frame) => Ok(frame),
EitherFrame::HandShake(_) => Err(Error::ExpectedSv2Frame),
Frame::Sv2(frame) => Ok(frame),
Frame::HandShake(_) => Err(Error::ExpectedSv2Frame),
}
}
}


/// Abstraction for a Noise Handshake Frame
/// Contains only a `Slice` payload with a fixed length
/// Only used during Noise Handshake process
Expand Down Expand Up @@ -233,18 +232,17 @@ impl HandShakeFrame {
}
}

impl<T, B> TryFrom<EitherFrame<T, B>> for HandShakeFrame {
impl<T, B> TryFrom<Frame<T, B>> for HandShakeFrame {
type Error = Error;

fn try_from(v: EitherFrame<T, B>) -> Result<Self, Error> {
fn try_from(v: Frame<T, B>) -> Result<Self, Error> {
match v {
EitherFrame::HandShake(frame) => Ok(frame),
EitherFrame::Sv2(_) => Err(Error::ExpectedHandshakeFrame),
Frame::HandShake(frame) => Ok(frame),
Frame::Sv2(_) => Err(Error::ExpectedHandshakeFrame),
}
}
}


/// Returns a `HandShakeFrame` from a generic byte array
#[allow(clippy::useless_conversion)]
pub fn handshake_message_to_frame<T: AsRef<[u8]>>(message: T) -> HandShakeFrame {
Expand Down

0 comments on commit 5a2b01b

Please sign in to comment.