1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 19:56:50 +00:00

Improve check output

This commit is contained in:
Paul-Christian Volkmer 2023-11-09 13:00:34 +01:00
parent c9ecb8e944
commit 2413e4b9b8
4 changed files with 84 additions and 49 deletions

View File

@ -55,6 +55,8 @@ pub enum CheckNotice {
description: String,
line: Option<usize>,
},
/// 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<String>) -> Vec<CheckNotice> {
pub fn check_file(file: &Path, password: Option<String>) -> Result<Vec<CheckNotice>, 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<String>) -> Vec<CheckNotice> {
}
},
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,
}],
}),
}
}

View File

@ -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<CheckNotice> {
pub fn check_file(file: &Path, password: &str) -> Result<Vec<CheckNotice>, 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<CheckNotice> {
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") {
let mut buf = String::new();
let _ = zip_file.read_to_string(&mut buf);
match osc::check(buf) {
Ok(ref mut check_result) => {
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));
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;
}
if zip_file.is_file() {
result.push(CheckNotice::Warning {
description: format!("Überspringe Eintrag '{}'", zip_file.name()),
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)
}

View File

@ -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<CheckNotice> {
use crate::checks::{CheckNotice, Checkable};
use crate::model::onkostar_editor::OnkostarEditor;
pub fn check_file(file: &Path) -> Result<Vec<CheckNotice>, 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<CheckNotice> {
pub fn check(content: String) -> Result<Vec<CheckNotice>, CheckNotice> {
let mut result = content
.lines()
.enumerate()
@ -47,14 +48,16 @@ pub fn check(content: String) -> Vec<CheckNotice> {
let inner_checks = &mut match OnkostarEditor::from_str(content.as_str()) {
Ok(data) => data.check(),
Err(err) => vec![CheckNotice::Error {
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<CheckNotice> {

View File

@ -277,13 +277,15 @@ fn main() -> Result<(), Box<dyn Error>> {
if list {
print_checks();
} else {
let notices = check_file(Path::new(file.unwrap_or_default().as_str()), password);
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,
CheckNotice::ErrorWithCode { .. }
| CheckNotice::Error { .. } => true,
_ => false,
})
.count()
@ -292,6 +294,11 @@ fn main() -> Result<(), Box<dyn Error>> {
.iter()
.for_each(|check_notice| println!("{}", check_notice));
}
Err(err) => {
println!("{}", err)
}
}
}
}
#[cfg(feature = "unzip-osb")]
SubCommand::UnzipOsb {