mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-24 00:54:17 +02:00
Add WriteError type, convert print_lines to return a result
This commit is contained in:
parent
22693c09ba
commit
feb20d8c5c
3 changed files with 33 additions and 24 deletions
13
src/error.rs
13
src/error.rs
|
|
@ -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 {
|
||||
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<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) => {
|
||||
|
|
@ -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
src/main.rs
22
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};
|
||||
|
|
@ -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()))?;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -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);
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue