瀏覽代碼

Fix #305 Implement Debug for many types

For most types it was a simple matter of adding #[derive(Debug)] but
ParseOptions needed a manual implementation because the type of
log_syntax_violation is Option<&'a Fn(&'static str)> and Fn doesn't
implement Debug. log_syntax_violation is formatter as Some(Fn(&'static
str)) or None depending upon its value.
Raminder Singh 9 年之前
父節點
當前提交
b7ebdb34bf
共有 6 個文件被更改,包括 25 次插入8 次删除
  1. 2 2
      src/encoding.rs
  2. 4 1
      src/form_urlencoded.rs
  3. 3 1
      src/host.rs
  4. 12 1
      src/lib.rs
  5. 1 0
      src/path_segments.rs
  6. 3 3
      src/percent_encoding.rs

+ 2 - 2
src/encoding.rs

@@ -19,7 +19,7 @@ use std::borrow::Cow;
 #[cfg(feature = "query_encoding")] pub use self::encoding::types::EncodingRef;
 #[cfg(feature = "query_encoding")] pub use self::encoding::types::EncodingRef;
 
 
 #[cfg(feature = "query_encoding")]
 #[cfg(feature = "query_encoding")]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub struct EncodingOverride {
 pub struct EncodingOverride {
     /// `None` means UTF-8.
     /// `None` means UTF-8.
     encoding: Option<EncodingRef>
     encoding: Option<EncodingRef>
@@ -91,7 +91,7 @@ impl EncodingOverride {
 
 
 
 
 #[cfg(not(feature = "query_encoding"))]
 #[cfg(not(feature = "query_encoding"))]
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub struct EncodingOverride;
 pub struct EncodingOverride;
 
 
 #[cfg(not(feature = "query_encoding"))]
 #[cfg(not(feature = "query_encoding"))]

+ 4 - 1
src/form_urlencoded.rs

@@ -81,7 +81,7 @@ pub fn parse_with_encoding<'a>(input: &'a [u8],
 }
 }
 
 
 /// The return type of `parse()`.
 /// The return type of `parse()`.
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 pub struct Parse<'a> {
 pub struct Parse<'a> {
     input: &'a [u8],
     input: &'a [u8],
     encoding: EncodingOverride,
     encoding: EncodingOverride,
@@ -145,6 +145,7 @@ impl<'a> Parse<'a> {
 }
 }
 
 
 /// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`.
 /// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`.
+#[derive(Debug)]
 pub struct ParseIntoOwned<'a> {
 pub struct ParseIntoOwned<'a> {
     inner: Parse<'a>
     inner: Parse<'a>
 }
 }
@@ -168,6 +169,7 @@ pub fn byte_serialize(input: &[u8]) -> ByteSerialize {
 }
 }
 
 
 /// Return value of `byte_serialize()`.
 /// Return value of `byte_serialize()`.
+#[derive(Debug)]
 pub struct ByteSerialize<'a> {
 pub struct ByteSerialize<'a> {
     bytes: &'a [u8],
     bytes: &'a [u8],
 }
 }
@@ -209,6 +211,7 @@ impl<'a> Iterator for ByteSerialize<'a> {
 
 
 /// The [`application/x-www-form-urlencoded` serializer](
 /// The [`application/x-www-form-urlencoded` serializer](
 /// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
 /// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
+#[derive(Debug)]
 pub struct Serializer<T: Target> {
 pub struct Serializer<T: Target> {
     target: Option<T>,
     target: Option<T>,
     start_position: usize,
     start_position: usize,

+ 3 - 1
src/host.rs

@@ -176,7 +176,7 @@ impl<S: AsRef<str>> fmt::Display for Host<S> {
 
 
 /// This mostly exists because coherence rules don’t allow us to implement
 /// This mostly exists because coherence rules don’t allow us to implement
 /// `ToSocketAddrs for (Host<S>, u16)`.
 /// `ToSocketAddrs for (Host<S>, u16)`.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct HostAndPort<S=String> {
 pub struct HostAndPort<S=String> {
     pub host: Host<S>,
     pub host: Host<S>,
     pub port: u16,
     pub port: u16,
@@ -213,10 +213,12 @@ impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
 }
 }
 
 
 /// Socket addresses for an URL.
 /// Socket addresses for an URL.
+#[derive(Debug)]
 pub struct SocketAddrs {
 pub struct SocketAddrs {
     state: SocketAddrsState
     state: SocketAddrsState
 }
 }
 
 
+#[derive(Debug)]
 enum SocketAddrsState {
 enum SocketAddrsState {
     Domain(vec::IntoIter<SocketAddr>),
     Domain(vec::IntoIter<SocketAddr>),
     One(SocketAddr),
     One(SocketAddr),

+ 12 - 1
src/lib.rs

@@ -108,7 +108,7 @@ use percent_encoding::{PATH_SEGMENT_ENCODE_SET, USERINFO_ENCODE_SET,
 use std::borrow::Borrow;
 use std::borrow::Borrow;
 use std::cmp;
 use std::cmp;
 #[cfg(feature = "serde")] use std::error::Error;
 #[cfg(feature = "serde")] use std::error::Error;
-use std::fmt::{self, Write};
+use std::fmt::{self, Write, Debug, Formatter};
 use std::hash;
 use std::hash;
 use std::io;
 use std::io;
 use std::mem;
 use std::mem;
@@ -213,6 +213,16 @@ impl<'a> ParseOptions<'a> {
     }
     }
 }
 }
 
 
+impl<'a> Debug for ParseOptions<'a> {
+    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+        write!(f, "ParseOptions {{ base_url: {:?}, encoding_override: {:?}, log_syntax_violation: ", self.base_url, self.encoding_override)?;
+        match self.log_syntax_violation {
+            Some(_) => write!(f, "Some(Fn(&'static str)) }}"),
+            None => write!(f, "None }}")
+        }
+    }
+}
+
 impl Url {
 impl Url {
     /// Parse an absolute URL from a string.
     /// Parse an absolute URL from a string.
     ///
     ///
@@ -1870,6 +1880,7 @@ fn io_error<T>(reason: &str) -> io::Result<T> {
 }
 }
 
 
 /// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.
 /// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.
+#[derive(Debug)]
 pub struct UrlQuery<'a> {
 pub struct UrlQuery<'a> {
     url: &'a mut Url,
     url: &'a mut Url,
     fragment: Option<String>,
     fragment: Option<String>,

+ 1 - 0
src/path_segments.rs

@@ -26,6 +26,7 @@ use Url;
 /// url.path_segments_mut().unwrap().pop().push("img").push("2/100%.png");
 /// url.path_segments_mut().unwrap().pop().push("img").push("2/100%.png");
 /// assert_eq!(url.as_str(), "http://example.net/foo/img/2%2F100%25.png");
 /// assert_eq!(url.as_str(), "http://example.net/foo/img/2%2F100%25.png");
 /// ```
 /// ```
+#[derive(Debug)]
 pub struct PathSegmentsMut<'a> {
 pub struct PathSegmentsMut<'a> {
     url: &'a mut Url,
     url: &'a mut Url,
     after_first_slash: usize,
     after_first_slash: usize,

+ 3 - 3
src/percent_encoding.rs

@@ -77,7 +77,7 @@ macro_rules! define_encode_set {
 }
 }
 
 
 /// This encode set is used for the path of cannot-be-a-base URLs.
 /// This encode set is used for the path of cannot-be-a-base URLs.
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Debug)]
 #[allow(non_camel_case_types)]
 #[allow(non_camel_case_types)]
 pub struct SIMPLE_ENCODE_SET;
 pub struct SIMPLE_ENCODE_SET;
 
 
@@ -163,7 +163,7 @@ pub fn utf8_percent_encode<E: EncodeSet>(input: &str, encode_set: E) -> PercentE
 }
 }
 
 
 /// The return type of `percent_encode()` and `utf8_percent_encode()`.
 /// The return type of `percent_encode()` and `utf8_percent_encode()`.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct PercentEncode<'a, E: EncodeSet> {
 pub struct PercentEncode<'a, E: EncodeSet> {
     bytes: &'a [u8],
     bytes: &'a [u8],
     encode_set: E,
     encode_set: E,
@@ -249,7 +249,7 @@ pub fn percent_decode<'a>(input: &'a [u8]) -> PercentDecode<'a> {
 }
 }
 
 
 /// The return type of `percent_decode()`.
 /// The return type of `percent_decode()`.
-#[derive(Clone)]
+#[derive(Clone, Debug)]
 pub struct PercentDecode<'a> {
 pub struct PercentDecode<'a> {
     bytes: slice::Iter<'a, u8>,
     bytes: slice::Iter<'a, u8>,
 }
 }