소스 검색

Transport module refactorings

• Replace derive_more::From with a bunch of manually-derived From impls, which is annoying, but saves on compilation time
• Remove some Debug derives that weren't doing anything
• Move the transport Error enum into its own module, mostly to accommodate all the impls it now carts along with it
Benjamin Sago 4 년 전
부모
커밋
6f04825347
9개의 변경된 파일57개의 추가작업 그리고 52개의 파일을 삭제
  1. 0 12
      Cargo.lock
  2. 0 1
      dns-transport/Cargo.toml
  3. 0 1
      dns-transport/src/auto.rs
  4. 53 0
      dns-transport/src/error.rs
  5. 0 1
      dns-transport/src/https.rs
  6. 4 34
      dns-transport/src/lib.rs
  7. 0 1
      dns-transport/src/tcp.rs
  8. 0 1
      dns-transport/src/tls.rs
  9. 0 1
      dns-transport/src/udp.rs

+ 0 - 12
Cargo.lock

@@ -131,17 +131,6 @@ dependencies = [
  "winapi",
 ]
 
-[[package]]
-name = "derive_more"
-version = "0.99.11"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "41cb0e6161ad61ed084a36ba71fbba9e3ac5aee3606fb607fe08da6acbcf3d8c"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "difference"
 version = "2.0.0"
@@ -162,7 +151,6 @@ dependencies = [
 name = "dns-transport"
 version = "0.1.0"
 dependencies = [
- "derive_more",
  "dns",
  "httparse",
  "log",

+ 0 - 1
dns-transport/Cargo.toml

@@ -6,7 +6,6 @@ edition = "2018"
 
 
 [dependencies]
-derive_more = "0.99"
 
 # dns wire protocol
 dns = { path = "../dns" }

+ 0 - 1
dns-transport/src/auto.rs

@@ -32,7 +32,6 @@ use super::{Transport, Error, UdpTransport, TcpTransport};
 /// let transport = AutoTransport::new("8.8.8.8");
 /// transport.send(&request);
 /// ```
-#[derive(Debug)]
 pub struct AutoTransport {
     addr: String,
 }

+ 53 - 0
dns-transport/src/error.rs

@@ -0,0 +1,53 @@
+/// Something that can go wrong making a DNS request.
+#[derive(Debug)]
+pub enum Error {
+
+    /// There was a problem with the network sending the request or receiving
+    /// a response asynchorously.
+    NetworkError(std::io::Error),
+
+    /// There was a problem making a TLS request.
+    #[cfg(feature="tls")]
+    TlsError(native_tls::Error),
+
+    /// There was a problem _establishing_ a TLS request.
+    #[cfg(feature="tls")]
+    TlsHandshakeError(native_tls::HandshakeError<std::net::TcpStream>),
+
+    /// The data in the response did not parse correctly from the DNS wire
+    /// protocol format.
+    WireError(dns::WireError),
+
+    /// The server specifically indicated that the request we sent it was
+    /// malformed.
+    BadRequest,
+}
+
+
+// From impls
+
+impl From<dns::WireError> for Error {
+    fn from(inner: dns::WireError) -> Self {
+        Self::WireError(inner)
+    }
+}
+
+impl From<std::io::Error> for Error {
+    fn from(inner: std::io::Error) -> Error {
+        Self::NetworkError(inner)
+    }
+}
+
+#[cfg(feature="tls")]
+impl From<native_tls::Error> for Error {
+    fn from(inner: native_tls::Error) -> Error {
+        Self::TlsError(inner)
+    }
+}
+
+#[cfg(feature="tls")]
+impl From<native_tls::HandshakeError<std::net::TcpStream>> for Error {
+    fn from(inner: native_tls::HandshakeError<std::net::TcpStream>) -> Error {
+        Self::TlsHandshakeError(inner)
+    }
+}

+ 0 - 1
dns-transport/src/https.rs

@@ -34,7 +34,6 @@ use super::{Transport, Error};
 /// let transport = HttpsTransport::new("https://cloudflare-dns.com/dns-query");
 /// transport.send(&request);
 /// ```
-#[derive(Debug)]
 pub struct HttpsTransport {
     url: String,
 }

+ 4 - 34
dns-transport/src/lib.rs

@@ -23,14 +23,6 @@
 #![deny(clippy::cast_sign_loss)]
 #![deny(unsafe_code)]
 
-use derive_more::From;
-
-use dns::{Request, Response};
-
-
-// Re-export the five transport types, as well as the Tokio runtime, so that
-// the dog crate can just use something called “Runtime” without worrying
-// about which runtime it actually is.
 
 mod auto;
 pub use self::auto::AutoTransport;
@@ -47,6 +39,9 @@ pub use self::tls::TlsTransport;
 mod https;
 pub use self::https::HttpsTransport;
 
+mod error;
+pub use self::error::Error;
+
 
 /// The trait implemented by all transport types.
 pub trait Transport {
@@ -60,30 +55,5 @@ pub trait Transport {
     /// receiving data, or the DNS packet in the response contained invalid
     /// bytes and failed to parse, or if there was a protocol-level error for
     /// the TLS and HTTPS transports.
-    fn send(&self, request: &Request) -> Result<Response, Error>;
-}
-
-/// Something that can go wrong making a DNS request.
-#[derive(Debug, From)]  // can’t be PartialEq due to std::io::Error
-pub enum Error {
-
-    /// There was a problem with the network sending the request or receiving
-    /// a response asynchorously.
-    NetworkError(std::io::Error),
-
-    /// There was a problem making a TLS request.
-    #[cfg(feature="tls")]
-    TlsError(native_tls::Error),
-
-    /// There was a problem _establishing_ a TLS request.
-    #[cfg(feature="tls")]
-    TlsHandshakeError(native_tls::HandshakeError<std::net::TcpStream>),
-
-    /// The data in the response did not parse correctly from the DNS wire
-    /// protocol format.
-    WireError(dns::WireError),
-
-    /// The server specifically indicated that the request we sent it was
-    /// malformed.
-    BadRequest,
+    fn send(&self, request: &dns::Request) -> Result<dns::Response, Error>;
 }

+ 0 - 1
dns-transport/src/tcp.rs

@@ -39,7 +39,6 @@ use super::{Transport, Error};
 ///   Implementation and Specification (November 1987)
 /// - [RFC 7766](https://tools.ietf.org/html/rfc1035) — DNS Transport over
 ///   TCP, Implementation Requirements (March 2016)
-#[derive(Debug)]
 pub struct TcpTransport {
     addr: String,
 }

+ 0 - 1
dns-transport/src/tls.rs

@@ -35,7 +35,6 @@ use super::{Transport, Error};
 /// let transport = TlsTransport::new("dns.google");
 /// transport.send(&request);
 /// ```
-#[derive(Debug)]
 pub struct TlsTransport {
     addr: String,
 }

+ 0 - 1
dns-transport/src/udp.rs

@@ -30,7 +30,6 @@ use super::{Transport, Error};
 /// let transport = UdpTransport::new("8.8.8.8");
 /// transport.send(&request);
 /// ```
-#[derive(Debug)]
 pub struct UdpTransport {
     addr: String,
 }