@@ -8,9 +8,9 @@ use golem_web_search::golem::web_search::types::{
8
8
pub fn convert_params_to_request ( params : & SearchParams , offset : Option < u32 > ) -> BraveSearchRequest {
9
9
let mut request = BraveSearchRequest {
10
10
q : params. query . clone ( ) ,
11
- country : params. region . clone ( ) ,
12
- search_lang : params. language . clone ( ) ,
13
- ui_lang : params . language . clone ( ) ,
11
+ country : country_code_to_brave ( params. region . as_ref ( ) . unwrap_or ( & "us" . to_string ( ) ) ) ,
12
+ search_lang : language_code_to_brave ( params. language . as_ref ( ) . unwrap_or ( & "en" . to_string ( ) ) ) ,
13
+ ui_lang : None ,
14
14
count : params. max_results ,
15
15
offset,
16
16
safesearch : params. safe_search . as_ref ( ) . map ( |s| match s {
@@ -54,6 +54,152 @@ pub fn convert_params_to_request(params: &SearchParams, offset: Option<u32>) ->
54
54
request
55
55
}
56
56
57
+ pub fn country_code_to_brave ( country_code : & str ) -> Option < String > {
58
+ match country_code. to_lowercase ( ) . as_str ( ) {
59
+ "us" | "usa" | "united states" => Some ( "us" . to_string ( ) ) ,
60
+ "uk" | "gb" | "united kingdom" => Some ( "gb" . to_string ( ) ) ,
61
+ "ca" | "canada" => Some ( "ca" . to_string ( ) ) ,
62
+ "au" | "australia" => Some ( "au" . to_string ( ) ) ,
63
+ "de" | "germany" => Some ( "de" . to_string ( ) ) ,
64
+ "fr" | "france" => Some ( "fr" . to_string ( ) ) ,
65
+ "es" | "spain" => Some ( "es" . to_string ( ) ) ,
66
+ "it" | "italy" => Some ( "it" . to_string ( ) ) ,
67
+ "jp" | "japan" => Some ( "jp" . to_string ( ) ) ,
68
+ "br" | "brazil" => Some ( "br" . to_string ( ) ) ,
69
+ "in" | "india" => Some ( "in" . to_string ( ) ) ,
70
+ "cn" | "china" => Some ( "cn" . to_string ( ) ) ,
71
+ "ru" | "russia" => Some ( "ru" . to_string ( ) ) ,
72
+ "mx" | "mexico" => Some ( "mx" . to_string ( ) ) ,
73
+ "ar" | "argentina" => Some ( "ar" . to_string ( ) ) ,
74
+ "cl" | "chile" => Some ( "cl" . to_string ( ) ) ,
75
+ "co" | "colombia" => Some ( "co" . to_string ( ) ) ,
76
+ "pe" | "peru" => Some ( "pe" . to_string ( ) ) ,
77
+ "za" | "south africa" => Some ( "za" . to_string ( ) ) ,
78
+ "ng" | "nigeria" => Some ( "ng" . to_string ( ) ) ,
79
+ "eg" | "egypt" => Some ( "eg" . to_string ( ) ) ,
80
+ "kr" | "south korea" => Some ( "kr" . to_string ( ) ) ,
81
+ "th" | "thailand" => Some ( "th" . to_string ( ) ) ,
82
+ "sg" | "singapore" => Some ( "sg" . to_string ( ) ) ,
83
+ "my" | "malaysia" => Some ( "my" . to_string ( ) ) ,
84
+ "id" | "indonesia" => Some ( "id" . to_string ( ) ) ,
85
+ "ph" | "philippines" => Some ( "ph" . to_string ( ) ) ,
86
+ "vn" | "vietnam" => Some ( "vn" . to_string ( ) ) ,
87
+ "tw" | "taiwan" => Some ( "tw" . to_string ( ) ) ,
88
+ "hk" | "hong kong" => Some ( "hk" . to_string ( ) ) ,
89
+ "nl" | "netherlands" => Some ( "nl" . to_string ( ) ) ,
90
+ "be" | "belgium" => Some ( "be" . to_string ( ) ) ,
91
+ "ch" | "switzerland" => Some ( "ch" . to_string ( ) ) ,
92
+ "at" | "austria" => Some ( "at" . to_string ( ) ) ,
93
+ "se" | "sweden" => Some ( "se" . to_string ( ) ) ,
94
+ "no" | "norway" => Some ( "no" . to_string ( ) ) ,
95
+ "dk" | "denmark" => Some ( "dk" . to_string ( ) ) ,
96
+ "fi" | "finland" => Some ( "fi" . to_string ( ) ) ,
97
+ "pl" | "poland" => Some ( "pl" . to_string ( ) ) ,
98
+ "cz" | "czech republic" => Some ( "cz" . to_string ( ) ) ,
99
+ "hu" | "hungary" => Some ( "hu" . to_string ( ) ) ,
100
+ "gr" | "greece" => Some ( "gr" . to_string ( ) ) ,
101
+ "pt" | "portugal" => Some ( "pt" . to_string ( ) ) ,
102
+ "tr" | "turkey" => Some ( "tr" . to_string ( ) ) ,
103
+ "il" | "israel" => Some ( "il" . to_string ( ) ) ,
104
+ "ae" | "uae" | "united arab emirates" => Some ( "ae" . to_string ( ) ) ,
105
+ "sa" | "saudi arabia" => Some ( "sa" . to_string ( ) ) ,
106
+ "nz" | "new zealand" => Some ( "nz" . to_string ( ) ) ,
107
+ _ => Some ( country_code. to_lowercase ( ) ) ,
108
+ }
109
+ }
110
+
111
+ pub fn language_code_to_brave ( language_code : & str ) -> Option < String > {
112
+ let input = language_code. to_lowercase ( ) ;
113
+
114
+ let lang_code = if input. starts_with ( "lang_" ) {
115
+ input. strip_prefix ( "lang_" ) . unwrap_or ( & input)
116
+ } else {
117
+ & input
118
+ } ;
119
+
120
+ match lang_code {
121
+ "en" | "english" => Some ( "en" . to_string ( ) ) ,
122
+ "es" | "spanish" => Some ( "es" . to_string ( ) ) ,
123
+ "fr" | "french" => Some ( "fr" . to_string ( ) ) ,
124
+ "de" | "german" => Some ( "de" . to_string ( ) ) ,
125
+ "it" | "italian" => Some ( "it" . to_string ( ) ) ,
126
+ "pt" | "portuguese" => Some ( "pt" . to_string ( ) ) ,
127
+ "ru" | "russian" => Some ( "ru" . to_string ( ) ) ,
128
+ "zh" | "chinese" => Some ( "zh" . to_string ( ) ) ,
129
+ "ja" | "japanese" => Some ( "ja" . to_string ( ) ) ,
130
+ "ko" | "korean" => Some ( "ko" . to_string ( ) ) ,
131
+ "ar" | "arabic" => Some ( "ar" . to_string ( ) ) ,
132
+ "hi" | "hindi" => Some ( "hi" . to_string ( ) ) ,
133
+ "th" | "thai" => Some ( "th" . to_string ( ) ) ,
134
+ "vi" | "vietnamese" => Some ( "vi" . to_string ( ) ) ,
135
+ "id" | "indonesian" => Some ( "id" . to_string ( ) ) ,
136
+ "ms" | "malay" => Some ( "ms" . to_string ( ) ) ,
137
+ "tl" | "tagalog" => Some ( "tl" . to_string ( ) ) ,
138
+ "nl" | "dutch" => Some ( "nl" . to_string ( ) ) ,
139
+ "sv" | "swedish" => Some ( "sv" . to_string ( ) ) ,
140
+ "no" | "norwegian" => Some ( "no" . to_string ( ) ) ,
141
+ "da" | "danish" => Some ( "da" . to_string ( ) ) ,
142
+ "fi" | "finnish" => Some ( "fi" . to_string ( ) ) ,
143
+ "pl" | "polish" => Some ( "pl" . to_string ( ) ) ,
144
+ "cs" | "czech" => Some ( "cs" . to_string ( ) ) ,
145
+ "hu" | "hungarian" => Some ( "hu" . to_string ( ) ) ,
146
+ "el" | "greek" => Some ( "el" . to_string ( ) ) ,
147
+ "tr" | "turkish" => Some ( "tr" . to_string ( ) ) ,
148
+ "he" | "hebrew" => Some ( "he" . to_string ( ) ) ,
149
+ "fa" | "persian" => Some ( "fa" . to_string ( ) ) ,
150
+ "ur" | "urdu" => Some ( "ur" . to_string ( ) ) ,
151
+ "bn" | "bengali" => Some ( "bn" . to_string ( ) ) ,
152
+ "ta" | "tamil" => Some ( "ta" . to_string ( ) ) ,
153
+ "te" | "telugu" => Some ( "te" . to_string ( ) ) ,
154
+ "ml" | "malayalam" => Some ( "ml" . to_string ( ) ) ,
155
+ "kn" | "kannada" => Some ( "kn" . to_string ( ) ) ,
156
+ "gu" | "gujarati" => Some ( "gu" . to_string ( ) ) ,
157
+ "pa" | "punjabi" => Some ( "pa" . to_string ( ) ) ,
158
+ "mr" | "marathi" => Some ( "mr" . to_string ( ) ) ,
159
+ "ne" | "nepali" => Some ( "ne" . to_string ( ) ) ,
160
+ "si" | "sinhala" => Some ( "si" . to_string ( ) ) ,
161
+ "my" | "myanmar" => Some ( "my" . to_string ( ) ) ,
162
+ "km" | "khmer" => Some ( "km" . to_string ( ) ) ,
163
+ "lo" | "lao" => Some ( "lo" . to_string ( ) ) ,
164
+ "ka" | "georgian" => Some ( "ka" . to_string ( ) ) ,
165
+ "hy" | "armenian" => Some ( "hy" . to_string ( ) ) ,
166
+ "az" | "azerbaijani" => Some ( "az" . to_string ( ) ) ,
167
+ "kk" | "kazakh" => Some ( "kk" . to_string ( ) ) ,
168
+ "ky" | "kyrgyz" => Some ( "ky" . to_string ( ) ) ,
169
+ "mn" | "mongolian" => Some ( "mn" . to_string ( ) ) ,
170
+ "uz" | "uzbek" => Some ( "uz" . to_string ( ) ) ,
171
+ "uk" | "ukrainian" => Some ( "uk" . to_string ( ) ) ,
172
+ "bg" | "bulgarian" => Some ( "bg" . to_string ( ) ) ,
173
+ "hr" | "croatian" => Some ( "hr" . to_string ( ) ) ,
174
+ "sr" | "serbian" => Some ( "sr" . to_string ( ) ) ,
175
+ "bs" | "bosnian" => Some ( "bs" . to_string ( ) ) ,
176
+ "mk" | "macedonian" => Some ( "mk" . to_string ( ) ) ,
177
+ "sl" | "slovenian" => Some ( "sl" . to_string ( ) ) ,
178
+ "sk" | "slovak" => Some ( "sk" . to_string ( ) ) ,
179
+ "ro" | "romanian" => Some ( "ro" . to_string ( ) ) ,
180
+ "lv" | "latvian" => Some ( "lv" . to_string ( ) ) ,
181
+ "lt" | "lithuanian" => Some ( "lt" . to_string ( ) ) ,
182
+ "et" | "estonian" => Some ( "et" . to_string ( ) ) ,
183
+ "mt" | "maltese" => Some ( "mt" . to_string ( ) ) ,
184
+ "is" | "icelandic" => Some ( "is" . to_string ( ) ) ,
185
+ "ga" | "irish" => Some ( "ga" . to_string ( ) ) ,
186
+ "cy" | "welsh" => Some ( "cy" . to_string ( ) ) ,
187
+ "eu" | "basque" => Some ( "eu" . to_string ( ) ) ,
188
+ "ca" | "catalan" => Some ( "ca" . to_string ( ) ) ,
189
+ "gl" | "galician" => Some ( "gl" . to_string ( ) ) ,
190
+ "af" | "afrikaans" => Some ( "af" . to_string ( ) ) ,
191
+ "sw" | "swahili" => Some ( "sw" . to_string ( ) ) ,
192
+ "am" | "amharic" => Some ( "am" . to_string ( ) ) ,
193
+ "or" | "oriya" => Some ( "or" . to_string ( ) ) ,
194
+ "as" | "assamese" => Some ( "as" . to_string ( ) ) ,
195
+ "sd" | "sindhi" => Some ( "sd" . to_string ( ) ) ,
196
+ "ps" | "pashto" => Some ( "ps" . to_string ( ) ) ,
197
+ "tg" | "tajik" => Some ( "tg" . to_string ( ) ) ,
198
+ "tk" | "turkmen" => Some ( "tk" . to_string ( ) ) ,
199
+ _ => Some ( lang_code. to_string ( ) ) ,
200
+ }
201
+ }
202
+
57
203
pub fn convert_response_to_results (
58
204
response : BraveSearchResponse ,
59
205
params : & SearchParams ,
0 commit comments