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:
parent
c9ecb8e944
commit
2413e4b9b8
@ -55,6 +55,8 @@ pub enum CheckNotice {
|
|||||||
description: String,
|
description: String,
|
||||||
line: Option<usize>,
|
line: Option<usize>,
|
||||||
},
|
},
|
||||||
|
/// Ok
|
||||||
|
Ok(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for CheckNotice {
|
impl Display for CheckNotice {
|
||||||
@ -74,7 +76,7 @@ impl Display for CheckNotice {
|
|||||||
line,
|
line,
|
||||||
description,
|
description,
|
||||||
match example {
|
match example {
|
||||||
Some(example) => format!(" -> '{}'", style(example).dim()),
|
Some(example) => format!("\n 🔥 '{}'", style(example).dim()),
|
||||||
_ => String::new(),
|
_ => String::new(),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
@ -85,7 +87,7 @@ impl Display for CheckNotice {
|
|||||||
code,
|
code,
|
||||||
description,
|
description,
|
||||||
match example {
|
match example {
|
||||||
Some(example) => format!(" -> '{}'", style(example).dim()),
|
Some(example) => format!("\n 🔥 '{}'", style(example).dim()),
|
||||||
_ => String::new(),
|
_ => String::new(),
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
@ -125,6 +127,7 @@ impl Display for CheckNotice {
|
|||||||
),
|
),
|
||||||
None => write!(f, "{: <7} {}", style("INFO").blue().bold(), description),
|
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)]
|
#[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() {
|
match file.extension() {
|
||||||
Some(ex) => match ex.to_str() {
|
Some(ex) => match ex.to_str() {
|
||||||
#[cfg(feature = "unzip-osb")]
|
#[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),
|
Some("osc") => osc::check_file(file),
|
||||||
_ => vec![CheckNotice::Error {
|
_ => Err(CheckNotice::Error {
|
||||||
description: "Keine prüfbare Datei".to_string(),
|
description: "Keine prüfbare Datei".to_string(),
|
||||||
line: None,
|
line: None,
|
||||||
}],
|
}),
|
||||||
},
|
},
|
||||||
_ => vec![CheckNotice::Error {
|
_ => Err(CheckNotice::Error {
|
||||||
description: "Keine prüfbare Datei".to_string(),
|
description: "Keine prüfbare Datei".to_string(),
|
||||||
line: None,
|
line: None,
|
||||||
}],
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,24 +31,24 @@ use indicatif::ProgressBar;
|
|||||||
use crate::checks::{osc, CheckNotice};
|
use crate::checks::{osc, CheckNotice};
|
||||||
|
|
||||||
#[cfg(feature = "unzip-osb")]
|
#[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) {
|
let file = match fs::File::open(file) {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return vec![CheckNotice::Error {
|
return Err(CheckNotice::Error {
|
||||||
description: format!("Kann Datei nicht lesen: {}", err),
|
description: format!("Kann Datei nicht lesen: {}", err),
|
||||||
line: None,
|
line: None,
|
||||||
}];
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut archive = match zip::ZipArchive::new(file) {
|
let mut archive = match zip::ZipArchive::new(file) {
|
||||||
Ok(file) => file,
|
Ok(file) => file,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return vec![CheckNotice::Error {
|
return Err(CheckNotice::Error {
|
||||||
description: format!("Kann Datei nicht lesen: {}", err),
|
description: format!("Kann Datei nicht lesen: {}", err),
|
||||||
line: None,
|
line: None,
|
||||||
}];
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -60,27 +60,49 @@ pub fn check_file(file: &Path, password: &str) -> Vec<CheckNotice> {
|
|||||||
progress_bar.inc(1);
|
progress_bar.inc(1);
|
||||||
if let Ok(Ok(mut zip_file)) = archive.by_index_decrypt(i, password.as_bytes()) {
|
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") {
|
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 {
|
result.push(CheckNotice::Info {
|
||||||
description: format!("Prüfe Eintrag '{}'", zip_file.name()),
|
description: format!("Prüfe Eintrag '{}'", zip_file.name()),
|
||||||
line: None,
|
line: None,
|
||||||
});
|
});
|
||||||
let mut buf = String::new();
|
if check_result.is_empty() {
|
||||||
let _ = zip_file.read_to_string(&mut buf);
|
result.push(CheckNotice::Ok(format!(
|
||||||
result.append(&mut osc::check(buf));
|
"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;
|
continue;
|
||||||
}
|
}
|
||||||
|
if zip_file.is_file() {
|
||||||
result.push(CheckNotice::Warning {
|
result.push(CheckNotice::Warning {
|
||||||
description: format!("Überspringe Eintrag '{}'", zip_file.name()),
|
description: format!(
|
||||||
|
"Überspringe Eintrag '{}': Keine OSC-Datei",
|
||||||
|
zip_file.name()
|
||||||
|
),
|
||||||
line: None,
|
line: None,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return vec![CheckNotice::Error {
|
return Err(CheckNotice::Error {
|
||||||
description: format!("Kann Datei nicht lesen"),
|
description: format!("Kann Datei nicht lesen"),
|
||||||
line: None,
|
line: None,
|
||||||
}];
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
progress_bar.finish_and_clear();
|
progress_bar.finish_and_clear();
|
||||||
|
|
||||||
result
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
@ -22,23 +22,24 @@
|
|||||||
* SOFTWARE.
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::checks::{CheckNotice, Checkable};
|
|
||||||
use crate::model::onkostar_editor::OnkostarEditor;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
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) {
|
match fs::read_to_string(file) {
|
||||||
Ok(content) => check(content),
|
Ok(content) => check(content),
|
||||||
_ => vec![CheckNotice::Error {
|
_ => Err(CheckNotice::Error {
|
||||||
description: "Kann Datei nicht lesen".to_string(),
|
description: "Kann Datei nicht lesen".to_string(),
|
||||||
line: None,
|
line: None,
|
||||||
}],
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check(content: String) -> Vec<CheckNotice> {
|
pub fn check(content: String) -> Result<Vec<CheckNotice>, CheckNotice> {
|
||||||
let mut result = content
|
let mut result = content
|
||||||
.lines()
|
.lines()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
@ -47,14 +48,16 @@ pub fn check(content: String) -> Vec<CheckNotice> {
|
|||||||
|
|
||||||
let inner_checks = &mut match OnkostarEditor::from_str(content.as_str()) {
|
let inner_checks = &mut match OnkostarEditor::from_str(content.as_str()) {
|
||||||
Ok(data) => data.check(),
|
Ok(data) => data.check(),
|
||||||
Err(err) => vec![CheckNotice::Error {
|
Err(err) => {
|
||||||
|
return Err(CheckNotice::Error {
|
||||||
description: format!("Interner Fehler: {}", err),
|
description: format!("Interner Fehler: {}", err),
|
||||||
line: None,
|
line: None,
|
||||||
}],
|
})
|
||||||
|
}
|
||||||
};
|
};
|
||||||
result.append(inner_checks);
|
result.append(inner_checks);
|
||||||
|
|
||||||
result
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_line(line: usize, content: String) -> Vec<CheckNotice> {
|
fn check_line(line: usize, content: String) -> Vec<CheckNotice> {
|
||||||
|
11
src/main.rs
11
src/main.rs
@ -277,13 +277,15 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
if list {
|
if list {
|
||||||
print_checks();
|
print_checks();
|
||||||
} else {
|
} 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!(
|
println!(
|
||||||
"Es wurden {} Probleme gefunden\n",
|
"Es wurden {} Probleme gefunden\n",
|
||||||
notices
|
notices
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|notice| match notice {
|
.filter(|notice| match notice {
|
||||||
CheckNotice::ErrorWithCode { .. } | CheckNotice::Error { .. } => true,
|
CheckNotice::ErrorWithCode { .. }
|
||||||
|
| CheckNotice::Error { .. } => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
})
|
})
|
||||||
.count()
|
.count()
|
||||||
@ -292,6 +294,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
.iter()
|
.iter()
|
||||||
.for_each(|check_notice| println!("{}", check_notice));
|
.for_each(|check_notice| println!("{}", check_notice));
|
||||||
}
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("{}", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#[cfg(feature = "unzip-osb")]
|
#[cfg(feature = "unzip-osb")]
|
||||||
SubCommand::UnzipOsb {
|
SubCommand::UnzipOsb {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user