From 9a7784eb7359c93e1436d551feb1a3ceed806f17 Mon Sep 17 00:00:00 2001 From: Sondre Nilsen Date: Thu, 20 May 2021 12:25:10 +0200 Subject: [PATCH] Add WriteError type, convert print_lines to return a result --- src/error.rs | 12 ++++++++++++ src/formatter.rs | 22 +++++++++++++++------- src/main.rs | 20 ++++---------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/error.rs b/src/error.rs index fdac30f4..34704d08 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,6 +7,17 @@ pub enum TealdeerError { CacheError(String), ConfigError(String), UpdateError(String), + WriteError(String), +} + +impl TealdeerError { + pub(crate) fn message(&self) -> &str { + use TealdeerError::{CacheError, ConfigError, UpdateError, WriteError}; + + match self { + CacheError(msg) | ConfigError(msg) | UpdateError(msg) | WriteError(msg) => msg, + } + } } impl From for TealdeerError { @@ -21,6 +32,7 @@ impl fmt::Display for TealdeerError { Self::CacheError(e) => write!(f, "CacheError: {}", e), Self::ConfigError(e) => write!(f, "ConfigError: {}", e), Self::UpdateError(e) => write!(f, "UpdateError: {}", e), + TealdeerError::WriteError(e) => write!(f, "WriteError: {}", e), } } } diff --git a/src/formatter.rs b/src/formatter.rs index 34610ee3..887df6be 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -6,6 +6,7 @@ use ansi_term::{ANSIString, ANSIStrings}; use log::debug; use crate::config::Config; +use crate::error::TealdeerError::{self, WriteError}; use crate::tokenizer::Tokenizer; use crate::types::LineType; @@ -63,17 +64,21 @@ fn format_code(command: &str, text: &str, config: &Config) -> String { } /// Print a token stream to an ANSI terminal. -pub fn print_lines(writer: &mut T, tokenizer: &mut Tokenizer, config: &Config) +pub fn print_lines( + writer: &mut T, + tokenizer: &mut Tokenizer, + config: &Config, +) -> Result<(), TealdeerError> where - R: BufRead, T: Write, + R: BufRead, { let mut command = String::new(); while let Some(token) = tokenizer.next_token() { match token { LineType::Empty => { if !config.display.compact { - writeln!(writer).unwrap(); + writeln!(writer).map_err(|e| WriteError(e.to_string()))?; } } LineType::Title(title) => { @@ -85,16 +90,19 @@ where debug!("Detected command name: {}", &command); } LineType::Description(text) => { - writeln!(writer, " {}", config.style.description.paint(text)).unwrap(); + writeln!(writer, " {}", config.style.description.paint(text)) + .map_err(|e| WriteError(e.to_string()))?; } LineType::ExampleText(text) => { - writeln!(writer, " {}", config.style.example_text.paint(text)).unwrap(); + writeln!(writer, " {}", config.style.example_text.paint(text)) + .map_err(|e| WriteError(e.to_string()))?; } LineType::ExampleCode(text) => { - writeln!(writer, " {}", &format_code(&command, &text, &config)).unwrap(); + writeln!(writer, " {}", &format_code(&command, &text, &config)) + .map_err(|e| WriteError(e.to_string()))?; } LineType::Other(text) => debug!("Unknown line type: {:?}", text), } } - writeln!(writer).unwrap(); + writeln!(writer).map_err(|e| WriteError(e.to_string())) } diff --git a/src/main.rs b/src/main.rs index 781f3ef3..c9995ea3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,7 +41,7 @@ mod types; use crate::cache::{Cache, PageLookupResult}; use crate::config::{get_config_dir, get_config_path, make_default_config, Config, MAX_CACHE_AGE}; use crate::dedup::Dedup; -use crate::error::TealdeerError::{CacheError, ConfigError, UpdateError}; +use crate::error::TealdeerError::ConfigError; use crate::formatter::print_lines; use crate::tokenizer::Tokenizer; use crate::types::{ColorOptions, OsType}; @@ -164,11 +164,7 @@ fn check_cache(args: &Args, enable_styles: bool) { /// Clear the cache fn clear_cache(quietly: bool) { Cache::clear().unwrap_or_else(|e| { - match e { - CacheError(msg) | ConfigError(msg) | UpdateError(msg) => { - eprintln!("Could not delete cache: {}", msg) - } - }; + eprintln!("Could not delete cache: {}", e.message()); process::exit(1); }); if !quietly { @@ -179,11 +175,7 @@ fn clear_cache(quietly: bool) { /// Update the cache fn update_cache(cache: &Cache, quietly: bool) { cache.update().unwrap_or_else(|e| { - match e { - CacheError(msg) | ConfigError(msg) | UpdateError(msg) => { - eprintln!("Could not update cache: {}", msg) - } - }; + eprintln!("Could not update cache: {}", e.message()); process::exit(1); }); if !quietly { @@ -461,11 +453,7 @@ fn main() { // Get list of pages let pages = cache.list_pages().unwrap_or_else(|e| { - match e { - CacheError(msg) | ConfigError(msg) | UpdateError(msg) => { - eprintln!("Could not get list of pages: {}", msg) - } - } + eprintln!("Could not get list of pages: {}", e.message()); process::exit(1); });