summaryrefslogtreecommitdiffstats
path: root/src/stackexchange.rs
diff options
context:
space:
mode:
authorSam Tay <sam.chong.tay@gmail.com>2020-06-16 20:55:10 -0700
committerSam Tay <sam.chong.tay@gmail.com>2020-06-16 20:55:10 -0700
commitd422c8424ae76fc85e0fdf55257e0cee7fa38271 (patch)
tree0ebfd038e294b0020ca41aeb9a43c1f6879615ba /src/stackexchange.rs
parent76be36e46a5d404cbfb93253bfba4b73f03ee811 (diff)
Add --lucky flag
Punting on async for now because loading up the cursive app is already super fast. Might be noticeably necessary after multi-site and external search engines are added
Diffstat (limited to 'src/stackexchange.rs')
-rw-r--r--src/stackexchange.rs28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/stackexchange.rs b/src/stackexchange.rs
index 941aad6..e9bc6d9 100644
--- a/src/stackexchange.rs
+++ b/src/stackexchange.rs
@@ -81,11 +81,32 @@ impl StackExchange {
StackExchange { client, config }
}
+ // TODO also return a future with the rest of the questions
+ /// Search query at stack exchange and get the top answer body
+ pub fn search_lucky(&self, q: &str) -> Result<String> {
+ let ans = self
+ .search_advanced(q, 1)?
+ .into_iter()
+ .next()
+ .ok_or(Error::NoResults)?
+ .answers
+ .into_iter()
+ .next()
+ .ok_or_else(|| {
+ Error::StackExchange(String::from("Received question with no answers"))
+ })?;
+ Ok(ans.body)
+ }
+
+ /// Search query at stack exchange and get a list of relevant questions
+ pub fn search(&self, q: &str) -> Result<Vec<Question>> {
+ self.search_advanced(q, self.config.limit)
+ }
/// Search against the search/advanced endpoint with a given query.
/// Only fetches questions that have at least one answer.
/// TODO async
/// TODO parallel requests over multiple sites
- pub fn search(&self, q: &str) -> Result<Vec<Question>> {
+ fn search_advanced(&self, q: &str, limit: u16) -> Result<Vec<Question>> {
let resp_body = self
.client
.get(stackexchange_url("search/advanced"))
@@ -93,7 +114,7 @@ impl StackExchange {
.query(&self.get_default_opts())
.query(&[
("q", q),
- ("pagesize", &self.config.limit.to_string()),
+ ("pagesize", &limit.to_string()),
("page", "1"),
("answers", "1"),
("order", "desc"),
@@ -141,7 +162,7 @@ impl LocalStorage {
})
}
- // TODO make this async, inform user if we are downloading
+ // TODO inform user if we are downloading
pub fn sites(&mut self) -> Result<&Vec<Site>> {
// Stop once Option ~ Some or Result ~ Err
if self.sites.is_none() && !self.fetch_local_sites()? {
@@ -177,7 +198,6 @@ impl LocalStorage {
}
// TODO decide whether or not I should give LocalStorage an api key..
- // TODO cool loading animation?
fn fetch_remote_sites(&mut self) -> Result<()> {
let resp_body = Client::new()
.get(stackexchange_url("sites"))