Parse and stringify URL query strings in a fantastic way.
- URL Encoding: Encodes characters that are not part of the unreserved set (
A-Z,a-z,0-9,-,_,.,~) and properly handles multi-byte UTF-8 characters. - URL Decoding: Decodes percent-encoded characters back to their original form.
- Query String Parsing: Parses a query string into a
HashMapof key-value pairs. - Query String Stringifying: Converts a
HashMapof key-value pairs back into a query string.
cargo add query_stringor
[dependencies]
query_string = "0.1.0"The encode function encodes any character that is not part of the unreserved set and handles multi-byte UTF-8 characters.
use your_crate_name::encode;
fn main() {
let input = "Hello World! γγγ«γ‘γ―";
let encoded = encode(input);
println!("{}", encoded); // Output: Hello%20World%21%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF
}The decode function decodes a percent-encoded string.
use your_crate_name::decode;
fn main() -> std::io::Result<()> {
let encoded = "Hello%20World%21%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF";
let decoded = decode(encoded)?;
println!("{}", decoded); // Output: Hello World! γγγ«γ‘γ―
Ok(())
}The parse function parses a query string into a HashMap<String, String>.
use your_crate_name::parse;
fn main() {
use query_string::parse;
fn main() {
let query = "?foo=bar&foo=baz&name=John&age=30";
let parsed = parse(query);
for (key, values) in &parsed {
println!("{}: {:?}", key, values);
}
// Output:
// foo: ["bar", "baz"]
// name: ["John"]
// age: ["30"]
}
}The stringify function converts a HashMap<String, String> into a query string.
use std::collections::HashMap;
use your_crate_name::stringify;
fn main() {
let mut params = HashMap::new();
params.insert("name".to_string(), "John".to_string());
params.insert("age".to_string(), "30".to_string());
params.insert("city".to_string(), "New York".to_string());
let query_string = stringify(¶ms);
println!("{}", query_string); // Output: name=John&age=30&city=New York
}Encodes the input string using percent-encoding. Reserved characters and non-ASCII characters are encoded as %XX, where XX is the hexadecimal representation of the byte.
Decodes a percent-encoded string. Returns an error if the input contains invalid percent-encoded sequences.
Parses a URL query string into a HashMap<String, Vec<String>>. Handles removing unnecessary characters like ?, #, and ! from the query string before parsing.
Converts a HashMap<String, String> into a query string format, where key-value pairs are joined by = and separated by &.
All contribution are welcome. Please create issue or make PR for bugs, issues or new features. Please feel free to reach out for detail information of the project.
This project is licensed under the MIT License. See the LICENSE file for details.