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 20, 2021
1 parent 7e96515 commit 9a7784e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
12 changes: 12 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReqwestError> for TealdeerError {
Expand All @@ -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),
}
}
}
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()))
}
20 changes: 4 additions & 16 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 @@ -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 9a7784e

Please sign in to comment.