summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Tay <sam.chong.tay@gmail.com>2020-06-18 18:34:51 -0700
committerSam Tay <sam.chong.tay@gmail.com>2020-06-18 18:34:51 -0700
commit7223a968b1334649865caf8b137d47ddca43b952 (patch)
tree916a9845edcfe9444b4680628b0c99a00f4b9362
parent2fea044264da4a35e112110164eb98f6b19295ce (diff)
Remove tokiono-tokio
This doesnt seem to fix the resizing problem...
-rw-r--r--Cargo.lock54
-rw-r--r--Cargo.toml4
-rw-r--r--src/error.rs2
-rw-r--r--src/main.rs23
-rw-r--r--src/stackexchange.rs65
5 files changed, 36 insertions, 112 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1b4ea90..61c039b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -903,36 +903,13 @@ dependencies = [
"kernel32-sys",
"libc",
"log",
- "miow 0.2.1",
+ "miow",
"net2",
"slab",
"winapi 0.2.8",
]
[[package]]
-name = "mio-named-pipes"
-version = "0.1.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3"
-dependencies = [
- "log",
- "mio",
- "miow 0.3.5",
- "winapi 0.3.8",
-]
-
-[[package]]
-name = "mio-uds"
-version = "0.6.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "afcb699eb26d4332647cc848492bbc15eafb26f08d0304550d5aa1f612e066f0"
-dependencies = [
- "iovec",
- "libc",
- "mio",
-]
-
-[[package]]
name = "miow"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -945,16 +922,6 @@ dependencies = [
]
[[package]]
-name = "miow"
-version = "0.3.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "07b88fb9795d4d36d62a012dfbf49a8f5cf12751f36d31a9dbe66d528e58979e"
-dependencies = [
- "socket2",
- "winapi 0.3.8",
-]
-
-[[package]]
name = "native-tls"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1572,7 +1539,6 @@ dependencies = [
"crossterm",
"cursive",
"directories",
- "futures",
"lazy_static",
"minimad",
"phf",
@@ -1584,7 +1550,6 @@ dependencies = [
"serde_yaml",
"termimad",
"thiserror",
- "tokio",
"unicode-width",
]
@@ -1716,28 +1681,11 @@ dependencies = [
"futures-core",
"iovec",
"lazy_static",
- "libc",
"memchr",
"mio",
- "mio-named-pipes",
- "mio-uds",
"num_cpus",
"pin-project-lite",
- "signal-hook-registry",
"slab",
- "tokio-macros",
- "winapi 0.3.8",
-]
-
-[[package]]
-name = "tokio-macros"
-version = "0.2.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index b9126e9..415e90f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -14,9 +14,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_yaml = "0.8"
-reqwest = { version = "0.10", features = ["gzip", "json"] }
-futures = "0.3"
-tokio = { version = "0.2", features = ["full"] }
+reqwest = { version = "0.10", features = ["gzip", "json", "blocking"] }
rayon = "1.3"
lazy_static = "1.4"
diff --git a/src/error.rs b/src/error.rs
index d104594..86fb55f 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -16,8 +16,6 @@ pub enum Error {
SerdeYaml(#[from] serde_yaml::Error),
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
- #[error("Futures Join error : {0}")]
- JoinError(#[from] tokio::task::JoinError),
#[error("File `{}` is malformed; try removing it", .0.display())]
MalformedFile(PathBuf),
#[error("Lacking {0:?} permissions on `{}`", .1.display())]
diff --git a/src/main.rs b/src/main.rs
index c900765..a302227 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -11,18 +11,17 @@ use error::{Error, Result};
use lazy_static::lazy_static;
use minimad::mad_inline;
use stackexchange::{LocalStorage, StackExchange};
+use std::thread;
use term::mk_print_error;
use termimad::{CompoundStyle, MadSkin};
-use tokio::task;
-#[tokio::main]
-async fn main() -> Result<(), Error> {
+fn main() -> Result<(), Error> {
let mut skin = MadSkin::default();
// TODO style configuration
skin.inline_code = CompoundStyle::with_fg(Color::Cyan);
skin.code_block.set_fgbg(Color::Cyan, termimad::gray(20));
let mut print_error = mk_print_error(&skin);
- run(&mut skin).await.or_else(|e: Error| {
+ run(&mut skin).or_else(|e: Error| {
print_error(&e.to_string())?;
match e {
Error::EmptySites => {
@@ -33,7 +32,7 @@ async fn main() -> Result<(), Error> {
})
}
-async fn run(skin: &mut MadSkin) -> Result<(), Error> {
+fn run(skin: &mut MadSkin) -> Result<(), Error> {
let opts = cli::get_opts()?;
let config = opts.config;
let sites = &config.sites;
@@ -45,11 +44,11 @@ async fn run(skin: &mut MadSkin) -> Result<(), Error> {
}
if opts.update_sites {
- ls.update_sites().await?;
+ ls.update_sites()?;
}
if opts.list_sites {
- let sites = ls.sites().await?;
+ let sites = ls.sites()?;
let mut md = String::new();
md.push_str("|:-:|:-:|\n");
md.push_str("|Site Code|Site URL|\n");
@@ -62,7 +61,7 @@ async fn run(skin: &mut MadSkin) -> Result<(), Error> {
return Ok(());
}
- if let Some(site) = ls.find_invalid_site(sites).await? {
+ if let Some(site) = ls.find_invalid_site(sites)? {
print_error!(skin, "$0 is not a valid StackExchange site.\n\n", site)?;
// TODO should only use inline for single lines; use termimad::text stuff
print_notice!(
@@ -81,17 +80,17 @@ async fn run(skin: &mut MadSkin) -> Result<(), Error> {
let se = StackExchange::new(config, q);
if lucky {
// TODO this needs preprocessing; all the more reason to do it at SE level
- let md = se.search_lucky().await?;
+ let md = se.search_lucky()?;
skin.print_text(&md);
skin.print_text("\nPress **[SPACE]** to see more results, or any other key to exit");
// Kick off the rest of the search in the background
- let qs = task::spawn(async move { se.search().await });
+ let handler = thread::spawn(move || se.search());
if !utils::wait_for_char(' ')? {
return Ok(());
}
- tui::run(qs.await.unwrap()?)?;
+ tui::run(handler.join().unwrap()?)?;
} else {
- tui::run(se.search().await?)?;
+ tui::run(se.search()?)?;
}
}
Ok(())
diff --git a/src/stackexchange.rs b/src/stackexchange.rs
index 1d4789a..166550f 100644
--- a/src/stackexchange.rs
+++ b/src/stackexchange.rs
@@ -1,6 +1,5 @@
-use futures::stream::StreamExt;
use rayon::prelude::*;
-use reqwest::Client;
+use reqwest::blocking::Client;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
@@ -25,9 +24,6 @@ const SE_FILTER: &str = ".DND5X2VHHUH8HyJzpjo)5NvdHI3w6auG";
/// Pagesize when fetching all SE sites. Should be good for many years...
const SE_SITES_PAGESIZE: u16 = 10000;
-/// Limit on concurrent requests (gets passed to `buffer_unordered`)
-const CONCURRENT_REQUESTS_LIMIT: usize = 8;
-
/// This structure allows interacting with parts of the StackExchange
/// API, using the `Config` struct to determine certain API settings and options.
// TODO should my se structs have &str instead of String?
@@ -98,10 +94,9 @@ impl StackExchange {
/// For now, use only the first configured site, since, parodoxically, sites
/// with the worst results will finish executing first, since there's less
/// data to retrieve.
- pub async fn search_lucky(&self) -> Result<String> {
+ pub fn search_lucky(&self) -> Result<String> {
Ok(self
- .search_advanced_site(self.config.sites.iter().next().unwrap(), 1)
- .await?
+ .search_advanced_site(self.config.sites.iter().next().unwrap(), 1)?
.into_iter()
.next()
.ok_or(Error::NoResults)?
@@ -113,26 +108,17 @@ impl StackExchange {
}
/// Search query at stack exchange and get a list of relevant questions
- pub async fn search(&self) -> Result<Vec<Question<Markdown>>> {
- self.search_advanced(self.config.limit).await
+ pub fn search(&self) -> Result<Vec<Question<Markdown>>> {
+ self.search_advanced(self.config.limit)
}
/// Parallel searches against the search/advanced endpoint across all configured sites
- async fn search_advanced(&self, limit: u16) -> Result<Vec<Question<Markdown>>> {
- futures::stream::iter(self.config.sites.clone())
- .map(|site| {
- let clone = self.clone();
- tokio::spawn(async move {
- let clone = &clone;
- clone.search_advanced_site(&site, limit).await
- })
- })
- .buffer_unordered(CONCURRENT_REQUESTS_LIMIT)
- .collect::<Vec<_>>()
- .await
- .into_iter()
- .map(|r| r.map_err(Error::from).and_then(|x| x))
- .collect::<Result<Vec<Vec<_>>>>()
+ fn search_advanced(&self, limit: u16) -> Result<Vec<Question<Markdown>>> {
+ self.config
+ .sites
+ .iter()
+ .map(|site| self.search_advanced_site(&site, limit))
+ .collect::<Result<Vec<_>>>()
.map(|v| {
let mut qs: Vec<Question<String>> = v.into_iter().flatten().collect();
if self.config.sites.len() > 1 {
@@ -144,7 +130,7 @@ impl StackExchange {
/// Search against the site's search/advanced endpoint with a given query.
/// Only fetches questions that have at least one answer.
- async fn search_advanced_site(&self, site: &str, limit: u16) -> Result<Vec<Question<String>>> {
+ fn search_advanced_site(&self, site: &str, limit: u16) -> Result<Vec<Question<String>>> {
let qs = self
.client
.get(stackexchange_url("search/advanced"))
@@ -159,10 +145,8 @@ impl StackExchange {
("order", "desc"),
("sort", "relevance"),
])
- .send()
- .await?
- .json::<ResponseWrapper<Question<String>>>()
- .await?
+ .send()?
+ .json::<ResponseWrapper<Question<String>>>()?
.items;
Ok(Self::preprocess(qs))
}
@@ -261,9 +245,9 @@ impl LocalStorage {
}
// TODO inform user if we are downloading
- pub async fn sites(&mut self) -> Result<&Vec<Site>> {
+ pub fn sites(&mut self) -> Result<&Vec<Site>> {
if self.sites.is_none() && !self.fetch_local_sites()? {
- self.fetch_remote_sites().await?;
+ self.fetch_remote_sites()?;
}
match &self.sites {
Some(sites) if sites.is_empty() => Err(Error::EmptySites),
@@ -272,18 +256,17 @@ impl LocalStorage {
}
}
- pub async fn update_sites(&mut self) -> Result<()> {
- self.fetch_remote_sites().await
+ pub fn update_sites(&mut self) -> Result<()> {
+ self.fetch_remote_sites()
}
// TODO is this HM worth it? Probably only will ever have < 10 site codes to search...
- pub async fn find_invalid_site<'a, 'b>(
+ pub fn find_invalid_site<'a, 'b>(
&'b mut self,
site_codes: &'a [String],
) -> Result<Option<&'a String>> {
let hm: HashMap<&str, ()> = self
- .sites()
- .await?
+ .sites()?
.iter()
.map(|site| (site.api_site_parameter.as_str(), ()))
.collect();
@@ -302,7 +285,7 @@ impl LocalStorage {
}
// TODO decide whether or not I should give LocalStorage an api key..
- async fn fetch_remote_sites(&mut self) -> Result<()> {
+ fn fetch_remote_sites(&mut self) -> Result<()> {
self.sites = Some(
Client::new()
.get(stackexchange_url("sites"))
@@ -311,10 +294,8 @@ impl LocalStorage {
("pagesize", SE_SITES_PAGESIZE.to_string()),
("page", "1".to_string()),
])
- .send()
- .await?
- .json::<ResponseWrapper<Site>>()
- .await?
+ .send()?
+ .json::<ResponseWrapper<Site>>()?
.items,
);
self.store_local_sites()