url_encoded_data
- crate: https://crates.io/crates/url_encoded_data
- doc: https://crates.io/crates/url_encoded_data
- test coverage: 100% lines covered(by grcov, 2021-03-12)
Ergonomic, Versatile Url-Encoded-Data Manipulator
Manipulate data of application/x-www-form-urlencoded
format,
eg:
* query_string of a url (eg: '?a=1&b=2&c=3&c=3&e=5')
* http content-type with: application/x-www-form-urlencoded
Features:
-
convenient api:
-
as_pairs
-
as_pairs_of_original_order
-
as_paris_of_sorted_order
-
as_map_of_single_key_to_multiple_values
-
as_map_of_single_key_to_first_occurrence_value
-
as_map_of_single_key_to_last_occurrence_value
-
set
-
push
-
clear
-
get
-
get_first
-
get_last
-
keys
-
len // pair length
-
keys_length
-
to_string (to_final_string), same to:
format!("{}", self)
-
exists
-
// consult doc for more
-
-
Automatic unicode encoding/decoding
Terminology
- Pair: a (key, format) tuple,
(Cow<'a, str>, Cow<'a, str>)
- url encoded string: a string which is encoded by standards of
application/x-www-form-urlencoded
Notes
- UrlEncodedDataPairScanner: Pairs Iterator, yields pairs only. (high performant)
- UrlEncodedData: eager version
Sample
Sample of url query string
use UrlEncodedData;
use Cow;
// note: the library will not check the validity of the url, it just searchs for url-encoded-data, eg: string after first '?' and then s.trim_start('?')
let url = "https://google.com/?q=rust&ei=code";
let q = from;
// q.to_string(), best performance, (key, value) pairs are in un-deterministic order.
assert_eq!;
assert_eq!;
// pairs length
assert_eq!;
// keys length
assert_eq!;
// keys
assert!;
assert!;
// exists
assert!;
assert!;
// let's do some manipulation
let url = "https://google.com/?q=rust&ei=code";
let q = parse_str
.set_one
.set
.set_one
.set_one
.set_one
.set // utf-8, auto encoding and decoding
.delete // ei is deleted
.push
.done; // now b is: vec!["1", "2"]
// q.keys() // performant
assert_eq!;
// something like: https://google.com/?b=2&b=3&q=rust-lang&a=1&hello=world&vector=1&vector=2&whole=world&whole=%E4%B8%96%E7%95%8C
println!; // calls q.to_final_string() actually.
// something like: https://google.com/?b=2&b=3&q=rust-lang&a=1&hello=world&vector=1&vector=2&whole=world&whole=%E4%B8%96%E7%95%8C
println!;
// https://google.com/?q=rust-lang&b=2&b=3&a=1&hello=world&vector=1&vector=2&whole=world&whole=%E4%B8%96%E7%95%8C
println!;
// https://google.com/?a=1&b=2&b=3&hello=world&q=rust-lang&vector=1&vector=2&whole=world&whole=%E4%B8%96%E7%95%8C
println!;
Sample of encoded data in www/x-www-form-urlencoded
use UrlEncodedData;
use Cow;
// note: the library will not check the validity of the url, it just searchs for url-encoded-data, eg: string after first '?' and then s.trim_start('?')
let s = "b=2&b=3&q=rust-lang&a=1&hello=world&vector=1&vector=2&whole=world&whole=%E4%B8%96%E7%95%8C";
let q = parse_str;
// q.to_string(), best performance, (key, value) pairs are in un-deterministic order.
assert_eq!;
// [("hello", "world"), ("vector", "1"), ("vector", "2"), ("whole", "world"), ("whole", "世界"), ("b", "2"), ("b", "3"), ("q", "rust-lang"), ("a", "1")]
println!;
// {"a": ["1"], "hello": ["world"], "b": ["2", "3"], "q": ["rust-lang"], "whole": ["world", "世界"], "vector": ["1", "2"]}
println!;
// {"b": "2", "a": "1", "q": "rust-lang", "whole": "world", "hello": "world", "vector": "1"}
println!;
// {"q": "rust-lang", "whole": "世界", "vector": "2", "a": "1", "b": "3", "hello": "world"}
println!;
// assert!(false);
Sample of performant pairs iterator: UrlEncodedDataPairScanner (Lazy version)
use ;
use Cow;
// note: the library will not check the validity of the url, it just searchs for url-encoded-data, eg: string after first '?' and then s.trim_start('?')
let s = "b=2&b=3&q=rust-lang&a=1&hello=world&vector=1&vector=2&whole=world&whole=%E4%B8%96%E7%95%8C";
let q = from;
// same:
// let q = UrlEncodedDataPairScanner::parse_from_data_str(s);
for in q.iter
Some apis
strigify: Stringify pairs to url encoded String
example 1
use stringify;
let encoded = stringify;
assert_eq!;
example 2
use stringify;
let encoded = stringify;
assert_eq!;
UrlEncodedDataPairScanner: Lazy iterator yielding pairs only, performant when you only needs pairs in sequence.
example:
use *;
let qs = "a=1&b=2&c=3&c=4&key_without_value&=value_without_key".to_string;
for s in
.iter
UrlEncodedData: parse url_encoded_data to pairs eagerly
some methods:
for string: "a=1&b=2&a=3"
- as_pairs: ["a", "1"], ["b", "2"], ["c", "3"]
- as_map_of_single_key_to_multiple_values: {"a": ["1", "3"], "b": ["2"]}
- as_map_of_single_key_to_first_occurrence_value: {"a": "1", "b": "2"}
- as_map_of_single_key_to_last_occurrence_value: {"a": "3", "b": "2"}
get shortcuts note: for multiple get, use the result of map mehtods directly
- get_multiple_values: "a" -> vec!["1".to_string(), "3".to_string()]
- get_first_occurrence_value: "a" -> "1".to_string()
- get_last_occurrence_value: "a" -> "3".to_string()
Typical usage might be:
use *;
use Cow;
let s = "a=1&b=2&a=3";
let ued = from;
// get pairs
let pairs = ued.as_pairs;
// 1:N
let map_n = ued.as_map_of_single_key_to_multiple_values;
let a = map_n.get.unwrap;
assert_eq!;
assert_eq!;
// 1:first-value-met
let map_f = ued.as_map_of_single_key_to_first_occurrence_value;
let a = map_f.get.unwrap;
assert_eq!;
// 1:last-value-met
let map_l = ued.as_map_of_single_key_to_last_occurrence_value;
let a = map_l.get.unwrap;
assert_eq!;
One time get(For best performance of multiple callings, use the result of method calling of as_map_*)
use *;
use Cow;
let s = "a=1&b=2&a=3";
let ued = from;
assert_eq!;
// get first occurrence value
assert_eq!;
// get last occurrence value
assert_eq!;
// no existed key
assert!;
full example
extern crate maplit;
use *;