From 7233477c7612918a6a8a6df5bcfc452b25e001be Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 27 Mar 2024 10:47:49 +0000 Subject: [PATCH] handle ProgressStyle's template now returning a Result --- src/epub.rs | 6 +++++- src/errors.rs | 8 ++++++++ src/html.rs | 6 +++++- src/main.rs | 15 ++++++++++++--- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/epub.rs b/src/epub.rs index 14239a3..20713a8 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -30,7 +30,11 @@ pub fn generate_epubs( let enabled_bar = ProgressBar::new(articles.len() as u64); let style = ProgressStyle::default_bar().template( "{spinner:.cyan} [{elapsed_precise}] {bar:40.white} {:>8} epub {pos}/{len:7} {msg:.green}", - ); + ).map_err(|e| { + let mut paperoni_err: PaperoniError = e.into(); + paperoni_err.set_article_source("progress bar"); + vec![paperoni_err] + })?; enabled_bar.set_style(style); if !articles.is_empty() { enabled_bar.set_message("Generating epubs"); diff --git a/src/errors.rs b/src/errors.rs index 7df872a..02be430 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -15,6 +15,8 @@ pub enum ErrorKind { UTF8Error(String), #[error("[ReadabilityError]: {0}")] ReadabilityError(String), + #[error("[TemplateError]: {0}")] + TemplateError(String), } #[derive(Error, Debug)] @@ -128,6 +130,12 @@ impl From for PaperoniError { } } +impl From for PaperoniError { + fn from(err: indicatif::style::TemplateError) -> Self { + PaperoniError::with_kind(ErrorKind::TemplateError(err.to_string())) + } +} + #[derive(Debug, Error)] pub enum LogError { #[error(transparent)] diff --git a/src/html.rs b/src/html.rs index 07ad767..44eecd9 100644 --- a/src/html.rs +++ b/src/html.rs @@ -44,7 +44,11 @@ pub fn generate_html_exports( let enabled_bar = ProgressBar::new(articles.len() as u64); let style = ProgressStyle::default_bar().template( "{spinner:.cyan} [{elapsed_precise}] {bar:40.white} {:>8} html {pos}/{len:7} {msg:.green}", - ); + ).map_err(|e| { + let mut paperoni_err: PaperoniError = e.into(); + paperoni_err.set_article_source("progress bar"); + vec![paperoni_err] + })?; enabled_bar.set_style(style); if !articles.is_empty() { enabled_bar.set_message("Generating html files"); diff --git a/src/main.rs b/src/main.rs index fa49ab0..56a8809 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ extern crate lazy_static; use std::process::exit; +use std::time::Duration; use colored::Colorize; use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_ONLY}; @@ -25,6 +26,8 @@ use epub::generate_epubs; use html::generate_html_exports; use logs::display_summary; +use crate::errors::PaperoniError; + fn main() { let app_config = match cli::AppConfig::init_with_cli() { Ok(app_config) => app_config, @@ -58,9 +61,15 @@ fn run(app_config: AppConfig) { let enabled_bar = ProgressBar::new(app_config.urls.len() as u64); let style = ProgressStyle::default_bar().template( "{spinner:.cyan} [{elapsed_precise}] {bar:40.white} {:>8} link {pos}/{len:7} {msg:.yellow/white}", - ); - enabled_bar.set_style(style); - enabled_bar.enable_steady_tick(500); + ).map_err(|e| { + let mut paperoni_err: PaperoniError = e.into(); + paperoni_err.set_article_source("progress bar"); + vec![paperoni_err] + }); + if let Ok(style) = style { + enabled_bar.set_style(style); + } + enabled_bar.enable_steady_tick(Duration::from_millis(500)); enabled_bar };