From 2413e4b9b832422c1cae9781536235c3a40a8507 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Thu, 9 Nov 2023 13:00:34 +0100 Subject: [PATCH] Improve check output --- src/checks/mod.rs | 17 ++++++++------ src/checks/osb.rs | 56 +++++++++++++++++++++++++++++++++-------------- src/checks/osc.rs | 25 +++++++++++---------- src/main.rs | 35 +++++++++++++++++------------ 4 files changed, 84 insertions(+), 49 deletions(-) diff --git a/src/checks/mod.rs b/src/checks/mod.rs index 949f533..295b881 100644 --- a/src/checks/mod.rs +++ b/src/checks/mod.rs @@ -55,6 +55,8 @@ pub enum CheckNotice { description: String, line: Option, }, + /// Ok + Ok(String), } impl Display for CheckNotice { @@ -74,7 +76,7 @@ impl Display for CheckNotice { line, description, match example { - Some(example) => format!(" -> '{}'", style(example).dim()), + Some(example) => format!("\n 🔥 '{}'", style(example).dim()), _ => String::new(), } ), @@ -85,7 +87,7 @@ impl Display for CheckNotice { code, description, match example { - Some(example) => format!(" -> '{}'", style(example).dim()), + Some(example) => format!("\n 🔥 '{}'", style(example).dim()), _ => String::new(), } ), @@ -125,6 +127,7 @@ impl Display for CheckNotice { ), None => write!(f, "{: <7} {}", style("INFO").blue().bold(), description), }, + CheckNotice::Ok(msg) => write!(f, "{: <7} {}", style("OK").green(), msg), } } } @@ -138,7 +141,7 @@ pub trait Fixable { } #[allow(unused_variables)] -pub fn check_file(file: &Path, password: Option) -> Vec { +pub fn check_file(file: &Path, password: Option) -> Result, CheckNotice> { match file.extension() { Some(ex) => match ex.to_str() { #[cfg(feature = "unzip-osb")] @@ -150,15 +153,15 @@ pub fn check_file(file: &Path, password: Option) -> Vec { } }, Some("osc") => osc::check_file(file), - _ => vec![CheckNotice::Error { + _ => Err(CheckNotice::Error { description: "Keine prüfbare Datei".to_string(), line: None, - }], + }), }, - _ => vec![CheckNotice::Error { + _ => Err(CheckNotice::Error { description: "Keine prüfbare Datei".to_string(), line: None, - }], + }), } } diff --git a/src/checks/osb.rs b/src/checks/osb.rs index e547341..e98d2fe 100644 --- a/src/checks/osb.rs +++ b/src/checks/osb.rs @@ -31,24 +31,24 @@ use indicatif::ProgressBar; use crate::checks::{osc, CheckNotice}; #[cfg(feature = "unzip-osb")] -pub fn check_file(file: &Path, password: &str) -> Vec { +pub fn check_file(file: &Path, password: &str) -> Result, CheckNotice> { let file = match fs::File::open(file) { Ok(file) => file, Err(err) => { - return vec![CheckNotice::Error { + return Err(CheckNotice::Error { description: format!("Kann Datei nicht lesen: {}", err), line: None, - }]; + }); } }; let mut archive = match zip::ZipArchive::new(file) { Ok(file) => file, Err(err) => { - return vec![CheckNotice::Error { + return Err(CheckNotice::Error { description: format!("Kann Datei nicht lesen: {}", err), line: None, - }]; + }); } }; @@ -60,27 +60,49 @@ pub fn check_file(file: &Path, password: &str) -> Vec { progress_bar.inc(1); if let Ok(Ok(mut zip_file)) = archive.by_index_decrypt(i, password.as_bytes()) { if zip_file.is_file() && zip_file.name().ends_with(".osc") { - result.push(CheckNotice::Info { - description: format!("Prüfe Eintrag '{}'", zip_file.name()), - line: None, - }); let mut buf = String::new(); let _ = zip_file.read_to_string(&mut buf); - result.append(&mut osc::check(buf)); + match osc::check(buf) { + Ok(ref mut check_result) => { + result.push(CheckNotice::Info { + description: format!("Prüfe Eintrag '{}'", zip_file.name()), + line: None, + }); + if check_result.is_empty() { + result.push(CheckNotice::Ok(format!( + "Keine Probleme in '{}' erkannt", + zip_file.name() + ))) + } + result.append(check_result) + } + Err(_) => result.push(CheckNotice::Warning { + description: format!( + "Überspringe Eintrag '{}': Inhalt kann nicht geprüft werden", + zip_file.name(), + ), + line: None, + }), + }; continue; } - result.push(CheckNotice::Warning { - description: format!("Überspringe Eintrag '{}'", zip_file.name()), - line: None, - }) + if zip_file.is_file() { + result.push(CheckNotice::Warning { + description: format!( + "Überspringe Eintrag '{}': Keine OSC-Datei", + zip_file.name() + ), + line: None, + }) + } } else { - return vec![CheckNotice::Error { + return Err(CheckNotice::Error { description: format!("Kann Datei nicht lesen"), line: None, - }]; + }); } } progress_bar.finish_and_clear(); - result + Ok(result) } diff --git a/src/checks/osc.rs b/src/checks/osc.rs index 2140906..b9af1ec 100644 --- a/src/checks/osc.rs +++ b/src/checks/osc.rs @@ -22,23 +22,24 @@ * SOFTWARE. */ -use crate::checks::{CheckNotice, Checkable}; -use crate::model::onkostar_editor::OnkostarEditor; use std::fs; use std::path::Path; use std::str::FromStr; -pub fn check_file(file: &Path) -> Vec { +use crate::checks::{CheckNotice, Checkable}; +use crate::model::onkostar_editor::OnkostarEditor; + +pub fn check_file(file: &Path) -> Result, CheckNotice> { match fs::read_to_string(file) { Ok(content) => check(content), - _ => vec![CheckNotice::Error { + _ => Err(CheckNotice::Error { description: "Kann Datei nicht lesen".to_string(), line: None, - }], + }), } } -pub fn check(content: String) -> Vec { +pub fn check(content: String) -> Result, CheckNotice> { let mut result = content .lines() .enumerate() @@ -47,14 +48,16 @@ pub fn check(content: String) -> Vec { let inner_checks = &mut match OnkostarEditor::from_str(content.as_str()) { Ok(data) => data.check(), - Err(err) => vec![CheckNotice::Error { - description: format!("Interner Fehler: {}", err), - line: None, - }], + Err(err) => { + return Err(CheckNotice::Error { + description: format!("Interner Fehler: {}", err), + line: None, + }) + } }; result.append(inner_checks); - result + Ok(result) } fn check_line(line: usize, content: String) -> Vec { diff --git a/src/main.rs b/src/main.rs index 90de1fd..3396915 100644 --- a/src/main.rs +++ b/src/main.rs @@ -277,20 +277,27 @@ fn main() -> Result<(), Box> { if list { print_checks(); } else { - let notices = check_file(Path::new(file.unwrap_or_default().as_str()), password); - println!( - "Es wurden {} Probleme gefunden\n", - notices - .iter() - .filter(|notice| match notice { - CheckNotice::ErrorWithCode { .. } | CheckNotice::Error { .. } => true, - _ => false, - }) - .count() - ); - notices - .iter() - .for_each(|check_notice| println!("{}", check_notice)); + match check_file(Path::new(file.unwrap_or_default().as_str()), password) { + Ok(notices) => { + println!( + "Es wurden {} Probleme gefunden\n", + notices + .iter() + .filter(|notice| match notice { + CheckNotice::ErrorWithCode { .. } + | CheckNotice::Error { .. } => true, + _ => false, + }) + .count() + ); + notices + .iter() + .for_each(|check_notice| println!("{}", check_notice)); + } + Err(err) => { + println!("{}", err) + } + } } } #[cfg(feature = "unzip-osb")]