Skip to content

Commit

Permalink
add formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 27, 2022
1 parent 53f4182 commit 0625537
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kaminari/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "kaminari"
version = "0.7.0"
version = "0.7.1"
edition = "2021"
authors = ["zephyr <[email protected]>"]
keywords = ["lightws", "network"]
Expand Down
71 changes: 71 additions & 0 deletions kaminari/src/mix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io::Result;
use std::future::Future;
use std::fmt::{Display, Formatter};

use super::{IOStream, AsyncAccept, AsyncConnect};
use super::nop::{NopAccept, NopConnect};
Expand Down Expand Up @@ -253,3 +254,73 @@ impl_type_cast!(
[as_tls :: Tls => TlsAccept<NopAccept>],
[as_wss :: Wss => WsAccept<TlsAccept<NopAccept>>],
);

// ========== display ==========

macro_rules! impl_display {
($mix: ident || $($member: ident,)+ ) => {
impl Display for $mix {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use $mix::*;
match self {
$(
$member(x) => write!(f, "{}", x),
)+
}
}
}
};
}

impl_display!(MixConnect || Plain, Ws, Tls, Wss,);
impl_display!(MixAccept || Plain, Ws, Tls, Wss,);

#[cfg(test)]
mod test {
use super::*;

#[test]
fn print_conn() {
let conf = MixClientConf {
ws: Some(WsConf {
host: String::from("abc"),
path: String::from("chat"),
}),
tls: Some(TlsClientConf {
sni: String::from("abc"),
insecure: true,
early_data: true,
}),
};

println!("ws: {}", conf.clone().ws.unwrap());
println!("tls: {}", conf.clone().tls.unwrap());

let conn = MixConnect::new(conf);

println!("{}", conn);
}

#[test]
fn print_lis() {
let conf = MixServerConf {
ws: Some(WsConf {
host: String::from("abc"),
path: String::from("chat"),
}),
tls: Some(TlsServerConf {
crt: String::new(),
key: String::new(),
ocsp: String::new(),
server_name: String::from("abc"),
}),
};

println!("ws: {}", conf.clone().ws.unwrap());
println!("tls: {}", conf.clone().tls.unwrap());

let lis = MixAccept::new(conf);

println!("{}", lis);
}
}
13 changes: 11 additions & 2 deletions kaminari/src/nop.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
use std::io::Result;
use std::future::Future;
use std::fmt::{Display, Formatter};

use super::{IOStream, AsyncAccept, AsyncConnect};

#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct NopConnect {}

#[derive(Clone, Copy)]
#[derive(Debug, Clone, Copy)]
pub struct NopAccept {}

impl Display for NopConnect {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[plain]") }
}

impl Display for NopAccept {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[plain]") }
}

impl<S> AsyncConnect<S> for NopConnect
where
S: IOStream,
Expand Down
35 changes: 35 additions & 0 deletions kaminari/src/tls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Result;
use std::future::Future;
use std::sync::Arc;
use std::fmt::{Display, Formatter};

use super::{IOStream, AsyncAccept, AsyncConnect};

Expand All @@ -21,13 +22,30 @@ pub struct TlsClientConf {
pub early_data: bool,
}

impl Display for TlsClientConf {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"sni: {}, insecure: {}, early_data: {}",
self.sni, self.insecure, self.early_data
)
}
}

#[derive(Clone)]
pub struct TlsConnect<T> {
conn: T,
sni: ServerName,
cc: TlsConnector,
}

impl<T> Display for TlsConnect<T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[tls]{}", self.conn) }
}

impl<T> TlsConnect<T> {
pub fn new(conn: T, conf: TlsClientConf) -> Self {
let TlsClientConf {
Expand Down Expand Up @@ -110,12 +128,29 @@ pub struct TlsServerConf {
pub server_name: String,
}

impl Display for TlsServerConf {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"cert: {}, key: {}, oscp: {}, server_name: {}",
self.crt, self.key, self.ocsp, self.server_name
)
}
}

#[derive(Clone)]
pub struct TlsAccept<T> {
lis: T,
ac: TlsAcceptor,
}

impl<T> Display for TlsAccept<T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[tls]{}", self.lis) }
}

impl<T> TlsAccept<T> {
pub fn new(lis: T, conf: TlsServerConf) -> Self {
let TlsServerConf {
Expand Down
23 changes: 22 additions & 1 deletion kaminari/src/ws.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Result;
use std::future::Future;
use std::marker::PhantomData;
use std::fmt::{Display, Formatter};

use super::{IOStream, AsyncAccept, AsyncConnect};

Expand All @@ -20,6 +21,12 @@ pub struct WsConf {
pub path: String,
}

impl Display for WsConf {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "host: {}, path: {}", self.host, self.path)
}
}

// =========== client ==========
#[derive(Clone, Copy)]
pub struct Simple {}
Expand All @@ -46,13 +53,20 @@ impl Mode for Fixed {
type ClientType = FixedMaskClient;
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct WsConnect<T, M = Simple> {
conn: T,
conf: WsConf,
_marker: PhantomData<M>,
}

impl<T, M> Display for WsConnect<T, M>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[ws]{}", self.conn) }
}

impl<T> WsConnect<T> {
#[inline]
pub const fn new(conn: T, conf: WsConf) -> Self {
Expand Down Expand Up @@ -115,6 +129,13 @@ pub struct WsAccept<T> {
conf: WsConf,
}

impl<T> Display for WsAccept<T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[ws]{}", self.lis) }
}

impl<T> WsAccept<T> {
#[inline]
pub const fn new(lis: T, conf: WsConf) -> Self { Self { lis, conf } }
Expand Down

0 comments on commit 0625537

Please sign in to comment.