diff options
-rw-r--r-- | src/cli.rs | 2 | ||||
-rw-r--r-- | src/config.rs | 11 | ||||
-rw-r--r-- | src/main.rs | 18 | ||||
-rw-r--r-- | src/stackexchange.rs | 10 | ||||
-rw-r--r-- | src/term.rs | 7 |
5 files changed, 28 insertions, 20 deletions
@@ -76,7 +76,7 @@ pub fn get_opts() -> Result<Opts> { update_sites: matches.is_present("update-sites"), query: matches .values_of("query") - .map(|q| q.into_iter().collect::<Vec<_>>().join(" ")), + .map(|q| q.collect::<Vec<_>>().join(" ")), config: Config { // these unwraps are safe via clap default values & validators limit: matches.value_of("limit").unwrap().parse::<u16>().unwrap(), diff --git a/src/config.rs b/src/config.rs index 88666d5..cb77086 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,5 @@ use directories::ProjectDirs; use serde::{Deserialize, Serialize}; -use serde_yaml; use std::fs; use std::fs::File; @@ -42,10 +41,12 @@ pub fn user_config() -> Result<Config> { /// Get project directory pub fn project_dir() -> Result<ProjectDirs> { - ProjectDirs::from("io", "Sam Tay", "so").ok_or(Error::os( - "Couldn't find a suitable project directory to store cache and configuration;\n\ - this application may not be supported on your operating system.", - )) + ProjectDirs::from("io", "Sam Tay", "so").ok_or_else(|| { + Error::os( + "Couldn't find a suitable project directory to store cache and configuration;\n\ + this application may not be supported on your operating system.", + ) + }) } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index e48323d..0f390df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ fn main() { if opts.list_sites { let sites = ls.sites()?; - match sites.into_iter().map(|s| s.api_site_parameter.len()).max() { + match sites.iter().map(|s| s.api_site_parameter.len()).max() { Some(max_w) => { for s in sites { println!("{:>w$}: {}", s.api_site_parameter, s.site_url, w = max_w); @@ -75,19 +75,23 @@ fn main() { let que = se.search(&q)?; let ans = que .first() - .ok_or(Error::no_results())? + .ok_or_else(Error::no_results)? .answers .first() - .ok_or(Error::from( - "StackExchange returned a question with no answers; this shouldn't be possible!", - ))?; + .ok_or_else(|| { + Error::from( + "StackExchange returned a question with no answers; \ + this shouldn't be possible!", + ) + })?; println!("{}", ans.body); } Ok(()) })() - .unwrap_or_else(|e| match e { - Error { error, .. } => printerr!(error), + .unwrap_or_else(|e| { + let Error { error, .. } = e; + printerr!(error); }) } diff --git a/src/stackexchange.rs b/src/stackexchange.rs index 9cd9767..10edaea 100644 --- a/src/stackexchange.rs +++ b/src/stackexchange.rs @@ -147,23 +147,23 @@ impl LocalStorage { // TODO make this async, inform user if we are downloading pub fn sites(&mut self) -> Result<&Vec<Site>> { // Stop once Option ~ Some or Result ~ Err - if let Some(_) = self.sites { + if self.sites.is_some() { return Ok(self.sites.as_ref().unwrap()); // safe } - if let Some(_) = self.fetch_local_sites()? { + if self.fetch_local_sites()?.is_some() { return Ok(self.sites.as_ref().unwrap()); // safe } self.fetch_remote_sites()?; self.sites .as_ref() - .ok_or(Error::from("Code failure in site listing retrieval")) + .ok_or_else(|| Error::from("Code failure in site listing retrieval")) } pub fn update_sites(&mut self) -> Result<()> { self.fetch_remote_sites() } - pub fn validate_site(&mut self, site_code: &String) -> Result<bool> { + pub fn validate_site(&mut self, site_code: &str) -> Result<bool> { let sites = self.sites()?; if sites.is_empty() { return Err(Error { @@ -177,7 +177,7 @@ impl LocalStorage { } fn fetch_local_sites(&mut self) -> Result<Option<()>> { - if let Some(file) = File::open(&self.filename).ok() { + if let Ok(file) = File::open(&self.filename) { self.sites = serde_json::from_reader(file).map_err(|_| Error::malformed(&self.filename))?; return Ok(Some(())); diff --git a/src/term.rs b/src/term.rs index 9356bf6..7507d31 100644 --- a/src/term.rs +++ b/src/term.rs @@ -1,6 +1,6 @@ use crossterm::style::{Color, Print, ResetColor, SetForegroundColor}; use crossterm::QueueableCommand; -use std::io::{Stderr, Write}; +use std::io::Write; pub trait ColoredOutput { fn queue_general(&mut self, color: Color, prefix: &str, s: &str) -> &mut Self; @@ -41,7 +41,10 @@ pub trait ColoredOutput { } } -impl ColoredOutput for Stderr { +impl<T> ColoredOutput for T +where + T: Write, +{ fn queue_general(&mut self, color: Color, prefix: &str, s: &str) -> &mut Self { (|| -> Result<(), crossterm::ErrorKind> { self.queue(SetForegroundColor(color))? |