Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #305 Implement Debug for many types #335

Merged
merged 6 commits into from
Jun 13, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#[cfg(feature = "query_encoding")] extern crate encoding;

use std::borrow::Cow;
#[cfg(feature = "query_encoding")] use std::fmt::{self, Debug, Formatter};

#[cfg(feature = "query_encoding")] use self::encoding::types::{DecoderTrap, EncoderTrap};
#[cfg(feature = "query_encoding")] use self::encoding::label::encoding_from_whatwg_label;
Expand Down Expand Up @@ -89,9 +90,19 @@ impl EncodingOverride {
}
}

#[cfg(feature = "query_encoding")]
impl Debug for EncodingOverride {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just derive Debug on the structs compiled for each feature.

Copy link
Contributor Author

@imor imor May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't derive Debug in this case because encoding field has type Option<EncodingRef> and EncodingRef trait isn't Debug

fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "EncodingOverride {{ encoding: ")?;
match self.encoding {
Some(_) => write!(f, "Some(EncodingOverride) }}"),
None => write!(f, "None }}")
}
}
}

#[cfg(not(feature = "query_encoding"))]
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub struct EncodingOverride;

#[cfg(not(feature = "query_encoding"))]
Expand Down
5 changes: 4 additions & 1 deletion src/form_urlencoded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub fn parse_with_encoding<'a>(input: &'a [u8],
}

/// The return type of `parse()`.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
pub struct Parse<'a> {
input: &'a [u8],
encoding: EncodingOverride,
Expand Down Expand Up @@ -145,6 +145,7 @@ impl<'a> Parse<'a> {
}

/// Like `Parse`, but yields pairs of `String` instead of pairs of `Cow<str>`.
#[derive(Debug)]
pub struct ParseIntoOwned<'a> {
inner: Parse<'a>
}
Expand All @@ -168,6 +169,7 @@ pub fn byte_serialize(input: &[u8]) -> ByteSerialize {
}

/// Return value of `byte_serialize()`.
#[derive(Debug)]
pub struct ByteSerialize<'a> {
bytes: &'a [u8],
}
Expand Down Expand Up @@ -209,6 +211,7 @@ impl<'a> Iterator for ByteSerialize<'a> {

/// The [`application/x-www-form-urlencoded` serializer](
/// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
#[derive(Debug)]
pub struct Serializer<T: Target> {
target: Option<T>,
start_position: usize,
Expand Down
4 changes: 3 additions & 1 deletion src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// `ToSocketAddrs for (Host<S>, u16)`.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct HostAndPort<S=String> {
pub host: Host<S>,
pub port: u16,
Expand Down Expand Up @@ -213,10 +213,12 @@ impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
}

/// Socket addresses for an URL.
#[derive(Debug)]
pub struct SocketAddrs {
state: SocketAddrsState
}

#[derive(Debug)]
enum SocketAddrsState {
Domain(vec::IntoIter<SocketAddr>),
One(SocketAddr),
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use percent_encoding::{PATH_SEGMENT_ENCODE_SET, USERINFO_ENCODE_SET,
use std::borrow::Borrow;
use std::cmp;
#[cfg(feature = "serde")] use std::error::Error;
use std::fmt::{self, Write};
use std::fmt::{self, Write, Debug, Formatter};
use std::hash;
use std::io;
use std::mem;
Expand Down Expand Up @@ -213,6 +213,16 @@ impl<'a> ParseOptions<'a> {
}
}

impl<'a> Debug for ParseOptions<'a> {
Copy link

@turnage turnage May 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body of this function can just be

(self.base_url, self.encoding_override).fmt(f)

Edit: I've never done a github review before so if this comment blocks or something feel free to close it with a garbage comment.

Copy link
Contributor Author

@imor imor May 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the log_syntax_violation field? Shouldn't it be included? And as it includes a Fn in the type, how is it usually formatted?

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 {
/// Parse an absolute URL from a string.
///
Expand Down Expand Up @@ -1870,6 +1880,7 @@ fn io_error<T>(reason: &str) -> io::Result<T> {
}

/// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.
#[derive(Debug)]
pub struct UrlQuery<'a> {
url: &'a mut Url,
fragment: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions src/path_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use Url;
/// 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");
/// ```
#[derive(Debug)]
pub struct PathSegmentsMut<'a> {
url: &'a mut Url,
after_first_slash: usize,
Expand Down
6 changes: 3 additions & 3 deletions src/percent_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ macro_rules! define_encode_set {
}

/// 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)]
pub struct SIMPLE_ENCODE_SET;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to derive Debug in the encoding set macro to get the rest of *_ENCODE_SET types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


Expand Down Expand Up @@ -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()`.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct PercentEncode<'a, E: EncodeSet> {
bytes: &'a [u8],
encode_set: E,
Expand Down Expand Up @@ -249,7 +249,7 @@ pub fn percent_decode<'a>(input: &'a [u8]) -> PercentDecode<'a> {
}

/// The return type of `percent_decode()`.
#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct PercentDecode<'a> {
bytes: slice::Iter<'a, u8>,
}
Expand Down