Explorar o código

Apply review suggestions

* Move decode_utf8_lossy back into query_encoding
* Don't export the query_encoding module, but re-export EncodingOverride
  from it
Jonas Platte %!s(int64=6) %!d(string=hai) anos
pai
achega
5edbc5ab0b
Modificáronse 4 ficheiros con 28 adicións e 22 borrados
  1. 4 19
      form_urlencoded/src/lib.rs
  2. 17 0
      form_urlencoded/src/query_encoding.rs
  3. 1 1
      src/lib.rs
  4. 6 2
      src/parser.rs

+ 4 - 19
form_urlencoded/src/lib.rs

@@ -18,11 +18,13 @@ extern crate percent_encoding;
 extern crate matches;
 
 use percent_encoding::{percent_decode, percent_encode_byte};
-use query_encoding::EncodingOverride;
+use query_encoding::decode_utf8_lossy;
 use std::borrow::{Borrow, Cow};
 use std::str;
 
-pub mod query_encoding;
+mod query_encoding;
+
+pub use query_encoding::EncodingOverride;
 
 /// Convert a byte string in the `application/x-www-form-urlencoded` syntax
 /// into a iterator of (name, value) pairs.
@@ -327,20 +329,3 @@ fn append_pair(
 fn append_encoded(s: &str, string: &mut String, encoding: EncodingOverride) {
     string.extend(byte_serialize(&query_encoding::encode(encoding, s.into())))
 }
-
-fn decode_utf8_lossy(input: Cow<[u8]>) -> Cow<str> {
-    match input {
-        Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
-        Cow::Owned(bytes) => {
-            let raw_utf8: *const [u8];
-            match String::from_utf8_lossy(&bytes) {
-                Cow::Borrowed(utf8) => raw_utf8 = utf8.as_bytes(),
-                Cow::Owned(s) => return s.into(),
-            }
-            // from_utf8_lossy returned a borrow of `bytes` unchanged.
-            debug_assert!(raw_utf8 == &*bytes as *const [u8]);
-            // Reuse the existing `Vec` allocation.
-            unsafe { String::from_utf8_unchecked(bytes) }.into()
-        }
-    }
-}

+ 17 - 0
form_urlencoded/src/query_encoding.rs

@@ -16,3 +16,20 @@ pub fn encode<'a>(encoding_override: EncodingOverride, input: &'a str) -> Cow<'a
     }
     input.as_bytes().into()
 }
+
+pub fn decode_utf8_lossy(input: Cow<[u8]>) -> Cow<str> {
+    match input {
+        Cow::Borrowed(bytes) => String::from_utf8_lossy(bytes),
+        Cow::Owned(bytes) => {
+            let raw_utf8: *const [u8];
+            match String::from_utf8_lossy(&bytes) {
+                Cow::Borrowed(utf8) => raw_utf8 = utf8.as_bytes(),
+                Cow::Owned(s) => return s.into(),
+            }
+            // from_utf8_lossy returned a borrow of `bytes` unchanged.
+            debug_assert!(raw_utf8 == &*bytes as *const [u8]);
+            // Reuse the existing `Vec` allocation.
+            unsafe { String::from_utf8_unchecked(bytes) }.into()
+        }
+    }
+}

+ 1 - 1
src/lib.rs

@@ -133,7 +133,7 @@ use std::str;
 
 use std::convert::TryFrom;
 
-pub use form_urlencoded::query_encoding::EncodingOverride;
+pub use form_urlencoded::EncodingOverride;
 pub use host::Host;
 pub use origin::{OpaqueOrigin, Origin};
 pub use parser::{ParseError, SyntaxViolation};

+ 6 - 2
src/parser.rs

@@ -10,7 +10,7 @@ use std::error::Error;
 use std::fmt::{self, Formatter, Write};
 use std::str;
 
-use form_urlencoded::query_encoding::{self, EncodingOverride};
+use form_urlencoded::EncodingOverride;
 use host::{Host, HostInternal};
 use percent_encoding::{percent_encode, utf8_percent_encode, AsciiSet, CONTROLS};
 use Url;
@@ -1441,7 +1441,11 @@ impl<'a> Parser<'a> {
             "http" | "https" | "file" | "ftp" | "gopher" => self.query_encoding_override,
             _ => None,
         };
-        let query_bytes = query_encoding::encode(encoding, &query);
+        let query_bytes = if let Some(o) = encoding {
+            o(&query)
+        } else {
+            query.as_bytes().into()
+        };
         let set = if scheme_type.is_special() {
             SPECIAL_QUERY
         } else {