Skip to content

Commit

Permalink
Add WriteError type, convert print_lines to return a result
Browse files Browse the repository at this point in the history
  • Loading branch information
sondr3 committed May 25, 2021
1 parent 22693c0 commit feb20d8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
13 changes: 13 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ pub enum TealdeerError {
CacheError(String),
ConfigError(String),
UpdateError(String),
WriteError(String),
}

impl TealdeerError {
pub fn message(&self) -> &str {
match self {
Self::CacheError(msg)
| Self::ConfigError(msg)
| Self::UpdateError(msg)
| Self::WriteError(msg) => msg,
}
}
}

impl From<ReqwestError> for TealdeerError {
Expand All @@ -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),
}
}
}
22 changes: 15 additions & 7 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<R, T>(writer: &mut T, tokenizer: &mut Tokenizer<R>, config: &Config)
pub fn print_lines<T, R>(
writer: &mut T,
tokenizer: &mut Tokenizer<R>,
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) => {
Expand All @@ -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()))
}
22 changes: 5 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -101,7 +101,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()))?;
};
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit feb20d8

Please sign in to comment.