1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
//! # A query string builder for percent encoding key-value pairs
//!
//! This is a tiny helper crate for simplifying the construction of URL query strings.
//! The initial `?` question mark is automatically prepended.
//!
//! ## Example
//!
//! ```
//! use query_string_builder::QueryString;
//!
//! let qs = QueryString::new()
//! .with_value("q", "🍎 apple")
//! .with_opt_value("color", None)
//! .with_opt_value("category", Some("fruits and vegetables?"));
//!
//! assert_eq!(
//! format!("example.com/{qs}"),
//! "example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables?"
//! );
//! ```
#![deny(unsafe_code)]
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use std::fmt::{Debug, Display, Formatter};
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
/// A query string builder for percent encoding key-value pairs.
///
/// ## Example
///
/// ```
/// use query_string_builder::QueryString;
///
/// let qs = QueryString::new()
/// .with_value("q", "apple")
/// .with_value("category", "fruits and vegetables");
///
/// assert_eq!(
/// format!("https://example.com/{qs}"),
/// "https://example.com/?q=apple&category=fruits%20and%20vegetables"
/// );
/// ```
#[derive(Debug, Default, Clone)]
pub struct QueryString<'a> {
pairs: Vec<Kvp<'a>>,
}
impl<'a> QueryString<'a> {
/// Creates a new, empty query string builder.
pub fn new() -> Self {
Self {
pairs: Vec::default(),
}
}
/// Appends a key-value pair to the query string.
///
/// ## Example
///
/// ```
/// use query_string_builder::QueryString;
///
/// let qs = QueryString::new()
/// .with_value("q", "🍎 apple")
/// .with_value("category", "fruits and vegetables");
///
/// assert_eq!(
/// format!("https://example.com/{qs}"),
/// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables"
/// );
/// ```
pub fn with_value(mut self, key: &'a str, value: &'a str) -> Self {
self.pairs.push(Kvp { key, value });
self
}
/// Appends a key-value pair to the query string if the value exists.
///
/// ## Example
///
/// ```
/// use query_string_builder::QueryString;
///
/// let qs = QueryString::new()
/// .with_opt_value("q", Some("🍎 apple"))
/// .with_opt_value("f", None)
/// .with_opt_value("category", Some("fruits and vegetables"));
///
/// assert_eq!(
/// format!("https://example.com/{qs}"),
/// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables"
/// );
/// ```
pub fn with_opt_value(self, key: &'a str, value: Option<&'a str>) -> Self {
if let Some(value) = value {
self.with_value(key, value)
} else {
self
}
}
/// Appends a key-value pair to the query string.
///
/// ## Example
///
/// ```
/// use query_string_builder::QueryString;
///
/// let mut qs = QueryString::new();
/// qs.push("q", "apple");
/// qs.push("category", "fruits and vegetables");
///
/// assert_eq!(
/// format!("https://example.com/{qs}"),
/// "https://example.com/?q=apple&category=fruits%20and%20vegetables"
/// );
/// ```
pub fn push(&mut self, key: &'a str, value: &'a str) -> &Self {
self.pairs.push(Kvp { key, value });
self
}
/// Appends a key-value pair to the query string if the value exists.
///
/// ## Example
///
/// ```
/// use query_string_builder::QueryString;
///
/// let mut qs = QueryString::new();
/// qs.push_opt("q", None);
/// qs.push_opt("q", Some("🍎 apple"));
///
/// assert_eq!(
/// format!("https://example.com/{qs}"),
/// "https://example.com/?q=%F0%9F%8D%8E%20apple"
/// );
/// ```
pub fn push_opt(&mut self, key: &'a str, value: Option<&'a str>) -> &Self {
if let Some(value) = value {
self.push(key, value)
} else {
self
}
}
/// Determines the number of key-value pairs currently in the builder.
pub fn len(&self) -> usize {
self.pairs.len()
}
/// Determines if the builder is currently empty.
pub fn is_empty(&self) -> bool {
self.pairs.is_empty()
}
}
impl<'a> Display for QueryString<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.pairs.is_empty() {
return Ok(());
} else {
write!(f, "?")?;
for (i, pair) in self.pairs.iter().enumerate() {
if i > 0 {
write!(f, "&")?;
}
write!(
f,
"{key}={value}",
key = utf8_percent_encode(pair.key, FRAGMENT),
value = utf8_percent_encode(pair.value, FRAGMENT)
)?;
}
Ok(())
}
}
}
#[derive(Debug, Clone)]
struct Kvp<'a> {
key: &'a str,
value: &'a str,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simple() {
let qs = QueryString::new()
.with_value("q", "apple???")
.with_value("category", "fruits and vegetables");
assert_eq!(
qs.to_string(),
"?q=apple???&category=fruits%20and%20vegetables"
);
}
#[test]
fn test_encoding() {
let qs = QueryString::new()
.with_value("q", "Grünkohl")
.with_value("category", "Gemüse");
assert_eq!(qs.to_string(), "?q=Gr%C3%BCnkohl&category=Gem%C3%BCse");
}
#[test]
fn test_emoji() {
let qs = QueryString::new()
.with_value("q", "🥦")
.with_value("🍽️", "🍔🍕");
assert_eq!(
qs.to_string(),
"?q=%F0%9F%A5%A6&%F0%9F%8D%BD%EF%B8%8F=%F0%9F%8D%94%F0%9F%8D%95"
);
}
#[test]
fn test_optional() {
let qs = QueryString::new()
.with_value("q", "celery")
.with_opt_value("taste", None)
.with_opt_value("category", Some("fruits and vegetables"));
assert_eq!(
qs.to_string(),
"?q=celery&category=fruits%20and%20vegetables"
);
}
}