diff --git a/Cargo.lock b/Cargo.lock index 2308a19..6272f0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,6 +115,8 @@ dependencies = [ [[package]] name = "kaminari" version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e61016796e12b5c49fae513e18b92b6cd722f6ad5b0f2c65efbf6fb7b98897a7" dependencies = [ "lazy_static", "lightws", @@ -127,9 +129,7 @@ dependencies = [ [[package]] name = "kaminari" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e61016796e12b5c49fae513e18b92b6cd722f6ad5b0f2c65efbf6fb7b98897a7" +version = "0.7.1" dependencies = [ "lazy_static", "lightws", @@ -145,7 +145,7 @@ name = "kaminari-cmd" version = "0.5.0" dependencies = [ "anyhow", - "kaminari 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "kaminari 0.7.0", "realm_io", "tokio", ] diff --git a/kaminari/Cargo.toml b/kaminari/Cargo.toml index 7289131..d0c4c5f 100644 --- a/kaminari/Cargo.toml +++ b/kaminari/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kaminari" -version = "0.7.0" +version = "0.7.1" edition = "2021" authors = ["zephyr "] keywords = ["lightws", "network"] diff --git a/kaminari/src/mix.rs b/kaminari/src/mix.rs index fa399fe..2c8dbbe 100644 --- a/kaminari/src/mix.rs +++ b/kaminari/src/mix.rs @@ -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}; @@ -253,3 +254,73 @@ impl_type_cast!( [as_tls :: Tls => TlsAccept], [as_wss :: Wss => WsAccept>], ); + +// ========== 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); + } +} diff --git a/kaminari/src/nop.rs b/kaminari/src/nop.rs index c63650d..6a3fd45 100644 --- a/kaminari/src/nop.rs +++ b/kaminari/src/nop.rs @@ -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 AsyncConnect for NopConnect where S: IOStream, diff --git a/kaminari/src/tls.rs b/kaminari/src/tls.rs index d684a56..912c7a8 100644 --- a/kaminari/src/tls.rs +++ b/kaminari/src/tls.rs @@ -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}; @@ -21,6 +22,16 @@ 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 { conn: T, @@ -28,6 +39,13 @@ pub struct TlsConnect { cc: TlsConnector, } +impl Display for TlsConnect +where + T: Display, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[tls]{}", self.conn) } +} + impl TlsConnect { pub fn new(conn: T, conf: TlsClientConf) -> Self { let TlsClientConf { @@ -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 { lis: T, ac: TlsAcceptor, } +impl Display for TlsAccept +where + T: Display, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[tls]{}", self.lis) } +} + impl TlsAccept { pub fn new(lis: T, conf: TlsServerConf) -> Self { let TlsServerConf { diff --git a/kaminari/src/ws.rs b/kaminari/src/ws.rs index 697e10b..0970bcd 100644 --- a/kaminari/src/ws.rs +++ b/kaminari/src/ws.rs @@ -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}; @@ -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 {} @@ -46,13 +53,20 @@ impl Mode for Fixed { type ClientType = FixedMaskClient; } -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct WsConnect { conn: T, conf: WsConf, _marker: PhantomData, } +impl Display for WsConnect +where + T: Display, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[ws]{}", self.conn) } +} + impl WsConnect { #[inline] pub const fn new(conn: T, conf: WsConf) -> Self { @@ -115,6 +129,13 @@ pub struct WsAccept { conf: WsConf, } +impl Display for WsAccept +where + T: Display, +{ + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "[ws]{}", self.lis) } +} + impl WsAccept { #[inline] pub const fn new(lis: T, conf: WsConf) -> Self { Self { lis, conf } }