Skip to content

Commit

Permalink
Auto merge of #335 - imor:impl-debug, r=SimonSapin
Browse files Browse the repository at this point in the history
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 formatted as Some(Fn(&'static
str)) or None depending upon its value.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-url/335)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo authored Jun 13, 2017
2 parents dc5cf2b + 81fdb30 commit 4083375
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
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 {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "EncodingOverride {{ encoding: ")?;
match self.encoding {
Some(e) => write!(f, "{} }}", e.name()),
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 @@ -222,10 +222,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 @@ -122,7 +122,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 @@ -227,6 +227,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 {
/// Parse an absolute URL from a string.
///
Expand Down Expand Up @@ -2082,6 +2092,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 @@ -33,6 +33,7 @@ use Url;
/// # }
/// # run().unwrap();
/// ```
#[derive(Debug)]
pub struct PathSegmentsMut<'a> {
url: &'a mut Url,
after_first_slash: usize,
Expand Down
8 changes: 4 additions & 4 deletions src/percent_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait EncodeSet: Clone {
macro_rules! define_encode_set {
($(#[$attr: meta])* pub $name: ident = [$base_set: expr] | {$($ch: pat),*}) => {
$(#[$attr])*
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
#[allow(non_camel_case_types)]
pub struct $name;

Expand All @@ -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;

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(input: &[u8]) -> PercentDecode {
}

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

0 comments on commit 4083375

Please sign in to comment.