sapiens_tools 0.10.2

The tools - Sapiens
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
use std::fmt::{Display, Formatter};

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// Google Custom Search Engine URL
pub const URL: &str = "https://customsearch.googleapis.com/customsearch/v1";

/// Error details
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ErrorDetail {
    /// Error code
    pub code: u16,
    /// Error message
    pub message: String,
    /// Status
    pub status: String,
}

/// Error body
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ErrorBody {
    /// Error details
    pub error: ErrorDetail,
}

/// Enable Simplified/Traditional Chinese
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum C2COff {
    /// Disabled
    Off,
    /// Enable Simplified and Traditional Chinese Search
    On,
}

impl Display for C2COff {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            C2COff::Off => write!(f, "0"),
            C2COff::On => write!(f, "1"),
        }
    }
}

/// Country Restriction
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Cr {
    /// Country code - See https://developers.google.com/custom-search/docs/json_api_reference#countryCollections
    Country(String),
    /// Not
    Not(Box<Cr>),
    /// And
    And(Box<Cr>, Box<Cr>),
    /// Or
    Or(Box<Cr>, Box<Cr>),
}

impl Display for Cr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Cr::Country(country) => write!(f, "{}", country),
            Cr::Not(cr) => write!(f, "-{}", cr),
            Cr::And(cr1, cr2) => write!(f, "({}).({})", cr1, cr2),
            Cr::Or(cr1, cr2) => write!(f, "({}|{})", cr1, cr2),
        }
    }
}

/// Date Restrict
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum DateRestrict {
    /// From the specified number of past days
    Day(u32),
    /// From the specified number of past weeks
    Week(u32),
    /// From the specified number of past months
    Month(u32),
    /// From the specified number of past years
    Year(u32),
}

impl Display for DateRestrict {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            DateRestrict::Day(day) => write!(f, "d{}", day),
            DateRestrict::Week(week) => write!(f, "w{}", week),
            DateRestrict::Month(month) => write!(f, "m{}", month),
            DateRestrict::Year(year) => write!(f, "y{}", year),
        }
    }
}

/// Duplicate Content Filter
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum DuplicateContentFilter {
    /// Off
    Off,
    /// On
    On,
}

impl Display for DuplicateContentFilter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            DuplicateContentFilter::Off => write!(f, "0"),
            DuplicateContentFilter::On => write!(f, "1"),
        }
    }
}

/// Geolocation of end user
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Gl {
    /// Country code - See https://developers.google.com/custom-search/docs/json_api_reference#country-codes
    /// (2 letters lower case).
    CountryCode(String),
}

impl Display for Gl {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Gl::CountryCode(country_code) => write!(f, "{}", country_code),
        }
    }
}

/// Image Color Type
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum ImgColorType {
    /// Color
    Color,
    /// Gray
    Gray,
    /// Mono
    Mono,
    /// Transparent
    Trans,
}

impl Display for ImgColorType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ImgColorType::Color => {
                write!(f, "color")
            }
            ImgColorType::Gray => {
                write!(f, "gray")
            }
            ImgColorType::Mono => {
                write!(f, "mono")
            }
            ImgColorType::Trans => {
                write!(f, "trans")
            }
        }
    }
}

/// Image dominant color
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum ImgDominantColor {
    /// Black
    Black,
    /// Blue
    Blue,
    /// Brown
    Brown,
    /// Gray
    Gray,
    /// Green
    Green,
    /// Orange
    Orange,
    /// Pink
    Pink,
    /// Purple
    Purple,
    /// Red
    Red,
    /// Teal
    Teal,
    /// White
    White,
    /// Yellow
    Yellow,
}

impl Display for ImgDominantColor {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ImgDominantColor::Black => {
                write!(f, "black")
            }
            ImgDominantColor::Blue => {
                write!(f, "blue")
            }
            ImgDominantColor::Brown => {
                write!(f, "brown")
            }
            ImgDominantColor::Gray => {
                write!(f, "gray")
            }
            ImgDominantColor::Green => {
                write!(f, "green")
            }
            ImgDominantColor::Orange => {
                write!(f, "orange")
            }
            ImgDominantColor::Pink => {
                write!(f, "pink")
            }
            ImgDominantColor::Purple => {
                write!(f, "purple")
            }
            ImgDominantColor::Red => {
                write!(f, "red")
            }
            ImgDominantColor::Teal => {
                write!(f, "teal")
            }
            ImgDominantColor::White => {
                write!(f, "white")
            }
            ImgDominantColor::Yellow => {
                write!(f, "yellow")
            }
        }
    }
}

/// Image Size
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum ImgSize {
    /// Icon
    Icon,
    /// Small
    Small,
    /// Medium
    Medium,
    /// Large
    Large,
    /// Xlarge
    Xlarge,
    /// Xxlarge
    Xxlarge,
    /// Huge
    Huge,
}

impl Display for ImgSize {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ImgSize::Icon => {
                write!(f, "icon")
            }
            ImgSize::Small => {
                write!(f, "small")
            }
            ImgSize::Medium => {
                write!(f, "medium")
            }
            ImgSize::Large => {
                write!(f, "large")
            }
            ImgSize::Xlarge => {
                write!(f, "xlarge")
            }
            ImgSize::Xxlarge => {
                write!(f, "xxlarge")
            }
            ImgSize::Huge => {
                write!(f, "huge")
            }
        }
    }
}

/// Image Type
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum ImgType {
    /// Face
    Face,
    /// Photo
    Photo,
    /// Clipart
    Clipart,
    /// Lineart
    Lineart,
    /// Animated
    Animated,
    /// Stock
    Stock,
}

impl Display for ImgType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ImgType::Face => {
                write!(f, "face")
            }
            ImgType::Photo => {
                write!(f, "photo")
            }
            ImgType::Clipart => {
                write!(f, "clipart")
            }
            ImgType::Lineart => {
                write!(f, "lineart")
            }
            ImgType::Animated => {
                write!(f, "animated")
            }
            ImgType::Stock => {
                write!(f, "stock")
            }
        }
    }
}

/// Language Restriction
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum Lr {
    LangAr,
    LangBg,
    LangCa,
    LangCs,
    LangDa,
    LangDe,
    LangEl,
    LangEn,
    LangEs,
    LangEt,
    LangFi,
    LangFr,
    LangHr,
    LangHu,
    LangId,
    LangIs,
    LangIt,
    LangIw,
    LangJa,
    LangKo,
    LangLt,
    LangLv,
    LangNl,
    LangNo,
    LangPl,
    LangPt,
    LangRo,
    LangRu,
    LangSk,
    LangSl,
    LangSr,
    LangSv,
    LangTr,
    LangZhCN,
    LangZhTW,
}

impl Display for Lr {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Lr::LangAr => write!(f, "lang_ar"),
            Lr::LangBg => write!(f, "lang_bg"),
            Lr::LangCa => write!(f, "lang_ca"),
            Lr::LangCs => write!(f, "lang_cs"),
            Lr::LangDa => write!(f, "lang_da"),
            Lr::LangDe => write!(f, "lang_de"),
            Lr::LangEl => write!(f, "lang_el"),
            Lr::LangEn => write!(f, "lang_en"),
            Lr::LangEs => write!(f, "lang_es"),
            Lr::LangEt => write!(f, "lang_et"),
            Lr::LangFi => write!(f, "lang_fi"),
            Lr::LangFr => write!(f, "lang_fr"),
            Lr::LangHr => write!(f, "lang_hr"),
            Lr::LangHu => write!(f, "lang_hu"),
            Lr::LangId => write!(f, "lang_id"),
            Lr::LangIs => write!(f, "lang_is"),
            Lr::LangIt => write!(f, "lang_it"),
            Lr::LangIw => write!(f, "lang_iw"),
            Lr::LangJa => write!(f, "lang_ja"),
            Lr::LangKo => write!(f, "lang_ko"),
            Lr::LangLt => write!(f, "lang_lt"),
            Lr::LangLv => write!(f, "lang_lv"),
            Lr::LangNl => write!(f, "lang_nl"),
            Lr::LangNo => write!(f, "lang_no"),
            Lr::LangPl => write!(f, "lang_pl"),
            Lr::LangPt => write!(f, "lang_pt"),
            Lr::LangRo => write!(f, "lang_ro"),
            Lr::LangRu => write!(f, "lang_ru"),
            Lr::LangSk => write!(f, "lang_sk"),
            Lr::LangSl => write!(f, "lang_sl"),
            Lr::LangSr => write!(f, "lang_sr"),
            Lr::LangSv => write!(f, "lang_sv"),
            Lr::LangTr => write!(f, "lang_tr"),
            Lr::LangZhCN => write!(f, "lang_zh-CN"),
            Lr::LangZhTW => write!(f, "lang_zh-TW"),
        }
    }
}

impl TryFrom<&str> for Lr {
    type Error = &'static str;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        match value {
            "lang_ar" => Ok(Lr::LangAr),
            "lang_bg" => Ok(Lr::LangBg),
            "lang_ca" => Ok(Lr::LangCa),
            "lang_cs" => Ok(Lr::LangCs),
            "lang_da" => Ok(Lr::LangDa),
            "lang_de" => Ok(Lr::LangDe),
            "lang_el" => Ok(Lr::LangEl),
            "lang_en" => Ok(Lr::LangEn),
            "lang_es" => Ok(Lr::LangEs),
            "lang_et" => Ok(Lr::LangEt),
            "lang_fi" => Ok(Lr::LangFi),
            "lang_fr" => Ok(Lr::LangFr),
            "lang_hr" => Ok(Lr::LangHr),
            "lang_hu" => Ok(Lr::LangHu),
            "lang_id" => Ok(Lr::LangId),
            "lang_is" => Ok(Lr::LangIs),
            "lang_it" => Ok(Lr::LangIt),
            "lang_iw" => Ok(Lr::LangIw),
            "lang_ja" => Ok(Lr::LangJa),
            "lang_ko" => Ok(Lr::LangKo),
            "lang_lt" => Ok(Lr::LangLt),
            "lang_lv" => Ok(Lr::LangLv),
            "lang_nl" => Ok(Lr::LangNl),
            "lang_no" => Ok(Lr::LangNo),
            "lang_pl" => Ok(Lr::LangPl),
            "lang_pt" => Ok(Lr::LangPt),
            "lang_ro" => Ok(Lr::LangRo),
            "lang_ru" => Ok(Lr::LangRu),
            "lang_sk" => Ok(Lr::LangSk),
            "lang_sl" => Ok(Lr::LangSl),
            "lang_sr" => Ok(Lr::LangSr),
            "lang_sv" => Ok(Lr::LangSv),
            "lang_tr" => Ok(Lr::LangTr),
            "lang_zh-CN" => Ok(Lr::LangZhCN),
            "lang_zh-TW" => Ok(Lr::LangZhTW),
            _ => Err("Invalid value"),
        }
    }
}

/// Search Result Number
#[allow(missing_docs)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SearchResultNumber {
    One,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
}

impl TryFrom<u32> for SearchResultNumber {
    type Error = &'static str;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(SearchResultNumber::One),
            2 => Ok(SearchResultNumber::Two),
            3 => Ok(SearchResultNumber::Three),
            4 => Ok(SearchResultNumber::Four),
            5 => Ok(SearchResultNumber::Five),
            6 => Ok(SearchResultNumber::Six),
            7 => Ok(SearchResultNumber::Seven),
            8 => Ok(SearchResultNumber::Eight),
            9 => Ok(SearchResultNumber::Nine),
            10 => Ok(SearchResultNumber::Ten),
            _ => Err("Invalid search result number"),
        }
    }
}

impl Display for SearchResultNumber {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SearchResultNumber::One => write!(f, "1"),
            SearchResultNumber::Two => write!(f, "2"),
            SearchResultNumber::Three => write!(f, "3"),
            SearchResultNumber::Four => write!(f, "4"),
            SearchResultNumber::Five => write!(f, "5"),
            SearchResultNumber::Six => write!(f, "6"),
            SearchResultNumber::Seven => write!(f, "7"),
            SearchResultNumber::Eight => write!(f, "8"),
            SearchResultNumber::Nine => write!(f, "9"),
            SearchResultNumber::Ten => write!(f, "10"),
        }
    }
}

/// Rights Restriction
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Rights {
    /// Creative Commons Public Domain
    CcPublicdomain,
    /// Creative Commons Attribution (CC BY)
    CcAttribute,
    /// Creative Commons ShareAlike (CC SA)
    CcSharealike,
    /// Creative Commons NoDerivs (CC ND)
    CcNoncommercial,
    /// Creative Commons NonCommercial (CC NC)
    CcNonderived,
    /// And
    And(Box<Rights>, Box<Rights>),
}

impl Display for Rights {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Rights::CcPublicdomain => write!(f, "cc_publicdomain"),
            Rights::CcAttribute => write!(f, "cc_attribute"),
            Rights::CcSharealike => write!(f, "cc_sharealike"),
            Rights::CcNoncommercial => write!(f, "cc_noncommercial"),
            Rights::CcNonderived => write!(f, "cc_nonderived"),
            Rights::And(left, right) => write!(f, "{}%7C{}", left, right),
        }
    }
}

/// Safe Search
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Safe {
    /// Active
    Active,
    /// Off
    Off,
}

impl Display for Safe {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Safe::Active => write!(f, "active"),
            Safe::Off => write!(f, "off"),
        }
    }
}

/// Search Type
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SearchType {
    /// Image
    Image,
}

impl Display for SearchType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SearchType::Image => write!(f, "image"),
        }
    }
}

/// Site Search Filter
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum SiteSearchFilter {
    /// Exclude
    E,
    /// Include
    I,
}

impl Display for SiteSearchFilter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            SiteSearchFilter::E => write!(f, "e"),
            SiteSearchFilter::I => write!(f, "i"),
        }
    }
}

/// CSE query parameters
///
/// See https://developers.google.com/custom-search/v1/reference/rest/v1/cse/list
#[derive(Default, Clone, Serialize, Deserialize)]
pub struct QueryParameters {
    /// Simplified/Traditional Chinese
    c2coff: Option<C2COff>,
    /// Country restrict(s)
    cr: Option<Box<Cr>>,
    /// Programmable Search Engine ID
    cx: Option<String>,
    /// API key
    key: Option<String>,
    /// Date restrict(s)
    date_restrict: Option<String>,
    /// Exact terms
    exact_terms: Option<String>,
    /// Exclude terms
    exclude_terms: Option<String>,
    /// File type(s)
    file_type: Option<String>,
    /// Filter
    filter: Option<DuplicateContentFilter>,
    /// Geo location of end user
    gl: Option<Gl>,
    /// Low range of search results
    low_range: Option<String>,
    /// High range of search results
    high_range: Option<String>,
    /// Interface language
    hl: Option<String>,
    /// 'And' query terms
    hq: Option<String>,

    /// Image color type
    img_color_type: Option<ImgColorType>,
    /// Image dominant color
    img_dominant_color: Option<ImgDominantColor>,
    /// Image size
    img_size: Option<ImgSize>,
    /// Image type
    img_type: Option<ImgType>,

    /// Link site(s)
    link_site: Option<String>,

    /// Language restrict(s)
    lr: Option<Lr>,

    /// Number of search results to return
    num: Option<SearchResultNumber>,

    /// 'Or' query terms
    or_terms: Option<String>,

    /// Query
    q: Option<String>,

    /// Related site(s)
    related_site: Option<String>,

    /// Rights restrict(s)
    rights: Option<Box<Rights>>,

    /// Search Safety Level
    safe: Option<Safe>,

    /// Search type
    search_type: Option<SearchType>,

    /// Site restrict(s)
    site_search: Option<String>,

    /// Site search filter
    site_search_filter: Option<SiteSearchFilter>,

    /// Sort by
    sort: Option<String>,

    /// Start index
    start: Option<u32>,
}

impl QueryParameters {
    /// Build a QueryParameters
    pub fn build(&mut self) -> Self {
        self.clone()
    }

    /// Create a new QueryParameters
    pub fn builder() -> QueryParameters {
        QueryParameters {
            c2coff: None,
            cr: None,
            cx: None,
            key: None,
            date_restrict: None,
            exact_terms: None,
            exclude_terms: None,
            file_type: None,
            filter: None,
            gl: None,
            low_range: None,
            high_range: None,
            hl: None,
            hq: None,
            img_color_type: None,
            img_dominant_color: None,
            img_size: None,
            img_type: None,
            link_site: None,
            lr: None,
            num: None,
            or_terms: None,
            q: None,
            related_site: None,
            rights: None,
            safe: None,
            search_type: None,
            site_search: None,
            site_search_filter: None,
            sort: None,
            start: None,
        }
    }

    /// Convert QueryParameters to a Vec of (String, String) tuples
    pub fn to_parameters(&self) -> Vec<(String, String)> {
        let mut params = vec![];

        if let Some(key) = &self.key {
            params.push(("key".to_string(), key.to_string()));
        }

        if let Some(cx) = &self.cx {
            params.push(("cx".to_string(), cx.to_string()));
        }

        if let Some(c2coff) = &self.c2coff {
            params.push(("c2coff".to_string(), c2coff.to_string()));
        }

        if let Some(cr) = &self.cr {
            params.push(("cr".to_string(), cr.to_string()));
        }

        if let Some(date_restrict) = &self.date_restrict {
            params.push(("dateRestrict".to_string(), date_restrict.to_string()));
        }

        if let Some(exact_terms) = &self.exact_terms {
            params.push(("exactTerms".to_string(), exact_terms.to_string()));
        }

        if let Some(exclude_terms) = &self.exclude_terms {
            params.push(("excludeTerms".to_string(), exclude_terms.to_string()));
        }

        if let Some(file_type) = &self.file_type {
            params.push(("fileType".to_string(), file_type.to_string()));
        }

        if let Some(filter) = &self.filter {
            params.push(("filter".to_string(), filter.to_string()));
        }

        if let Some(gl) = &self.gl {
            params.push(("gl".to_string(), gl.to_string()));
        }

        if let Some(low_range) = &self.low_range {
            params.push(("lowRange".to_string(), low_range.to_string()));
        }

        if let Some(high_range) = &self.high_range {
            params.push(("highRange".to_string(), high_range.to_string()));
        }

        if let Some(hl) = &self.hl {
            params.push(("hl".to_string(), hl.to_string()));
        }

        if let Some(hq) = &self.hq {
            params.push(("hq".to_string(), hq.to_string()));
        }

        if let Some(img_color_type) = &self.img_color_type {
            params.push(("imgColorType".to_string(), img_color_type.to_string()));
        }

        if let Some(img_dominant_color) = &self.img_dominant_color {
            params.push((
                "imgDominantColor".to_string(),
                img_dominant_color.to_string(),
            ));
        }

        if let Some(img_size) = &self.img_size {
            params.push(("imgSize".to_string(), img_size.to_string()));
        }

        if let Some(img_type) = &self.img_type {
            params.push(("imgType".to_string(), img_type.to_string()));
        }

        if let Some(link_site) = &self.link_site {
            params.push(("linkSite".to_string(), link_site.to_string()));
        }

        if let Some(lr) = &self.lr {
            params.push(("lr".to_string(), lr.to_string()));
        }

        if let Some(num) = &self.num {
            params.push(("num".to_string(), num.to_string()));
        }

        if let Some(or_terms) = &self.or_terms {
            params.push(("orTerms".to_string(), or_terms.to_string()));
        }

        if let Some(q) = &self.q {
            params.push(("q".to_string(), q.to_string()));
        }

        if let Some(related_site) = &self.related_site {
            params.push(("relatedSite".to_string(), related_site.to_string()));
        }

        if let Some(rights) = &self.rights {
            params.push(("rights".to_string(), rights.to_string()));
        }

        if let Some(safe) = &self.safe {
            params.push(("safe".to_string(), safe.to_string()));
        }

        if let Some(search_type) = &self.search_type {
            params.push(("searchType".to_string(), search_type.to_string()));
        }

        if let Some(site_search) = &self.site_search {
            params.push(("siteSearch".to_string(), site_search.to_string()));
        }

        if let Some(site_search_filter) = &self.site_search_filter {
            params.push((
                "siteSearchFilter".to_string(),
                site_search_filter.to_string(),
            ));
        }

        if let Some(sort) = &self.sort {
            params.push(("sort".to_string(), sort.to_string()));
        }

        if let Some(start) = &self.start {
            params.push(("start".to_string(), start.to_string()));
        }

        params
    }

    /// Simplified/Traditional Chinese Search
    pub fn c2coff(&mut self, c2coff: C2COff) -> &mut Self {
        self.c2coff = Some(c2coff);
        self
    }

    /// Country restrict(s)
    pub fn cr(&mut self, cr: Box<Cr>) -> &mut Self {
        self.cr = Some(cr);
        self
    }

    /// The custom search engine ID to scope this search query
    pub fn cx(&mut self, cx: impl AsRef<str>) -> &mut Self {
        self.cx = Some(cx.as_ref().to_string());
        self
    }

    /// API key
    pub fn key(&mut self, key: impl AsRef<str>) -> &mut Self {
        self.key = Some(key.as_ref().to_string());
        self
    }

    /// Date restrict(s)
    pub fn date_restrict(&mut self, date_restrict: String) -> &mut Self {
        self.date_restrict = Some(date_restrict);
        self
    }

    /// Identifies a phrase that all documents in the search results must
    /// contain
    pub fn exact_terms(&mut self, exact_terms: impl AsRef<str>) -> &mut Self {
        self.exact_terms = Some(exact_terms.as_ref().to_string());
        self
    }

    /// Identifies a word or phrase that should not appear in any documents
    /// in the search results
    pub fn exclude_terms(&mut self, exclude_terms: impl AsRef<str>) -> &mut Self {
        self.exclude_terms = Some(exclude_terms.as_ref().to_string());
        self
    }

    /// Returns images of a specified type. Some of the allowed values are:
    /// bmp, gif, png, jpg, svg, pdf, ...
    pub fn file_type(&mut self, file_type: String) -> &mut Self {
        self.file_type = Some(file_type);
        self
    }

    /// Controls turning on or off the duplicate content filter.
    pub fn filter(&mut self, filter: DuplicateContentFilter) -> &mut Self {
        self.filter = Some(filter);
        self
    }

    /// Geolocation of end user.
    pub fn gl(&mut self, gl: Gl) -> &mut Self {
        self.gl = Some(gl);
        self
    }

    /// Specifies the ending value for a search range.
    pub fn high_range(&mut self, high_range: String) -> &mut Self {
        self.high_range = Some(high_range);
        self
    }

    /// Sets the user interface language.
    pub fn hl(&mut self, hl: String) -> &mut Self {
        self.hl = Some(hl);
        self
    }

    /// Appends the specified query terms to the query, as if they were
    pub fn hq(&mut self, hq: String) -> &mut Self {
        self.hq = Some(hq);
        self
    }

    /// Restricts results to images of a specified color type.
    pub fn img_color_type(&mut self, img_color_type: ImgColorType) -> &mut Self {
        self.img_color_type = Some(img_color_type);
        self
    }

    /// Restricts results to images with a specific dominant color.
    pub fn img_dominant_color(&mut self, img_dominant_color: ImgDominantColor) -> &mut Self {
        self.img_dominant_color = Some(img_dominant_color);
        self
    }

    /// Restricts results to images of a specified size.
    pub fn img_size(&mut self, img_size: ImgSize) -> &mut Self {
        self.img_size = Some(img_size);
        self
    }

    /// Restricts results to images of a specified type.
    pub fn img_type(&mut self, img_type: ImgType) -> &mut Self {
        self.img_type = Some(img_type);
        self
    }

    /// Specifies that all search results should contain a link to a
    /// particular URL
    pub fn link_site(&mut self, link_site: String) -> &mut Self {
        self.link_site = Some(link_site);
        self
    }

    /// Restricts the search to documents written in a particular language
    pub fn lr(&mut self, lr: Lr) -> &mut Self {
        self.lr = Some(lr);
        self
    }

    /// Number of search results to return
    pub fn num(&mut self, num: SearchResultNumber) -> &mut Self {
        self.num = Some(num);
        self
    }

    /// Provides additional search terms to check for in a document, where
    /// each document in the search results must contain at least one of the
    /// additional search terms
    pub fn or_terms(&mut self, or_terms: String) -> &mut Self {
        self.or_terms = Some(or_terms);
        self
    }

    /// Query
    pub fn q(&mut self, q: impl AsRef<str>) -> &mut Self {
        self.q = Some(q.as_ref().to_string());
        self
    }

    /// Specifies that all search results should be pages that are related
    /// to the specified URL

    pub fn related_site(&mut self, related_site: String) -> &mut Self {
        self.related_site = Some(related_site);
        self
    }

    /// Filters based on licensing. Supported values include:
    /// cc_publicdomain, cc_attribute, cc_sharealike,
    /// cc_noncommercial, cc_nonderived and combinations of these.
    pub fn rights(&mut self, rights: Rights) -> &mut Self {
        self.rights = Some(Box::new(rights));
        self
    }

    /// Search safety level
    pub fn safe(&mut self, safe: Safe) -> &mut Self {
        self.safe = Some(safe);
        self
    }

    /// Specifies the search type: image.
    pub fn search_type(&mut self, search_type: SearchType) -> &mut Self {
        self.search_type = Some(search_type);
        self
    }

    /// Specifies a given site which should always be included or excluded
    /// from results
    pub fn site_search(&mut self, site_search: String) -> &mut Self {
        self.site_search = Some(site_search);
        self
    }

    /// Controls whether to include or exclude results from the site named
    pub fn site_search_filter(&mut self, site_search_filter: SiteSearchFilter) -> &mut Self {
        self.site_search_filter = Some(site_search_filter);
        self
    }

    /// The sort expression to apply to the results
    pub fn sort(&mut self, sort: String) -> &mut Self {
        self.sort = Some(sort);
        self
    }

    /// The index of the first result to return
    pub fn start(&mut self, start: u32) -> &mut Self {
        self.start = Some(start);
        self
    }

    /// Build query for next page of results.
    pub fn next_page(&mut self, n: NextPage) -> &mut Self {
        self.start = Some(n.start);
        self
    }
}

/// A search result item.
#[derive(Debug, Deserialize, Serialize)]
pub struct Item {
    /// The title of the search result, in plain text.
    pub title: String,
    /// The URL of the result.
    pub link: String,
    /// The snippet of the result, in plain text.
    pub snippet: String,
}

/// The next page of results.
pub struct NextPage {
    /// The index of the first result.
    pub start: u32,
}

/// A search result.
///
/// The complete documentation is available at https://developers.google.com/custom-search/v1/reference/rest/v1/Search
#[derive(Debug, Deserialize, Serialize)]
pub struct SearchResults {
    /// The search results.
    pub items: Vec<Item>,

    #[serde(flatten)]
    other: Map<String, Value>,
}

impl SearchResults {
    /// Returns the [`NextPage`] so one can construct a new query to get the
    /// next page of results.
    ///
    /// See [`QueryParameters::next_page`].
    pub fn next_page(&self) -> Option<NextPage> {
        self.other
            .get("queries")
            .and_then(|queries| queries.get("nextPage"))
            .and_then(|next_page| next_page.get(0))
            .and_then(|next_page| next_page.get("startIndex"))
            .and_then(|start_index| start_index.as_u64())
            .map(|start_index| NextPage {
                start: start_index as u32,
            })
    }
}