Skip to content

Commit e5bf91f

Browse files
tjg1soedirgo
authored andcommitted
fix: Use AsRef trait where ownership is not needed
1 parent 309531b commit e5bf91f

File tree

3 files changed

+109
-80
lines changed

3 files changed

+109
-80
lines changed

src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ impl Builder {
5050
/// ```
5151
pub fn auth<T>(mut self, token: T) -> Self
5252
where
53-
T: Into<String>,
53+
T: AsRef<str>,
5454
{
5555
self.headers.insert(
5656
"Authorization",
57-
HeaderValue::from_str(&format!("Bearer {}", token.into())).unwrap(),
57+
HeaderValue::from_str(&format!("Bearer {}", token.as_ref())).unwrap(),
5858
);
5959
self
6060
}

src/filter.rs

Lines changed: 103 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1+
use std::borrow::Cow;
2+
13
use crate::Builder;
24

3-
fn clean_param<T>(param: T) -> String
4-
where
5-
T: Into<String>,
6-
{
7-
let param = param.into();
5+
fn clean_param(param: &str) -> Cow<str> {
86
if ",.:()".chars().any(|c| param.contains(c)) {
9-
format!("\"{}\"", param)
7+
format!("\"{}\"", param).into()
108
} else {
11-
param
9+
param.into()
1210
}
1311
}
1412

@@ -32,13 +30,13 @@ impl Builder {
3230
/// ```
3331
pub fn not<T, U, V>(mut self, operator: T, column: U, filter: V) -> Self
3432
where
35-
T: Into<String>,
36-
U: Into<String>,
37-
V: Into<String>,
33+
T: AsRef<str>,
34+
U: AsRef<str>,
35+
V: AsRef<str>,
3836
{
3937
self.queries.push((
40-
clean_param(column),
41-
format!("not.{}.{}", operator.into(), clean_param(filter)),
38+
clean_param(column.as_ref()).into(),
39+
format!("not.{}.{}", operator.as_ref(), clean_param(filter.as_ref())),
4240
));
4341
self
4442
}
@@ -69,10 +67,10 @@ impl Builder {
6967
/// ```
7068
pub fn and<T>(mut self, filters: T) -> Self
7169
where
72-
T: Into<String>,
70+
T: AsRef<str>,
7371
{
7472
self.queries
75-
.push(("and".to_string(), format!("({})", filters.into())));
73+
.push(("and".to_string(), format!("({})", filters.as_ref())));
7674
self
7775
}
7876

@@ -102,10 +100,10 @@ impl Builder {
102100
/// ```
103101
pub fn or<T>(mut self, filters: T) -> Self
104102
where
105-
T: Into<String>,
103+
T: AsRef<str>,
106104
{
107105
self.queries
108-
.push(("or".to_string(), format!("({})", filters.into())));
106+
.push(("or".to_string(), format!("({})", filters.as_ref())));
109107
self
110108
}
111109

@@ -129,11 +127,13 @@ impl Builder {
129127
/// ```
130128
pub fn eq<T, U>(mut self, column: T, filter: U) -> Self
131129
where
132-
T: Into<String>,
133-
U: Into<String>,
130+
T: AsRef<str>,
131+
U: AsRef<str>,
134132
{
135-
self.queries
136-
.push((clean_param(column), format!("eq.{}", clean_param(filter))));
133+
self.queries.push((
134+
clean_param(column.as_ref()).into(),
135+
format!("eq.{}", clean_param(filter.as_ref())),
136+
));
137137
self
138138
}
139139

@@ -157,11 +157,13 @@ impl Builder {
157157
/// ```
158158
pub fn neq<T, U>(mut self, column: T, filter: U) -> Self
159159
where
160-
T: Into<String>,
161-
U: Into<String>,
160+
T: AsRef<str>,
161+
U: AsRef<str>,
162162
{
163-
self.queries
164-
.push((clean_param(column), format!("neq.{}", clean_param(filter))));
163+
self.queries.push((
164+
clean_param(column.as_ref()).into(),
165+
format!("neq.{}", clean_param(filter.as_ref())),
166+
));
165167
self
166168
}
167169

@@ -185,11 +187,13 @@ impl Builder {
185187
/// ```
186188
pub fn gt<T, U>(mut self, column: T, filter: U) -> Self
187189
where
188-
T: Into<String>,
189-
U: Into<String>,
190+
T: AsRef<str>,
191+
U: AsRef<str>,
190192
{
191-
self.queries
192-
.push((clean_param(column), format!("gt.{}", clean_param(filter))));
193+
self.queries.push((
194+
clean_param(column.as_ref()).into(),
195+
format!("gt.{}", clean_param(filter.as_ref())),
196+
));
193197
self
194198
}
195199

@@ -213,11 +217,13 @@ impl Builder {
213217
/// ```
214218
pub fn gte<T, U>(mut self, column: T, filter: U) -> Self
215219
where
216-
T: Into<String>,
217-
U: Into<String>,
220+
T: AsRef<str>,
221+
U: AsRef<str>,
218222
{
219-
self.queries
220-
.push((clean_param(column), format!("gte.{}", clean_param(filter))));
223+
self.queries.push((
224+
clean_param(column.as_ref()).into(),
225+
format!("gte.{}", clean_param(filter.as_ref())),
226+
));
221227
self
222228
}
223229

@@ -241,11 +247,13 @@ impl Builder {
241247
/// ```
242248
pub fn lt<T, U>(mut self, column: T, filter: U) -> Self
243249
where
244-
T: Into<String>,
245-
U: Into<String>,
250+
T: AsRef<str>,
251+
U: AsRef<str>,
246252
{
247-
self.queries
248-
.push((clean_param(column), format!("lt.{}", clean_param(filter))));
253+
self.queries.push((
254+
clean_param(column.as_ref()).into(),
255+
format!("lt.{}", clean_param(filter.as_ref())),
256+
));
249257
self
250258
}
251259

@@ -269,11 +277,13 @@ impl Builder {
269277
/// ```
270278
pub fn lte<T, U>(mut self, column: T, filter: U) -> Self
271279
where
272-
T: Into<String>,
273-
U: Into<String>,
280+
T: AsRef<str>,
281+
U: AsRef<str>,
274282
{
275-
self.queries
276-
.push((clean_param(column), format!("lte.{}", clean_param(filter))));
283+
self.queries.push((
284+
clean_param(column.as_ref()).into(),
285+
format!("lte.{}", clean_param(filter.as_ref())),
286+
));
277287
self
278288
}
279289

@@ -304,12 +314,14 @@ impl Builder {
304314
/// ```
305315
pub fn like<T, U>(mut self, column: T, pattern: U) -> Self
306316
where
307-
T: Into<String>,
317+
T: AsRef<str>,
308318
U: Into<String>,
309319
{
310320
let pattern = pattern.into().replace('%', "*");
311-
self.queries
312-
.push((clean_param(column), format!("like.{}", pattern)));
321+
self.queries.push((
322+
clean_param(column.as_ref()).into(),
323+
format!("like.{}", pattern),
324+
));
313325
self
314326
}
315327

@@ -340,12 +352,14 @@ impl Builder {
340352
/// ```
341353
pub fn ilike<T, U>(mut self, column: T, pattern: U) -> Self
342354
where
343-
T: Into<String>,
355+
T: AsRef<str>,
344356
U: Into<String>,
345357
{
346358
let pattern = pattern.into().replace('%', "*");
347-
self.queries
348-
.push((clean_param(column), format!("ilike.{}", pattern)));
359+
self.queries.push((
360+
clean_param(column.as_ref()).into(),
361+
format!("ilike.{}", pattern),
362+
));
349363
self
350364
}
351365

@@ -369,11 +383,13 @@ impl Builder {
369383
/// ```
370384
pub fn is<T, U>(mut self, column: T, filter: U) -> Self
371385
where
372-
T: Into<String>,
373-
U: Into<String>,
386+
T: AsRef<str>,
387+
U: AsRef<str>,
374388
{
375-
self.queries
376-
.push((clean_param(column), format!("is.{}", clean_param(filter))));
389+
self.queries.push((
390+
clean_param(column.as_ref()).into(),
391+
format!("is.{}", clean_param(filter.as_ref())),
392+
));
377393
self
378394
}
379395

@@ -411,13 +427,18 @@ impl Builder {
411427
/// ```
412428
pub fn in_<T, U, V>(mut self, column: T, values: U) -> Self
413429
where
414-
T: Into<String>,
430+
T: AsRef<str>,
415431
U: IntoIterator<Item = V>,
416-
V: Into<String>,
432+
V: AsRef<str>,
417433
{
418-
let values: Vec<_> = values.into_iter().map(clean_param).collect();
419-
self.queries
420-
.push((clean_param(column), format!("in.({})", values.join(","))));
434+
let mut values: String = values
435+
.into_iter()
436+
.fold(String::new(), |a, s| a + &clean_param(s.as_ref()) + ",");
437+
values.pop();
438+
self.queries.push((
439+
clean_param(column.as_ref()).into(),
440+
format!("in.({})", values),
441+
));
421442
self
422443
}
423444

@@ -441,11 +462,13 @@ impl Builder {
441462
/// ```
442463
pub fn cs<T, U>(mut self, column: T, filter: U) -> Self
443464
where
444-
T: Into<String>,
445-
U: Into<String>,
465+
T: AsRef<str>,
466+
U: AsRef<str>,
446467
{
447-
self.queries
448-
.push((clean_param(column), format!("cs.{}", filter.into())));
468+
self.queries.push((
469+
clean_param(column.as_ref()).into(),
470+
format!("cs.{}", filter.as_ref()),
471+
));
449472
self
450473
}
451474

@@ -470,10 +493,10 @@ impl Builder {
470493
pub fn cd<T, U>(mut self, column: T, filter: U) -> Self
471494
where
472495
T: Into<String>,
473-
U: Into<String>,
496+
U: AsRef<str>,
474497
{
475498
self.queries
476-
.push((column.into(), format!("cd.{}", filter.into())));
499+
.push((column.into(), format!("cd.{}", filter.as_ref())));
477500
self
478501
}
479502

@@ -633,10 +656,10 @@ impl Builder {
633656
pub fn ov<T, U>(mut self, column: T, filter: U) -> Self
634657
where
635658
T: Into<String>,
636-
U: Into<String>,
659+
U: AsRef<str>,
637660
{
638661
self.queries
639-
.push((column.into(), format!("cd.{}", filter.into())));
662+
.push((column.into(), format!("cd.{}", filter.as_ref())));
640663
self
641664
}
642665

@@ -661,15 +684,15 @@ impl Builder {
661684
pub fn fts<T, U>(mut self, column: T, tsquery: U, config: Option<&str>) -> Self
662685
where
663686
T: Into<String>,
664-
U: Into<String>,
687+
U: AsRef<str>,
665688
{
666689
let config = if let Some(conf) = config {
667690
format!("({})", conf)
668691
} else {
669692
String::new()
670693
};
671694
self.queries
672-
.push((column.into(), format!("fts{}.{}", config, tsquery.into())));
695+
.push((column.into(), format!("fts{}.{}", config, tsquery.as_ref())));
673696
self
674697
}
675698

@@ -694,15 +717,17 @@ impl Builder {
694717
pub fn plfts<T, U>(mut self, column: T, tsquery: U, config: Option<&str>) -> Self
695718
where
696719
T: Into<String>,
697-
U: Into<String>,
720+
U: AsRef<str>,
698721
{
699722
let config = if let Some(conf) = config {
700723
format!("({})", conf)
701724
} else {
702725
String::new()
703726
};
704-
self.queries
705-
.push((column.into(), format!("plfts{}.{}", config, tsquery.into())));
727+
self.queries.push((
728+
column.into(),
729+
format!("plfts{}.{}", config, tsquery.as_ref()),
730+
));
706731
self
707732
}
708733

@@ -727,15 +752,17 @@ impl Builder {
727752
pub fn phfts<T, U>(mut self, column: T, tsquery: U, config: Option<&str>) -> Self
728753
where
729754
T: Into<String>,
730-
U: Into<String>,
755+
U: AsRef<str>,
731756
{
732757
let config = if let Some(conf) = config {
733758
format!("({})", conf)
734759
} else {
735760
String::new()
736761
};
737-
self.queries
738-
.push((column.into(), format!("phfts{}.{}", config, tsquery.into())));
762+
self.queries.push((
763+
column.into(),
764+
format!("phfts{}.{}", config, tsquery.as_ref()),
765+
));
739766
self
740767
}
741768

@@ -760,15 +787,17 @@ impl Builder {
760787
pub fn wfts<T, U>(mut self, column: T, tsquery: U, config: Option<&str>) -> Self
761788
where
762789
T: Into<String>,
763-
U: Into<String>,
790+
U: AsRef<str>,
764791
{
765792
let config = if let Some(conf) = config {
766793
format!("({})", conf)
767794
} else {
768795
String::new()
769796
};
770-
self.queries
771-
.push((column.into(), format!("wfts{}.{}", config, tsquery.into())));
797+
self.queries.push((
798+
column.into(),
799+
format!("wfts{}.{}", config, tsquery.as_ref()),
800+
));
772801
self
773802
}
774803
}

0 commit comments

Comments
 (0)