Skip to content

Commit 8a866f3

Browse files
Suficiosyphar
authored andcommitted
Extract executed_query before search
1 parent 3f6a734 commit 8a866f3

File tree

2 files changed

+23
-56
lines changed

2 files changed

+23
-56
lines changed

src/registry_api.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ pub(crate) struct SearchMeta {
8686
pub(crate) struct Search {
8787
pub(crate) crates: Vec<SearchCrate>,
8888
pub(crate) meta: SearchMeta,
89-
pub(crate) executed_query: Option<String>,
9089
}
9190

9291
impl RegistryApi {
@@ -271,15 +270,6 @@ impl RegistryApi {
271270
url
272271
};
273272

274-
// Extract the query from the query args
275-
let executed_query = url.query_pairs().find_map(|(key, value)| {
276-
if key == "q" {
277-
Some(value.to_string())
278-
} else {
279-
None
280-
}
281-
});
282-
283273
let response: SearchResponse = retry_async(
284274
|| async {
285275
Ok(self
@@ -308,10 +298,6 @@ impl RegistryApi {
308298
bail!("missing metadata in crates.io response");
309299
};
310300

311-
Ok(Search {
312-
crates,
313-
meta,
314-
executed_query,
315-
})
301+
Ok(Search { crates, meta })
316302
}
317303
}

src/web/releases.rs

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ pub(crate) async fn get_releases(
132132

133133
struct SearchResult {
134134
pub results: Vec<Release>,
135-
pub executed_query: Option<String>,
136135
pub prev_page: Option<String>,
137136
pub next_page: Option<String>,
138137
}
@@ -145,11 +144,7 @@ async fn get_search_results(
145144
registry: &RegistryApi,
146145
query_params: &str,
147146
) -> Result<SearchResult, anyhow::Error> {
148-
let crate::registry_api::Search {
149-
crates,
150-
meta,
151-
executed_query,
152-
} = registry.search(query_params).await?;
147+
let crate::registry_api::Search { crates, meta } = registry.search(query_params).await?;
153148

154149
let names = Arc::new(
155150
crates
@@ -220,7 +215,6 @@ async fn get_search_results(
220215
.filter_map(|name| crates.get(name))
221216
.cloned()
222217
.collect(),
223-
executed_query,
224218
prev_page: meta.prev_page,
225219
next_page: meta.next_page,
226220
})
@@ -506,7 +500,7 @@ pub(crate) async fn search_handler(
506500
Extension(metrics): Extension<Arc<InstanceMetrics>>,
507501
Query(mut params): Query<HashMap<String, String>>,
508502
) -> AxumResult<AxumResponse> {
509-
let query = params
503+
let mut query = params
510504
.get("query")
511505
.map(|q| q.to_string())
512506
.unwrap_or_else(|| "".to_string());
@@ -568,39 +562,28 @@ pub(crate) async fn search_handler(
568562

569563
let search_result = if let Some(paginate) = params.get("paginate") {
570564
let decoded = b64.decode(paginate.as_bytes()).map_err(|e| {
571-
warn!(
572-
"error when decoding pagination base64 string \"{}\": {:?}",
573-
paginate, e
574-
);
565+
warn!("error when decoding pagination base64 string \"{paginate}\": {e:?}");
575566
AxumNope::NoResults
576567
})?;
577568
let query_params = String::from_utf8_lossy(&decoded);
578-
let query_params = match query_params.strip_prefix('?') {
579-
Some(query_params) => query_params,
580-
None => {
581-
// sometimes we see plain bytes being passed to `paginate`.
582-
// In these cases we just return `NoResults` and don't call
583-
// the crates.io API.
584-
// The whole point of the `paginate` design is that we don't
585-
// know anything about the pagination args and crates.io can
586-
// change them as they wish, so we cannot do any more checks here.
587-
warn!(
588-
"didn't get query args in `paginate` arguments for search: \"{query_params}\""
589-
);
590-
return Err(AxumNope::NoResults);
591-
}
592-
};
569+
let query_params = query_params.strip_prefix('?').ok_or_else(|| {
570+
// sometimes we see plain bytes being passed to `paginate`.
571+
// In these cases we just return `NoResults` and don't call
572+
// the crates.io API.
573+
// The whole point of the `paginate` design is that we don't
574+
// know anything about the pagination args and crates.io can
575+
// change them as they wish, so we cannot do any more checks here.
576+
warn!("didn't get query args in `paginate` arguments for search: \"{query_params}\"");
577+
AxumNope::NoResults
578+
})?;
593579

594-
let mut p = form_urlencoded::parse(query_params.as_bytes());
595-
if let Some(v) = p.find_map(|(k, v)| {
596-
if &k == "sort" {
597-
Some(v.to_string())
598-
} else {
599-
None
580+
for (k, v) in form_urlencoded::parse(query_params.as_bytes()) {
581+
match &*k {
582+
"q" => query = v.to_string(),
583+
"sort" => sort_by = v.to_string(),
584+
_ => {}
600585
}
601-
}) {
602-
sort_by = v;
603-
};
586+
}
604587

605588
get_search_results(&mut conn, &registry, query_params).await?
606589
} else if !query.is_empty() {
@@ -615,18 +598,16 @@ pub(crate) async fn search_handler(
615598
return Err(AxumNope::NoResults);
616599
};
617600

618-
let executed_query = search_result.executed_query.unwrap_or_default();
619-
620601
let title = if search_result.results.is_empty() {
621-
format!("No results found for '{executed_query}'")
602+
format!("No results found for '{query}'")
622603
} else {
623-
format!("Search results for '{executed_query}'")
604+
format!("Search results for '{query}'")
624605
};
625606

626607
Ok(Search {
627608
title,
628609
releases: search_result.results,
629-
search_query: Some(executed_query),
610+
search_query: Some(query),
630611
search_sort_by: Some(sort_by),
631612
next_page_link: search_result
632613
.next_page

0 commit comments

Comments
 (0)