فهرست منبع

Change doctests to use &str as error in some cases

- Remove verbose `CannotBeBase` error in examples, use a `&'static str` instead.
Eduardo Pinho 9 سال پیش
والد
کامیت
fb71349ace
2فایلهای تغییر یافته به همراه18 افزوده شده و 102 حذف شده
  1. 6 30
      src/lib.rs
  2. 12 72
      src/path_segments.rs

+ 6 - 30
src/lib.rs

@@ -924,29 +924,17 @@ impl Url {
     ///
     /// ```
     /// use url::Url;
-    /// use std::error::Error;
-    /// use std::fmt;
-    ///
-    /// #[derive(Debug)]
-    /// struct CannotBeBaseError;
-    /// impl fmt::Display for CannotBeBaseError {
-    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-    ///         f.write_str(self.description())
-    ///     }
-    /// }
-    /// impl Error for CannotBeBaseError {
-    ///     fn description(&self) -> &str { "cannot be a base" }
-    /// }
+    /// # use std::error::Error;
     ///
     /// # fn run() -> Result<(), Box<Error>> {
     /// let url = Url::parse("https://example.com/foo/bar")?;
-    /// let mut path_segments = url.path_segments().ok_or_else(|| CannotBeBaseError)?;
+    /// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
     /// assert_eq!(path_segments.next(), Some("foo"));
     /// assert_eq!(path_segments.next(), Some("bar"));
     /// assert_eq!(path_segments.next(), None);
     ///
     /// let url = Url::parse("https://example.com")?;
-    /// let mut path_segments = url.path_segments().ok_or_else(|| CannotBeBaseError)?;
+    /// let mut path_segments = url.path_segments().ok_or_else(|| "cannot be base")?;
     /// assert_eq!(path_segments.next(), Some(""));
     /// assert_eq!(path_segments.next(), None);
     ///
@@ -1171,27 +1159,15 @@ impl Url {
     ///
     /// ```
     /// use url::Url;
-    /// use std::error::Error;
-    /// use std::fmt;
-    ///
-    /// #[derive(Debug)]
-    /// struct CannotBeBaseError;
-    /// impl fmt::Display for CannotBeBaseError {
-    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-    ///         f.write_str(self.description())
-    ///     }
-    /// }
-    /// impl Error for CannotBeBaseError {
-    ///     fn description(&self) -> &str { "cannot be a base" }
-    /// }
+    /// # use std::error::Error;
     ///
     /// # fn run() -> Result<(), Box<Error>> {
     /// let mut url = Url::parse("ssh://example.net:2048/")?;
     ///
-    /// url.set_port(Some(4096)).map_err(|_| CannotBeBaseError)?;
+    /// url.set_port(Some(4096)).map_err(|_| "cannot be base")?;
     /// assert_eq!(url.as_str(), "ssh://example.net:4096/");
     ///
-    /// url.set_port(None).map_err(|_| CannotBeBaseError)?;
+    /// url.set_port(None).map_err(|_| "cannot be base")?;
     /// assert_eq!(url.as_str(), "ssh://example.net/");
     /// # Ok(())
     /// # }

+ 12 - 72
src/path_segments.rs

@@ -19,26 +19,14 @@ use Url;
 ///
 /// ```rust
 /// use url::Url;
-/// use std::error::Error;
-/// use std::fmt;
-///
-/// #[derive(Debug)]
-/// struct CannotBeBaseError;
-/// impl fmt::Display for CannotBeBaseError {
-///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-///         f.write_str(self.description())
-///     }
-/// }
-/// impl Error for CannotBeBaseError {
-///     fn description(&self) -> &str { "cannot be a base" }
-/// }
+/// # use std::error::Error;
 ///
 /// # fn run() -> Result<(), Box<Error>> {
 /// let mut url = Url::parse("mailto:me@example.com")?;
 /// assert!(url.path_segments_mut().is_err());
 ///
 /// let mut url = Url::parse("http://example.net/foo/index.html")?;
-/// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
+/// url.path_segments_mut().map_err(|_| "cannot be base")?
 ///     .pop().push("img").push("2/100%.png");
 /// assert_eq!(url.as_str(), "http://example.net/foo/img/2%2F100%25.png");
 /// # Ok(())
@@ -79,23 +67,11 @@ impl<'a> PathSegmentsMut<'a> {
     ///
     /// ```rust
     /// use url::Url;
-    /// use std::error::Error;
-    /// use std::fmt;
-    ///
-    /// #[derive(Debug)]
-    /// struct CannotBeBaseError;
-    /// impl fmt::Display for CannotBeBaseError {
-    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-    ///         f.write_str(self.description())
-    ///     }
-    /// }
-    /// impl Error for CannotBeBaseError {
-    ///     fn description(&self) -> &str { "cannot be a base" }
-    /// }
+    /// # use std::error::Error;
     ///
     /// # fn run() -> Result<(), Box<Error>> {
     /// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
-    /// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
+    /// url.path_segments_mut().map_err(|_| "cannot be base")?
     ///     .clear().push("logout");
     /// assert_eq!(url.as_str(), "https://github.com/logout");
     /// # Ok(())
@@ -117,29 +93,17 @@ impl<'a> PathSegmentsMut<'a> {
     /// Example:
     ///
     /// ```rust
-    /// # use url::{Url, ParseError};
-    /// use std::error::Error;
-    /// use std::fmt;
-    ///
-    /// #[derive(Debug)]
-    /// struct CannotBeBaseError;
-    /// impl fmt::Display for CannotBeBaseError {
-    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-    ///         f.write_str(self.description())
-    ///     }
-    /// }
-    /// impl Error for CannotBeBaseError {
-    ///     fn description(&self) -> &str { "cannot be a base" }
-    /// }
+    /// use url::Url;
+    /// # use std::error::Error;
     ///
     /// # fn run() -> Result<(), Box<Error>> {
     /// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
-    /// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
+    /// url.path_segments_mut().map_err(|_| "cannot be base")?
     ///     .push("pulls");
     /// assert_eq!(url.as_str(), "https://github.com/servo/rust-url//pulls");
     ///
     /// let mut url = Url::parse("https://github.com/servo/rust-url/")?;
-    /// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
+    /// url.path_segments_mut().map_err(|_| "cannot be base")?
     ///     .pop_if_empty().push("pulls");
     /// assert_eq!(url.as_str(), "https://github.com/servo/rust-url/pulls");
     /// # Ok(())
@@ -194,26 +158,14 @@ impl<'a> PathSegmentsMut<'a> {
     ///
     /// ```rust
     /// use url::Url;
-    /// use std::error::Error;
-    /// use std::fmt;
-    ///
-    /// #[derive(Debug)]
-    /// struct CannotBeBaseError;
-    /// impl fmt::Display for CannotBeBaseError {
-    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-    ///         f.write_str(self.description())
-    ///     }
-    /// }
-    /// impl Error for CannotBeBaseError {
-    ///     fn description(&self) -> &str { "cannot be a base" }
-    /// }
+    /// # use std::error::Error;
     ///
     /// # fn run() -> Result<(), Box<Error>> {
     /// let mut url = Url::parse("https://github.com/")?;
     /// let org = "servo";
     /// let repo = "rust-url";
     /// let issue_number = "188";
-    /// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
+    /// url.path_segments_mut().map_err(|_| "cannot be base")?
     ///     .extend(&[org, repo, "issues", issue_number]);
     /// assert_eq!(url.as_str(), "https://github.com/servo/rust-url/issues/188");
     /// # Ok(())
@@ -225,23 +177,11 @@ impl<'a> PathSegmentsMut<'a> {
     ///
     /// ```rust
     /// use url::Url;
-    /// use std::error::Error;
-    /// use std::fmt;
-    ///
-    /// #[derive(Debug)]
-    /// struct CannotBeBaseError;
-    /// impl fmt::Display for CannotBeBaseError {
-    ///     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-    ///         f.write_str(self.description())
-    ///     }
-    /// }
-    /// impl Error for CannotBeBaseError {
-    ///     fn description(&self) -> &str { "cannot be a base" }
-    /// }
+    /// # use std::error::Error;
     ///
     /// # fn run() -> Result<(), Box<Error>> {
     /// let mut url = Url::parse("https://github.com/servo")?;
-    /// url.path_segments_mut().map_err(|_| CannotBeBaseError)?
+    /// url.path_segments_mut().map_err(|_| "cannot be base")?
     ///     .extend(&["..", "rust-url", ".", "pulls"]);
     /// assert_eq!(url.as_str(), "https://github.com/servo/rust-url/pulls");
     /// # Ok(())