From eac28da798569989175a44de1947dfdeff0f454a Mon Sep 17 00:00:00 2001 From: Kenneth Gitere Date: Sat, 24 Jul 2021 12:35:30 +0300 Subject: [PATCH] fix: add validation when passing `--inline-toc` feat: add coloring when displaying CLI errors --- src/cli.rs | 12 +++++++++++- src/errors.rs | 2 ++ src/main.rs | 3 ++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 67f8ace..12a6357 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -119,7 +119,17 @@ impl<'a> TryFrom> for AppConfig { 4..=u64::MAX => LogLevel::Debug, }) .is_logging_to_file(arg_matches.is_present("log-to-file")) - .inline_toc(arg_matches.is_present("inline-toc")) + .inline_toc( + (if arg_matches.is_present("inline-toc") { + if arg_matches.value_of("export") == Some("epub") { + Ok(true) + } else { + Err(Error::WrongExportInliningToC) + } + } else { + Ok(false) + })?, + ) .output_directory( arg_matches .value_of("output_directory") diff --git a/src/errors.rs b/src/errors.rs index 17ae34a..8fbfeea 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -156,4 +156,6 @@ pub enum CliError { OutputDirectoryNotExists, #[error("Unable to start logger!\n{0}")] LogError(#[from] LogError), + #[error("The --inline-toc can only be used exporting to epub")] + WrongExportInliningToC, } diff --git a/src/main.rs b/src/main.rs index 925beca..d82ac95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ extern crate lazy_static; use std::process::exit; +use colored::Colorize; use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY}; use comfy_table::{ContentArrangement, Table}; use http::download; @@ -28,7 +29,7 @@ fn main() { let app_config = match cli::AppConfig::init_with_cli() { Ok(app_config) => app_config, Err(err) => { - eprintln!("{}", err); + eprintln!("{}: {}", "ERROR".bold().bright_red(), err); exit(1); } };