Skip to content

Commit 4083375

Browse files
author
bors-servo
authored
Auto merge of #335 - imor:impl-debug, r=SimonSapin
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 -->
2 parents dc5cf2b + 81fdb30 commit 4083375

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

src/encoding.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#[cfg(feature = "query_encoding")] extern crate encoding;
1414

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

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

93+
#[cfg(feature = "query_encoding")]
94+
impl Debug for EncodingOverride {
95+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
96+
write!(f, "EncodingOverride {{ encoding: ")?;
97+
match self.encoding {
98+
Some(e) => write!(f, "{} }}", e.name()),
99+
None => write!(f, "None }}")
100+
}
101+
}
102+
}
92103

93104
#[cfg(not(feature = "query_encoding"))]
94-
#[derive(Copy, Clone)]
105+
#[derive(Copy, Clone, Debug)]
95106
pub struct EncodingOverride;
96107

97108
#[cfg(not(feature = "query_encoding"))]

src/form_urlencoded.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn parse_with_encoding<'a>(input: &'a [u8],
8181
}
8282

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

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

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

210212
/// The [`application/x-www-form-urlencoded` serializer](
211213
/// https://url.spec.whatwg.org/#concept-urlencoded-serializer).
214+
#[derive(Debug)]
212215
pub struct Serializer<T: Target> {
213216
target: Option<T>,
214217
start_position: usize,

src/host.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<S: AsRef<str>> fmt::Display for Host<S> {
176176

177177
/// This mostly exists because coherence rules don’t allow us to implement
178178
/// `ToSocketAddrs for (Host<S>, u16)`.
179-
#[derive(Clone)]
179+
#[derive(Clone, Debug)]
180180
pub struct HostAndPort<S=String> {
181181
pub host: Host<S>,
182182
pub port: u16,
@@ -222,10 +222,12 @@ impl<S: AsRef<str>> ToSocketAddrs for HostAndPort<S> {
222222
}
223223

224224
/// Socket addresses for an URL.
225+
#[derive(Debug)]
225226
pub struct SocketAddrs {
226227
state: SocketAddrsState
227228
}
228229

230+
#[derive(Debug)]
229231
enum SocketAddrsState {
230232
Domain(vec::IntoIter<SocketAddr>),
231233
One(SocketAddr),

src/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ use percent_encoding::{PATH_SEGMENT_ENCODE_SET, USERINFO_ENCODE_SET,
122122
use std::borrow::Borrow;
123123
use std::cmp;
124124
#[cfg(feature = "serde")] use std::error::Error;
125-
use std::fmt::{self, Write};
125+
use std::fmt::{self, Write, Debug, Formatter};
126126
use std::hash;
127127
use std::io;
128128
use std::mem;
@@ -227,6 +227,16 @@ impl<'a> ParseOptions<'a> {
227227
}
228228
}
229229

230+
impl<'a> Debug for ParseOptions<'a> {
231+
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
232+
write!(f, "ParseOptions {{ base_url: {:?}, encoding_override: {:?}, log_syntax_violation: ", self.base_url, self.encoding_override)?;
233+
match self.log_syntax_violation {
234+
Some(_) => write!(f, "Some(Fn(&'static str)) }}"),
235+
None => write!(f, "None }}")
236+
}
237+
}
238+
}
239+
230240
impl Url {
231241
/// Parse an absolute URL from a string.
232242
///
@@ -2082,6 +2092,7 @@ fn io_error<T>(reason: &str) -> io::Result<T> {
20822092
}
20832093

20842094
/// Implementation detail of `Url::query_pairs_mut`. Typically not used directly.
2095+
#[derive(Debug)]
20852096
pub struct UrlQuery<'a> {
20862097
url: &'a mut Url,
20872098
fragment: Option<String>,

src/path_segments.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use Url;
3333
/// # }
3434
/// # run().unwrap();
3535
/// ```
36+
#[derive(Debug)]
3637
pub struct PathSegmentsMut<'a> {
3738
url: &'a mut Url,
3839
after_first_slash: usize,

src/percent_encoding.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub trait EncodeSet: Clone {
5858
macro_rules! define_encode_set {
5959
($(#[$attr: meta])* pub $name: ident = [$base_set: expr] | {$($ch: pat),*}) => {
6060
$(#[$attr])*
61-
#[derive(Copy, Clone)]
61+
#[derive(Copy, Clone, Debug)]
6262
#[allow(non_camel_case_types)]
6363
pub struct $name;
6464

@@ -77,7 +77,7 @@ macro_rules! define_encode_set {
7777
}
7878

7979
/// This encode set is used for the path of cannot-be-a-base URLs.
80-
#[derive(Copy, Clone)]
80+
#[derive(Copy, Clone, Debug)]
8181
#[allow(non_camel_case_types)]
8282
pub struct SIMPLE_ENCODE_SET;
8383

@@ -163,7 +163,7 @@ pub fn utf8_percent_encode<E: EncodeSet>(input: &str, encode_set: E) -> PercentE
163163
}
164164

165165
/// The return type of `percent_encode()` and `utf8_percent_encode()`.
166-
#[derive(Clone)]
166+
#[derive(Clone, Debug)]
167167
pub struct PercentEncode<'a, E: EncodeSet> {
168168
bytes: &'a [u8],
169169
encode_set: E,
@@ -249,7 +249,7 @@ pub fn percent_decode(input: &[u8]) -> PercentDecode {
249249
}
250250

251251
/// The return type of `percent_decode()`.
252-
#[derive(Clone)]
252+
#[derive(Clone, Debug)]
253253
pub struct PercentDecode<'a> {
254254
bytes: slice::Iter<'a, u8>,
255255
}

0 commit comments

Comments
 (0)