Skip to content

Commit 323ba48

Browse files
committed
Change function argements to take T: ToString
See #2
1 parent f86621e commit 323ba48

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
55

6+
## Unreleased
7+
8+
### Changed
9+
10+
- [#3](https://github.com/sunsided/query-string-builder/pull/3):
11+
The functions now change inputs that implement `ToString` rather than requiring `Into<String>`.
12+
This allows for any `Display` types to be used directly.
13+
614
## [0.4.0] - 2023-07-08
715

816
### Added

src/lib.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ impl QueryString {
6565
///
6666
/// let qs = QueryString::new()
6767
/// .with_value("q", "🍎 apple")
68-
/// .with_value("category", "fruits and vegetables");
68+
/// .with_value("category", "fruits and vegetables")
69+
/// .with_value("answer", 42);
6970
///
7071
/// assert_eq!(
7172
/// format!("https://example.com/{qs}"),
72-
/// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables"
73+
/// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&answer=42"
7374
/// );
7475
/// ```
75-
pub fn with_value<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
76+
pub fn with_value<K: ToString, V: ToString>(mut self, key: K, value: V) -> Self {
7677
self.pairs.push(Kvp {
77-
key: key.into(),
78-
value: value.into(),
78+
key: key.to_string(),
79+
value: value.to_string(),
7980
});
8081
self
8182
}
@@ -90,14 +91,15 @@ impl QueryString {
9091
/// let qs = QueryString::new()
9192
/// .with_opt_value("q", Some("🍎 apple"))
9293
/// .with_opt_value("f", None::<String>)
93-
/// .with_opt_value("category", Some("fruits and vegetables"));
94+
/// .with_opt_value("category", Some("fruits and vegetables"))
95+
/// .with_opt_value("works", Some(true));
9496
///
9597
/// assert_eq!(
9698
/// format!("https://example.com/{qs}"),
97-
/// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables"
99+
/// "https://example.com/?q=%F0%9F%8D%8E%20apple&category=fruits%20and%20vegetables&works=true"
98100
/// );
99101
/// ```
100-
pub fn with_opt_value<K: Into<String>, V: Into<String>>(
102+
pub fn with_opt_value<K: ToString, V: ToString>(
101103
self,
102104
key: K,
103105
value: Option<V>,
@@ -125,10 +127,10 @@ impl QueryString {
125127
/// "https://example.com/?q=apple&category=fruits%20and%20vegetables"
126128
/// );
127129
/// ```
128-
pub fn push<K: Into<String>, V: Into<String>>(&mut self, key: K, value: V) -> &Self {
130+
pub fn push<K: ToString, V: ToString>(&mut self, key: K, value: V) -> &Self {
129131
self.pairs.push(Kvp {
130-
key: key.into(),
131-
value: value.into(),
132+
key: key.to_string(),
133+
value: value.to_string(),
132134
});
133135
self
134136
}
@@ -149,7 +151,7 @@ impl QueryString {
149151
/// "https://example.com/?q=%F0%9F%8D%8E%20apple"
150152
/// );
151153
/// ```
152-
pub fn push_opt<K: Into<String>, V: Into<String>>(
154+
pub fn push_opt<K: ToString, V: ToString>(
153155
&mut self,
154156
key: K,
155157
value: Option<V>,
@@ -262,8 +264,9 @@ mod tests {
262264
fn test_encoding() {
263265
let qs = QueryString::new()
264266
.with_value("q", "Grünkohl")
265-
.with_value("category", "Gemüse");
266-
assert_eq!(qs.to_string(), "?q=Gr%C3%BCnkohl&category=Gem%C3%BCse");
267+
.with_value("category", "Gemüse")
268+
.with_value("answer", 42);
269+
assert_eq!(qs.to_string(), "?q=Gr%C3%BCnkohl&category=Gem%C3%BCse&answer=42");
267270
}
268271

269272
#[test]

0 commit comments

Comments
 (0)