فهرست منبع

chore: refactor

Yujia Qiao 4 سال پیش
والد
کامیت
4c08779ff6
3فایلهای تغییر یافته به همراه10 افزوده شده و 28 حذف شده
  1. 2 6
      src/cli.rs
  2. 7 16
      src/client.rs
  3. 1 6
      src/transport/tls.rs

+ 2 - 6
src/cli.rs

@@ -8,12 +8,8 @@ pub enum KeypairType {
 }
 
 lazy_static! {
-    static ref VERSION: &'static str = {
-        match option_env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT") {
-            Some(v) => v,
-            None => env!("VERGEN_BUILD_SEMVER"),
-        }
-    };
+    static ref VERSION: &'static str =
+        option_env!("VERGEN_GIT_SEMVER_LIGHTWEIGHT").unwrap_or(env!("VERGEN_BUILD_SEMVER"));
     static ref LONG_VERSION: String = format!(
         "
 Build Timestamp:     {}

+ 7 - 16
src/client.rs

@@ -33,12 +33,9 @@ pub async fn run_client(
     shutdown_rx: broadcast::Receiver<bool>,
     service_rx: mpsc::Receiver<ServiceChange>,
 ) -> Result<()> {
-    let config = match &config.client {
-        Some(v) => v,
-        None => {
-            return Err(anyhow!("Try to run as a client, but the configuration is missing. Please add the `[client]` block"))
-        }
-    };
+    let config = config.client.as_ref().ok_or(anyhow!(
+        "Try to run as a client, but the configuration is missing. Please add the `[client]` block"
+    ))?;
 
     match config.transport.transport_type {
         TransportType::Tcp => {
@@ -169,19 +166,11 @@ async fn do_data_channel_handshake<T: Transport>(
     let mut conn: T::Stream = retry_notify(
         backoff,
         || async {
-            match args
-                .connector
+            args.connector
                 .connect(&args.remote_addr)
                 .await
                 .with_context(|| format!("Failed to connect to {}", &args.remote_addr))
                 .map_err(backoff::Error::transient)
-            {
-                Ok(conn) => {
-                    T::hint(&conn, args.socket_opts);
-                    Ok(conn)
-                }
-                Err(e) => Err(e),
-            }
         },
         |e, duration| {
             warn!("{:#}. Retry in {:?}", e, duration);
@@ -189,6 +178,8 @@ async fn do_data_channel_handshake<T: Transport>(
     )
     .await?;
 
+    T::hint(&conn, args.socket_opts);
+
     // Send nonce
     let v: &[u8; HASH_WIDTH_IN_BYTES] = args.session_key[..].try_into().unwrap();
     let hello = Hello::DataChannelHello(CURRENT_PROTO_VERSION, v.to_owned());
@@ -346,7 +337,7 @@ async fn run_udp_forwarder(
             val = s.recv(&mut buf) => {
                 let len = match val {
                     Ok(v) => v,
-                    Err(_) => {break;}
+                    Err(_) => break
                 };
 
                 let t = UdpTraffic{

+ 1 - 6
src/transport/tls.rs

@@ -25,12 +25,7 @@ impl Transport for TlsTransport {
 
     fn new(config: &TransportConfig) -> Result<Self> {
         let tcp = TcpTransport::new(config)?;
-        let config = match &config.tls {
-            Some(v) => v,
-            None => {
-                return Err(anyhow!("Missing tls config"));
-            }
-        };
+        let config = config.tls.as_ref().ok_or(anyhow!("Missing tls config"))?;
 
         let connector = match config.trusted_root.as_ref() {
             Some(path) => {