diff --git a/src/error.rs b/src/error.rs index fdac30f4..b82c05d8 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,6 +7,18 @@ pub enum TealdeerError { CacheError(String), ConfigError(String), UpdateError(String), + WriteError(String), +} + +impl TealdeerError { + pub(crate) fn message(&self) -> &str { + match self { + Self::CacheError(msg) + | Self::ConfigError(msg) + | Self::UpdateError(msg) + | Self::WriteError(msg) => msg, + } + } } impl From for TealdeerError { @@ -21,6 +33,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), + Self::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..c9b92dbc 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}; @@ -84,7 +84,6 @@ fn print_page( enable_markdown: bool, config: &Config, ) -> Result<(), String> { - // Open file for path in page.paths() { let file = File::open(path).map_err(|msg| format!("Could not open file: {}", msg))?; let reader = BufReader::new(file); @@ -101,7 +100,7 @@ fn print_page( // Create tokenizer and print output let mut tokenizer = Tokenizer::new(reader); print_lines(&mut handle, &mut tokenizer, &config) - .map_err(|_| "Could not write to stdout".to_string())?; + .map_err(|e| format!("Could not write to stdout: {}", e.message()))?; }; handle @@ -164,11 +163,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 +174,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 +452,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); });