Просмотр исходного кода

Make ParseError a C-like enum.

Simon Sapin 12 лет назад
Родитель
Сommit
ac355d4bca
3 измененных файлов с 27 добавлено и 25 удалено
  1. 0 1
      src/lib.rs
  2. 17 11
      src/parser.rs
  3. 10 13
      src/urlutils.rs

+ 0 - 1
src/lib.rs

@@ -137,7 +137,6 @@ pub use parser::{
     InvalidCharacter, InvalidBackslash, InvalidPercentEncoded, InvalidAtSymbolInUser,
     ExpectedTwoSlashes, NonUrlCodePoint, RelativeUrlWithScheme, RelativeUrlWithoutBase,
     RelativeUrlWithNonRelativeBase, NonAsciiDomainsNotSupportedYet,
-    CannotSetFileScheme, CannotSetJavascriptScheme, CannotSetNonRelativeScheme,
 };
 
 #[deprecated = "Moved to the `percent_encoding` module"]

+ 17 - 11
src/parser.rs

@@ -10,8 +10,7 @@ pub use ParseError::{
     InvalidCharacter,InvalidBackslash,InvalidPercentEncoded,InvalidAtSymbolInUser,
     ExpectedTwoSlashes,ExpectedInitialSlash,NonUrlCodePoint,RelativeUrlWithScheme,
     RelativeUrlWithoutBase,RelativeUrlWithNonRelativeBase,
-    NonAsciiDomainsNotSupportedYet,CannotSetFileScheme,
-    CannotSetJavascriptScheme,CannotSetNonRelativeScheme
+    NonAsciiDomainsNotSupportedYet
 };
 
 pub use self::Context::{SetterContext,UrlParserContext};
@@ -41,6 +40,7 @@ macro_rules! is_match(
 
 pub type ParseResult<T> = Result<T, ParseError>;
 
+
 /// Errors that can occur during parsing.
 #[deriving(PartialEq, Eq, Clone)]
 pub enum ParseError {
@@ -60,9 +60,14 @@ pub enum ParseError {
     RelativeUrlWithoutBase,
     RelativeUrlWithNonRelativeBase,
     NonAsciiDomainsNotSupportedYet,
-    CannotSetFileScheme(&'static str),
-    CannotSetJavascriptScheme(&'static str),
-    CannotSetNonRelativeScheme(&'static str)
+    CannotSetPortWithFileLikeScheme,
+    CannotSetFragmentWithJavascriptScheme,
+    CannotSetUsernameWithNonRelativeScheme,
+    CannotSetPasswordWithNonRelativeScheme,
+    CannotSetHostPortWithNonRelativeScheme,
+    CannotSetHostWithNonRelativeScheme,
+    CannotSetPortWithNonRelativeScheme,
+    CannotSetPathWithNonRelativeScheme,
 }
 
 impl Show for ParseError {
@@ -84,12 +89,13 @@ impl Show for ParseError {
             RelativeUrlWithoutBase => "relative URL without a base",
             RelativeUrlWithNonRelativeBase => "relative URL with a non-relative base",
             NonAsciiDomainsNotSupportedYet => "non-ASCII domains are not supported yet",
-            CannotSetFileScheme(ref part) =>
-                return write!(fmt, "cannot set {} on file: URLs", part),
-            CannotSetJavascriptScheme(ref part) =>
-                return write!(fmt, "cannot set {} on javascript: URLs", part),
-            CannotSetNonRelativeScheme(ref part) =>
-                return write!(fmt, "cannot set {} on non-relative URLs", part),
+            _ => "Can not set […]"
+//            CannotSetFileScheme(ref part) =>
+//                return write!(fmt, "cannot set {} on file: URLs", part),
+//            CannotSetJavascriptScheme(ref part) =>
+//                return write!(fmt, "cannot set {} on javascript: URLs", part),
+//            CannotSetNonRelativeScheme(ref part) =>
+//                return write!(fmt, "cannot set {} on non-relative URLs", part),
         }.fmt(fmt)
     }
 }

+ 10 - 13
src/urlutils.rs

@@ -12,10 +12,7 @@
 
 use super::{Url, UrlParser, RelativeSchemeData, NonRelativeSchemeData, FileLikeRelativeScheme};
 use super::UrlRelativeSchemeData;
-use parser::{
-    ParseResult, InvalidScheme,
-    CannotSetFileScheme, CannotSetJavascriptScheme, CannotSetNonRelativeScheme,
-};
+use parser::{ParseError, ParseResult};
 use percent_encoding::{utf8_percent_encode_to, USERNAME_ENCODE_SET, PASSWORD_ENCODE_SET};
 
 
@@ -47,7 +44,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 self.url.scheme = scheme;
                 Ok(())
             },
-            None => Err(InvalidScheme),
+            None => Err(ParseError::InvalidScheme),
         }
     }
 
@@ -59,7 +56,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 utf8_percent_encode_to(input, USERNAME_ENCODE_SET, username);
                 Ok(())
             },
-            NonRelativeSchemeData(_) => Err(CannotSetNonRelativeScheme("username"))
+            NonRelativeSchemeData(_) => Err(ParseError::CannotSetUsernameWithNonRelativeScheme)
         }
     }
 
@@ -72,7 +69,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 *password = Some(new_password);
                 Ok(())
             },
-            NonRelativeSchemeData(_) => Err(CannotSetNonRelativeScheme("password"))
+            NonRelativeSchemeData(_) => Err(ParseError::CannotSetPasswordWithNonRelativeScheme)
         }
     }
 
@@ -90,7 +87,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 *default_port = new_default_port;
                 Ok(())
             },
-            NonRelativeSchemeData(_) => Err(CannotSetNonRelativeScheme("host/port"))
+            NonRelativeSchemeData(_) => Err(ParseError::CannotSetHostPortWithNonRelativeScheme)
         }
     }
 
@@ -102,7 +99,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 *host = new_host;
                 Ok(())
             },
-            NonRelativeSchemeData(_) => Err(CannotSetNonRelativeScheme("host"))
+            NonRelativeSchemeData(_) => Err(ParseError::CannotSetHostWithNonRelativeScheme)
         }
     }
 
@@ -112,7 +109,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
             RelativeSchemeData(UrlRelativeSchemeData { ref mut port, ref mut default_port, .. }) => {
                 let scheme_type = self.parser.get_scheme_type(self.url.scheme.as_slice());
                 if scheme_type == FileLikeRelativeScheme {
-                    return Err(CannotSetFileScheme("port"));
+                    return Err(ParseError::CannotSetPortWithFileLikeScheme);
                 }
                 let (new_port, new_default_port, _) = try!(::parser::parse_port(
                     input, scheme_type, self.parser));
@@ -120,7 +117,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 *default_port = new_default_port;
                 Ok(())
             },
-            NonRelativeSchemeData(_) => Err(CannotSetNonRelativeScheme("port"))
+            NonRelativeSchemeData(_) => Err(ParseError::CannotSetPortWithNonRelativeScheme)
         }
     }
 
@@ -134,7 +131,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
                 *path = new_path;
                 Ok(())
             },
-            NonRelativeSchemeData(_) => Err(CannotSetNonRelativeScheme("path"))
+            NonRelativeSchemeData(_) => Err(ParseError::CannotSetPathWithNonRelativeScheme)
         }
     }
 
@@ -154,7 +151,7 @@ impl<'a> UrlUtils for UrlUtilsWrapper<'a> {
     /// `URLUtils.hash` setter
     fn set_fragment(&mut self, input: &str) -> ParseResult<()> {
         if self.url.scheme.as_slice() == "javascript" {
-            return Err(CannotSetJavascriptScheme("fragment"))
+            return Err(ParseError::CannotSetFragmentWithJavascriptScheme)
         }
         self.url.fragment = if input.is_empty() {
             None