refactor!: move modules to sensible parents

This commit is contained in:
sevki 2024-03-30 00:37:46 +00:00
parent 6e0720a822
commit 4eba5fb010
7 changed files with 58 additions and 54 deletions

View file

@ -46,7 +46,7 @@
extern crate jetstream_wire_format_derive;
#[cfg(feature = "async")]
pub mod async_wire_format;
pub mod wire_format_extensions;
#[cfg(feature = "client")]
pub mod client;
#[cfg(feature = "filesystem")]

View file

@ -9,7 +9,7 @@ use s2n_quic::{
use slog_scope::{debug, error};
use crate::async_wire_format::AsyncWireFormatExt;
use crate::wire_format_extensions::AsyncWireFormatExt;
#[derive(Debug, Clone)]
/// Represents a DialQuic struct.

View file

@ -1,6 +1,6 @@
use s2n_quic::Server;
use crate::async_wire_format::AsyncWireFormatExt;
use crate::wire_format_extensions::AsyncWireFormatExt;
use slog_scope::{debug, error};

View file

@ -1,7 +1,7 @@
#[cfg(test)]
mod tests {
use crate::{
async_wire_format::AsyncWireFormatExt,
wire_format_extensions::AsyncWireFormatExt,
log::{drain, setup_logging},
server::{
proxy::{DialQuic, Proxy},

View file

@ -4,7 +4,6 @@ use std::{
};
use crate::coding::{Rframe, Tframe, WireFormat};
use bytes::{BufMut, Bytes, BytesMut};
use futures::prelude::*;
/// Message trait for JetStream messages, which need to implement the `WireFormat` trait.
@ -59,51 +58,3 @@ impl<S: NinePService> JetStreamService<Tframe, Rframe> for NinePServiceImpl<S> {
self.inner.call(req)
}
}
/// A trait for converting types to and from a wire format.
pub trait ConvertWireFormat: WireFormat {
/// Converts the type to a byte representation.
///
/// # Returns
///
/// A `Bytes` object representing the byte representation of the type.
fn to_bytes(&self) -> Bytes;
/// Converts a byte buffer to the type.
///
/// # Arguments
///
/// * `buf` - A mutable reference to a `Bytes` object containing the byte buffer.
///
/// # Returns
///
/// A `Result` containing the converted type or an `std::io::Error` if the conversion fails.
fn from_bytes(buf: &mut Bytes) -> Result<Self, std::io::Error>;
}
/// Implements the `ConvertWireFormat` trait for types that implement `jetstream_p9::WireFormat`.
/// This trait provides methods for converting the type to and from bytes.
impl<T> ConvertWireFormat for T
where
T: WireFormat,
{
/// Converts the type to bytes.
/// Returns a `Bytes` object containing the encoded bytes.
fn to_bytes(&self) -> Bytes {
let mut buf = vec![];
let res = self.encode(&mut buf);
if let Err(e) = res {
panic!("Failed to encode: {}", e);
}
let mut bytes = BytesMut::new();
bytes.put_slice(buf.as_slice());
bytes.freeze()
}
/// Converts bytes to the type.
/// Returns a `Result` containing the decoded type or an `std::io::Error` if decoding fails.
fn from_bytes(buf: &mut Bytes) -> Result<Self, std::io::Error> {
let buf = buf.to_vec();
T::decode(&mut buf.as_slice())
}
}

View file

@ -3,6 +3,8 @@ use std::{
io::{self},
};
use bytes::{BufMut, Bytes, BytesMut};
use crate::coding::WireFormat;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
@ -67,6 +69,57 @@ where
/// Implements the `AsyncWireFormatExt` trait for types that implement the `WireFormat` trait and can be sent across threads.
impl<T: WireFormat + Send> AsyncWireFormatExt for T {}
/// A trait for converting types to and from a wire format.
pub trait ConvertWireFormat: WireFormat {
/// Converts the type to a byte representation.
///
/// # Returns
///
/// A `Bytes` object representing the byte representation of the type.
fn to_bytes(&self) -> Bytes;
/// Converts a byte buffer to the type.
///
/// # Arguments
///
/// * `buf` - A mutable reference to a `Bytes` object containing the byte buffer.
///
/// # Returns
///
/// A `Result` containing the converted type or an `std::io::Error` if the conversion fails.
fn from_bytes(buf: &mut Bytes) -> Result<Self, std::io::Error>;
}
/// Implements the `ConvertWireFormat` trait for types that implement `jetstream_p9::WireFormat`.
/// This trait provides methods for converting the type to and from bytes.
impl<T> ConvertWireFormat for T
where
T: WireFormat,
{
/// Converts the type to bytes.
/// Returns a `Bytes` object containing the encoded bytes.
fn to_bytes(&self) -> Bytes {
let mut buf = vec![];
let res = self.encode(&mut buf);
if let Err(e) = res {
panic!("Failed to encode: {}", e);
}
let mut bytes = BytesMut::new();
bytes.put_slice(buf.as_slice());
bytes.freeze()
}
/// Converts bytes to the type.
/// Returns a `Result` containing the decoded type or an `std::io::Error` if decoding fails.
fn from_bytes(buf: &mut Bytes) -> Result<Self, std::io::Error> {
let buf = buf.to_vec();
T::decode(&mut buf.as_slice())
}
}
// tests
mod tests {
use std::{pin::Pin, time::Duration};

View file

@ -497,7 +497,7 @@ pub fn protocol_inner(
mod #module_name {
pub use async_trait::async_trait;
use std::io;
pub use jetstream::{Message, WireFormat, JetStreamWireFormat, async_wire_format::AsyncWireFormatExt};
pub use jetstream::{Message, WireFormat, JetStreamWireFormat, wire_format_extensions::AsyncWireFormatExt};
pub use std::mem;
pub use std::io::{Read, Write, ErrorKind};
pub use std::future::Future;