1
+ use std:: borrow:: Cow ;
2
+
1
3
use crate :: Builder ;
2
4
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 > {
8
6
if ",.:()" . chars ( ) . any ( |c| param. contains ( c) ) {
9
- format ! ( "\" {}\" " , param)
7
+ format ! ( "\" {}\" " , param) . into ( )
10
8
} else {
11
- param
9
+ param. into ( )
12
10
}
13
11
}
14
12
@@ -32,13 +30,13 @@ impl Builder {
32
30
/// ```
33
31
pub fn not < T , U , V > ( mut self , operator : T , column : U , filter : V ) -> Self
34
32
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 > ,
38
36
{
39
37
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 ( ) ) ) ,
42
40
) ) ;
43
41
self
44
42
}
@@ -69,10 +67,10 @@ impl Builder {
69
67
/// ```
70
68
pub fn and < T > ( mut self , filters : T ) -> Self
71
69
where
72
- T : Into < String > ,
70
+ T : AsRef < str > ,
73
71
{
74
72
self . queries
75
- . push ( ( "and" . to_string ( ) , format ! ( "({})" , filters. into ( ) ) ) ) ;
73
+ . push ( ( "and" . to_string ( ) , format ! ( "({})" , filters. as_ref ( ) ) ) ) ;
76
74
self
77
75
}
78
76
@@ -102,10 +100,10 @@ impl Builder {
102
100
/// ```
103
101
pub fn or < T > ( mut self , filters : T ) -> Self
104
102
where
105
- T : Into < String > ,
103
+ T : AsRef < str > ,
106
104
{
107
105
self . queries
108
- . push ( ( "or" . to_string ( ) , format ! ( "({})" , filters. into ( ) ) ) ) ;
106
+ . push ( ( "or" . to_string ( ) , format ! ( "({})" , filters. as_ref ( ) ) ) ) ;
109
107
self
110
108
}
111
109
@@ -129,11 +127,13 @@ impl Builder {
129
127
/// ```
130
128
pub fn eq < T , U > ( mut self , column : T , filter : U ) -> Self
131
129
where
132
- T : Into < String > ,
133
- U : Into < String > ,
130
+ T : AsRef < str > ,
131
+ U : AsRef < str > ,
134
132
{
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
+ ) ) ;
137
137
self
138
138
}
139
139
@@ -157,11 +157,13 @@ impl Builder {
157
157
/// ```
158
158
pub fn neq < T , U > ( mut self , column : T , filter : U ) -> Self
159
159
where
160
- T : Into < String > ,
161
- U : Into < String > ,
160
+ T : AsRef < str > ,
161
+ U : AsRef < str > ,
162
162
{
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
+ ) ) ;
165
167
self
166
168
}
167
169
@@ -185,11 +187,13 @@ impl Builder {
185
187
/// ```
186
188
pub fn gt < T , U > ( mut self , column : T , filter : U ) -> Self
187
189
where
188
- T : Into < String > ,
189
- U : Into < String > ,
190
+ T : AsRef < str > ,
191
+ U : AsRef < str > ,
190
192
{
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
+ ) ) ;
193
197
self
194
198
}
195
199
@@ -213,11 +217,13 @@ impl Builder {
213
217
/// ```
214
218
pub fn gte < T , U > ( mut self , column : T , filter : U ) -> Self
215
219
where
216
- T : Into < String > ,
217
- U : Into < String > ,
220
+ T : AsRef < str > ,
221
+ U : AsRef < str > ,
218
222
{
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
+ ) ) ;
221
227
self
222
228
}
223
229
@@ -241,11 +247,13 @@ impl Builder {
241
247
/// ```
242
248
pub fn lt < T , U > ( mut self , column : T , filter : U ) -> Self
243
249
where
244
- T : Into < String > ,
245
- U : Into < String > ,
250
+ T : AsRef < str > ,
251
+ U : AsRef < str > ,
246
252
{
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
+ ) ) ;
249
257
self
250
258
}
251
259
@@ -269,11 +277,13 @@ impl Builder {
269
277
/// ```
270
278
pub fn lte < T , U > ( mut self , column : T , filter : U ) -> Self
271
279
where
272
- T : Into < String > ,
273
- U : Into < String > ,
280
+ T : AsRef < str > ,
281
+ U : AsRef < str > ,
274
282
{
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
+ ) ) ;
277
287
self
278
288
}
279
289
@@ -304,12 +314,14 @@ impl Builder {
304
314
/// ```
305
315
pub fn like < T , U > ( mut self , column : T , pattern : U ) -> Self
306
316
where
307
- T : Into < String > ,
317
+ T : AsRef < str > ,
308
318
U : Into < String > ,
309
319
{
310
320
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
+ ) ) ;
313
325
self
314
326
}
315
327
@@ -340,12 +352,14 @@ impl Builder {
340
352
/// ```
341
353
pub fn ilike < T , U > ( mut self , column : T , pattern : U ) -> Self
342
354
where
343
- T : Into < String > ,
355
+ T : AsRef < str > ,
344
356
U : Into < String > ,
345
357
{
346
358
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
+ ) ) ;
349
363
self
350
364
}
351
365
@@ -369,11 +383,13 @@ impl Builder {
369
383
/// ```
370
384
pub fn is < T , U > ( mut self , column : T , filter : U ) -> Self
371
385
where
372
- T : Into < String > ,
373
- U : Into < String > ,
386
+ T : AsRef < str > ,
387
+ U : AsRef < str > ,
374
388
{
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
+ ) ) ;
377
393
self
378
394
}
379
395
@@ -411,13 +427,18 @@ impl Builder {
411
427
/// ```
412
428
pub fn in_ < T , U , V > ( mut self , column : T , values : U ) -> Self
413
429
where
414
- T : Into < String > ,
430
+ T : AsRef < str > ,
415
431
U : IntoIterator < Item = V > ,
416
- V : Into < String > ,
432
+ V : AsRef < str > ,
417
433
{
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
+ ) ) ;
421
442
self
422
443
}
423
444
@@ -441,11 +462,13 @@ impl Builder {
441
462
/// ```
442
463
pub fn cs < T , U > ( mut self , column : T , filter : U ) -> Self
443
464
where
444
- T : Into < String > ,
445
- U : Into < String > ,
465
+ T : AsRef < str > ,
466
+ U : AsRef < str > ,
446
467
{
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
+ ) ) ;
449
472
self
450
473
}
451
474
@@ -470,10 +493,10 @@ impl Builder {
470
493
pub fn cd < T , U > ( mut self , column : T , filter : U ) -> Self
471
494
where
472
495
T : Into < String > ,
473
- U : Into < String > ,
496
+ U : AsRef < str > ,
474
497
{
475
498
self . queries
476
- . push ( ( column. into ( ) , format ! ( "cd.{}" , filter. into ( ) ) ) ) ;
499
+ . push ( ( column. into ( ) , format ! ( "cd.{}" , filter. as_ref ( ) ) ) ) ;
477
500
self
478
501
}
479
502
@@ -633,10 +656,10 @@ impl Builder {
633
656
pub fn ov < T , U > ( mut self , column : T , filter : U ) -> Self
634
657
where
635
658
T : Into < String > ,
636
- U : Into < String > ,
659
+ U : AsRef < str > ,
637
660
{
638
661
self . queries
639
- . push ( ( column. into ( ) , format ! ( "cd.{}" , filter. into ( ) ) ) ) ;
662
+ . push ( ( column. into ( ) , format ! ( "cd.{}" , filter. as_ref ( ) ) ) ) ;
640
663
self
641
664
}
642
665
@@ -661,15 +684,15 @@ impl Builder {
661
684
pub fn fts < T , U > ( mut self , column : T , tsquery : U , config : Option < & str > ) -> Self
662
685
where
663
686
T : Into < String > ,
664
- U : Into < String > ,
687
+ U : AsRef < str > ,
665
688
{
666
689
let config = if let Some ( conf) = config {
667
690
format ! ( "({})" , conf)
668
691
} else {
669
692
String :: new ( )
670
693
} ;
671
694
self . queries
672
- . push ( ( column. into ( ) , format ! ( "fts{}.{}" , config, tsquery. into ( ) ) ) ) ;
695
+ . push ( ( column. into ( ) , format ! ( "fts{}.{}" , config, tsquery. as_ref ( ) ) ) ) ;
673
696
self
674
697
}
675
698
@@ -694,15 +717,17 @@ impl Builder {
694
717
pub fn plfts < T , U > ( mut self , column : T , tsquery : U , config : Option < & str > ) -> Self
695
718
where
696
719
T : Into < String > ,
697
- U : Into < String > ,
720
+ U : AsRef < str > ,
698
721
{
699
722
let config = if let Some ( conf) = config {
700
723
format ! ( "({})" , conf)
701
724
} else {
702
725
String :: new ( )
703
726
} ;
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
+ ) ) ;
706
731
self
707
732
}
708
733
@@ -727,15 +752,17 @@ impl Builder {
727
752
pub fn phfts < T , U > ( mut self , column : T , tsquery : U , config : Option < & str > ) -> Self
728
753
where
729
754
T : Into < String > ,
730
- U : Into < String > ,
755
+ U : AsRef < str > ,
731
756
{
732
757
let config = if let Some ( conf) = config {
733
758
format ! ( "({})" , conf)
734
759
} else {
735
760
String :: new ( )
736
761
} ;
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
+ ) ) ;
739
766
self
740
767
}
741
768
@@ -760,15 +787,17 @@ impl Builder {
760
787
pub fn wfts < T , U > ( mut self , column : T , tsquery : U , config : Option < & str > ) -> Self
761
788
where
762
789
T : Into < String > ,
763
- U : Into < String > ,
790
+ U : AsRef < str > ,
764
791
{
765
792
let config = if let Some ( conf) = config {
766
793
format ! ( "({})" , conf)
767
794
} else {
768
795
String :: new ( )
769
796
} ;
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
+ ) ) ;
772
801
self
773
802
}
774
803
}
0 commit comments