Quellcode durchsuchen

form-urlencoded: Allow to encode key parameters without a values

Add methods:
    - Serializer<UrlQuery>::append_key_only(&mut self, name: &str) -> &mut Self;
    - Serializer<UrlQuery>::extend_keys_only<I, K>(&mut self, iter: I) -> &mut Self;
in order to allow query parameters without a values like:
    - https://example.org/exchange?date=20190915&json
    - https://example.org/exchange?date=20190915&xml
    - https://example.org/exchange?date=20190915&bin
Argentumbolo vor 7 Jahren
Ursprung
Commit
47b6555468
2 geänderte Dateien mit 55 neuen und 2 gelöschten Zeilen
  1. 51 0
      form_urlencoded/src/lib.rs
  2. 4 2
      url/tests/unit.rs

+ 51 - 0
form_urlencoded/src/lib.rs

@@ -260,6 +260,19 @@ impl<'a, T: Target> Serializer<'a, T> {
         self
     }
 
+    /// Serialize and append a name of parameter without any value.
+    ///
+    /// Panics if called after `.finish()`.
+    pub fn append_key_only(&mut self, name: &str) -> &mut Self {
+        append_key_only(
+            string(&mut self.target),
+            self.start_position,
+            self.encoding,
+            name,
+        );
+        self
+    }
+
     /// Serialize and append a number of name/value pairs.
     ///
     /// This simply calls `append_pair` repeatedly.
@@ -290,6 +303,34 @@ impl<'a, T: Target> Serializer<'a, T> {
         self
     }
 
+    /// Serialize and append a number of names without values.
+    ///
+    /// This simply calls `append_key_only` repeatedly.
+    /// This can be more convenient, so the user doesn’t need to introduce a block
+    /// to limit the scope of `Serializer`’s borrow of its string.
+    ///
+    /// Panics if called after `.finish()`.
+    pub fn extend_keys_only<I, K>(&mut self, iter: I) -> &mut Self
+    where
+        I: IntoIterator,
+        I::Item: Borrow<K>,
+        K: AsRef<str>,
+    {
+        {
+            let string = string(&mut self.target);
+            for key in iter {
+                let ref k = key.borrow();
+                append_key_only(
+                    string,
+                    self.start_position,
+                    self.encoding,
+                    k.as_ref(),
+                );
+            }
+        }
+        self
+    }
+
     /// If this serializer was constructed with a string, take and return that string.
     ///
     /// ```rust
@@ -336,6 +377,16 @@ fn append_pair(
     append_encoded(value, string, encoding);
 }
 
+fn append_key_only(
+    string: &mut String,
+    start_position: usize,
+    encoding: EncodingOverride,
+    name: &str,
+) {
+    append_separator_if_needed(string, start_position);
+    append_encoded(name, string, encoding);
+}
+
 fn append_encoded(s: &str, string: &mut String, encoding: EncodingOverride<'_>) {
     string.extend(byte_serialize(&query_encoding::encode(encoding, s)))
 }

+ 4 - 2
url/tests/unit.rs

@@ -355,8 +355,9 @@ fn test_form_serialize() {
         .append_pair("foo", "é&")
         .append_pair("bar", "")
         .append_pair("foo", "#")
+        .append_key_only("json")
         .finish();
-    assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23");
+    assert_eq!(encoded, "foo=%C3%A9%26&bar=&foo=%23&json");
 }
 
 #[test]
@@ -364,8 +365,9 @@ fn form_urlencoded_encoding_override() {
     let encoded = form_urlencoded::Serializer::new(String::new())
         .encoding_override(Some(&|s| s.as_bytes().to_ascii_uppercase().into()))
         .append_pair("foo", "bar")
+        .append_key_only("xml")
         .finish();
-    assert_eq!(encoded, "FOO=BAR");
+    assert_eq!(encoded, "FOO=BAR&XML");
 }
 
 #[test]