Skip to content

Commit 7e57e57

Browse files
committed
fix small codesmells in websearch
1 parent 69adbc9 commit 7e57e57

2 files changed

Lines changed: 26 additions & 25 deletions

File tree

src/websearch/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Web {
1919
cached_results: HashMap<String, Vec<SearchResult>>,
2020

2121
// The memory cost of this isnt actually that bad. Each image is just a couple kB each since
22-
// they are very small thumbnails. It only increased a few mB over like 10s of useage
22+
// they are very small thumbnails. It only increased a few mB over like 10s of usage
2323
image_hashmap: HashMap<String, widget::image::Handle>,
2424
client: reqwest::Client,
2525
}
@@ -60,14 +60,18 @@ impl Web {
6060
match (first, search_text) {
6161
// get first char
6262
(Some('w'), search_text) => {
63-
log::info!("wikipedia time!");
63+
log::debug!("wikipedia time!");
6464
let client = self.client.clone();
6565

6666
let full_text = self.input_for_results.clone();
6767
// trim first character. TODO. Dont hardcode
6868
let trimmed_text = search_text[1..].to_owned();
6969
Task::perform(
70-
async move { wikipedia::search(&client, trimmed_text.as_str(), full_text).await },
70+
async move {
71+
let res = wikipedia::search(&client, trimmed_text.as_str()).await;
72+
// this little tuple maneuver is cool
73+
(full_text, res)
74+
},
7175
|r| ModuleMessage::WebMessage(WebMsg::GotResult(r.0, r.1)),
7276
)
7377
}

src/websearch/wikipedia.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,28 @@ impl From<WikiResultSingle> for SearchResult {
3838
pub async fn search(
3939
client: &reqwest::Client,
4040
search_text: &str,
41-
full_text: String,
42-
) -> (String, Result<Vec<SearchResult>, SearchError>) {
41+
) -> Result<Vec<SearchResult>, SearchError> {
4342
let url =
4443
format!("https://en.wikipedia.org/w/rest.php/v1/search/title?q={search_text}&limit=5");
45-
let req_result = async |url| -> Result<Vec<SearchResult>, SearchError> {
46-
{
47-
let response =
48-
client.get(url).send().await.map_err(|e| {
49-
SearchError::BadResponse(format!("failed to get response: {}", e))
50-
})?;
5144

52-
let text = response
53-
.text()
54-
.await
55-
.map_err(|e| SearchError::BadResponse(format!("failed to get text: {}", e)))?;
56-
log::trace!("text found from wikipedia: {}", text);
57-
let data: WikiResults = serde_json::from_str(&text).map_err(|e| {
58-
SearchError::BadResponse(format!("failed to parse from json: {}", e))
59-
})?;
45+
let response = client
46+
.get(url)
47+
.send()
48+
.await
49+
.map_err(|e| SearchError::BadResponse(format!("failed to get response: {}", e)))?;
6050

61-
let parsed = data.pages.into_iter().map(|result| result.into()).collect();
62-
log::debug!("parsed text from wikipedia: {:#?}", parsed);
63-
Ok(parsed)
64-
}
65-
};
51+
let text = response
52+
.text()
53+
.await
54+
.map_err(|e| SearchError::BadResponse(format!("failed to get text: {}", e)))?;
55+
56+
log::trace!("text found from wikipedia: {}", text);
57+
58+
let data: WikiResults = serde_json::from_str(&text)
59+
.map_err(|e| SearchError::BadResponse(format!("failed to parse from json: {}", e)))?;
60+
61+
let parsed = data.pages.into_iter().map(|result| result.into()).collect();
62+
log::debug!("parsed text from wikipedia: {:#?}", parsed);
6663

67-
(full_text, req_result(url).await)
64+
Ok(parsed)
6865
}

0 commit comments

Comments
 (0)