Skip to content

Commit fb71349

Browse files
committed
Change doctests to use &str as error in some cases
- Remove verbose `CannotBeBase` error in examples, use a `&'static str` instead.
1 parent 711d6b8 commit fb71349

File tree

2 files changed

+18
-102
lines changed

2 files changed

+18
-102
lines changed

src/lib.rs

+6-30
Original file line numberDiff line numberDiff line change
@@ -924,29 +924,17 @@ impl Url {
924924
///
925925
/// ```
926926
/// use url::Url;
927-
/// use std::error::Error;
928-
/// use std::fmt;
929-
///
930-
/// #[derive(Debug)]
931-
/// struct CannotBeBaseError;
932-
/// impl fmt::Display for CannotBeBaseError {
933-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
934-
/// f.write_str(self.description())
935-
/// }
936-
/// }
937-
/// impl Error for CannotBeBaseError {
938-
/// fn description(&self) -> &str { "cannot be a base" }
939-
/// }
927+
/// # use std::error::Error;
940928
///
941929
/// # fn run() -> Result<(), Box<Error>> {
942930
/// let url = Url::parse("https://example.com/foo/bar")?;
943-
/// let mut path_segments = url.path_segments().ok_or_else(|| CannotBeBaseError)?;
931+
/// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
944932
/// assert_eq!(path_segments.next(), Some("foo"));
945933
/// assert_eq!(path_segments.next(), Some("bar"));
946934
/// assert_eq!(path_segments.next(), None);
947935
///
948936
/// let url = Url::parse("https://example.com")?;
949-
/// let mut path_segments = url.path_segments().ok_or_else(|| CannotBeBaseError)?;
937+
/// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
950938
/// assert_eq!(path_segments.next(), Some(""));
951939
/// assert_eq!(path_segments.next(), None);
952940
///
@@ -1171,27 +1159,15 @@ impl Url {
11711159
///
11721160
/// ```
11731161
/// use url::Url;
1174-
/// use std::error::Error;
1175-
/// use std::fmt;
1176-
///
1177-
/// #[derive(Debug)]
1178-
/// struct CannotBeBaseError;
1179-
/// impl fmt::Display for CannotBeBaseError {
1180-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1181-
/// f.write_str(self.description())
1182-
/// }
1183-
/// }
1184-
/// impl Error for CannotBeBaseError {
1185-
/// fn description(&self) -> &str { "cannot be a base" }
1186-
/// }
1162+
/// # use std::error::Error;
11871163
///
11881164
/// # fn run() -> Result<(), Box<Error>> {
11891165
/// let mut url = Url::parse("ssh://example.net:2048/")?;
11901166
///
1191-
/// url.set_port(Some(4096)).map_err(|_| CannotBeBaseError)?;
1167+
/// url.set_port(Some(4096)).map_err(|_| "cannot be base")?;
11921168
/// assert_eq!(url.as_str(), "ssh://example.net:4096/");
11931169
///
1194-
/// url.set_port(None).map_err(|_| CannotBeBaseError)?;
1170+
/// url.set_port(None).map_err(|_| "cannot be base")?;
11951171
/// assert_eq!(url.as_str(), "ssh://example.net/");
11961172
/// # Ok(())
11971173
/// # }

src/path_segments.rs

+12-72
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,14 @@ use Url;
1919
///
2020
/// ```rust
2121
/// use url::Url;
22-
/// use std::error::Error;
23-
/// use std::fmt;
24-
///
25-
/// #[derive(Debug)]
26-
/// struct CannotBeBaseError;
27-
/// impl fmt::Display for CannotBeBaseError {
28-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29-
/// f.write_str(self.description())
30-
/// }
31-
/// }
32-
/// impl Error for CannotBeBaseError {
33-
/// fn description(&self) -> &str { "cannot be a base" }
34-
/// }
22+
/// # use std::error::Error;
3523
///
3624
/// # fn run() -> Result<(), Box<Error>> {
3725
/// let mut url = Url::parse("mailto:[email protected]")?;
3826
/// assert!(url.path_segments_mut().is_err());
3927
///
4028
/// let mut url = Url::parse("http://example.net/foo/index.html")?;
41-
/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
29+
/// url.path_segments_mut().map_err(|_| "cannot be base")?
4230
/// .pop().push("img").push("2/100%.png");
4331
/// assert_eq!(url.as_str(), "http://example.net/foo/img/2%2F100%25.png");
4432
/// # Ok(())
@@ -79,23 +67,11 @@ impl<'a> PathSegmentsMut<'a> {
7967
///
8068
/// ```rust
8169
/// use url::Url;
82-
/// use std::error::Error;
83-
/// use std::fmt;
84-
///
85-
/// #[derive(Debug)]
86-
/// struct CannotBeBaseError;
87-
/// impl fmt::Display for CannotBeBaseError {
88-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89-
/// f.write_str(self.description())
90-
/// }
91-
/// }
92-
/// impl Error for CannotBeBaseError {
93-
/// fn description(&self) -> &str { "cannot be a base" }
94-
/// }
70+
/// # use std::error::Error;
9571
///
9672
/// # fn run() -> Result<(), Box<Error>> {
9773
/// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
98-
/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
74+
/// url.path_segments_mut().map_err(|_| "cannot be base")?
9975
/// .clear().push("logout");
10076
/// assert_eq!(url.as_str(), "https://github.com/logout");
10177
/// # Ok(())
@@ -117,29 +93,17 @@ impl<'a> PathSegmentsMut<'a> {
11793
/// Example:
11894
///
11995
/// ```rust
120-
/// # use url::{Url, ParseError};
121-
/// use std::error::Error;
122-
/// use std::fmt;
123-
///
124-
/// #[derive(Debug)]
125-
/// struct CannotBeBaseError;
126-
/// impl fmt::Display for CannotBeBaseError {
127-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128-
/// f.write_str(self.description())
129-
/// }
130-
/// }
131-
/// impl Error for CannotBeBaseError {
132-
/// fn description(&self) -> &str { "cannot be a base" }
133-
/// }
96+
/// use url::Url;
97+
/// # use std::error::Error;
13498
///
13599
/// # fn run() -> Result<(), Box<Error>> {
136100
/// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
137-
/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
101+
/// url.path_segments_mut().map_err(|_| "cannot be base")?
138102
/// .push("pulls");
139103
/// assert_eq!(url.as_str(), "https://github.com/servo/rust-url//pulls");
140104
///
141105
/// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
142-
/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
106+
/// url.path_segments_mut().map_err(|_| "cannot be base")?
143107
/// .pop_if_empty().push("pulls");
144108
/// assert_eq!(url.as_str(), "https://github.com/servo/rust-url/pulls");
145109
/// # Ok(())
@@ -194,26 +158,14 @@ impl<'a> PathSegmentsMut<'a> {
194158
///
195159
/// ```rust
196160
/// use url::Url;
197-
/// use std::error::Error;
198-
/// use std::fmt;
199-
///
200-
/// #[derive(Debug)]
201-
/// struct CannotBeBaseError;
202-
/// impl fmt::Display for CannotBeBaseError {
203-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
204-
/// f.write_str(self.description())
205-
/// }
206-
/// }
207-
/// impl Error for CannotBeBaseError {
208-
/// fn description(&self) -> &str { "cannot be a base" }
209-
/// }
161+
/// # use std::error::Error;
210162
///
211163
/// # fn run() -> Result<(), Box<Error>> {
212164
/// let mut url = Url::parse("https://github.com/")?;
213165
/// let org = "servo";
214166
/// let repo = "rust-url";
215167
/// let issue_number = "188";
216-
/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
168+
/// url.path_segments_mut().map_err(|_| "cannot be base")?
217169
/// .extend(&[org, repo, "issues", issue_number]);
218170
/// assert_eq!(url.as_str(), "https://github.com/servo/rust-url/issues/188");
219171
/// # Ok(())
@@ -225,23 +177,11 @@ impl<'a> PathSegmentsMut<'a> {
225177
///
226178
/// ```rust
227179
/// use url::Url;
228-
/// use std::error::Error;
229-
/// use std::fmt;
230-
///
231-
/// #[derive(Debug)]
232-
/// struct CannotBeBaseError;
233-
/// impl fmt::Display for CannotBeBaseError {
234-
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
235-
/// f.write_str(self.description())
236-
/// }
237-
/// }
238-
/// impl Error for CannotBeBaseError {
239-
/// fn description(&self) -> &str { "cannot be a base" }
240-
/// }
180+
/// # use std::error::Error;
241181
///
242182
/// # fn run() -> Result<(), Box<Error>> {
243183
/// let mut url = Url::parse("https://github.com/servo")?;
244-
/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
184+
/// url.path_segments_mut().map_err(|_| "cannot be base")?
245185
/// .extend(&["..", "rust-url", ".", "pulls"]);
246186
/// assert_eq!(url.as_str(), "https://github.com/servo/rust-url/pulls");
247187
/// # Ok(())

0 commit comments

Comments
 (0)