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

Merge pull request #18 from CCC-MF/issue_15

Checks and fixes for known issues in OSC files
This commit is contained in:
Paul-Christian Volkmer 2023-11-06 14:02:32 +01:00 committed by GitHub
commit 352f5e23fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 317 additions and 2 deletions

160
src/checks/mod.rs Normal file
View File

@ -0,0 +1,160 @@
/*
* MIT License
*
* Copyright (c) 2023 Comprehensive Cancer Center Mainfranken
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
pub mod osc;
use console::style;
use std::fmt::{Display, Formatter};
pub enum CheckNotice {
/// This will result in Error if importing file and has a support code
ErrorWithCode {
code: String,
description: String,
line: Option<usize>,
example: Option<String>,
},
/// This will result in Error if importing file
Error {
description: String,
line: Option<usize>,
},
#[allow(dead_code)]
/// Other known issues
Warning {
description: String,
line: Option<usize>,
},
}
impl Display for CheckNotice {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
CheckNotice::ErrorWithCode {
code,
description,
line,
example,
} => match line {
Some(line) => write!(
f,
"{} ({}) at Line {}: {}{}",
style("ERROR").red().bold(),
code,
line,
description,
match example {
Some(example) => format!(" -> '{}'", style(example).dim()),
_ => String::new(),
}
),
None => write!(
f,
"{} ({}): {}{}",
style("ERROR").red().bold(),
code,
description,
match example {
Some(example) => format!(" -> '{}'", style(example).dim()),
_ => String::new(),
}
),
},
CheckNotice::Error { description, line } => match line {
Some(line) => write!(
f,
"{} at Line {}: {}",
style("ERROR").red().bold(),
line,
description
),
None => write!(f, "{}: {}", style("ERROR").red().bold(), description),
},
CheckNotice::Warning { description, line } => match line {
Some(line) => write!(
f,
"{} at Line {}: {}",
style("WARNING").yellow().bold(),
line,
description
),
None => write!(f, "{}: {}", style("WARNING").yellow().bold(), description),
},
}
}
}
pub trait Checkable {
fn check(&self) -> Vec<CheckNotice>;
}
pub trait Fixable {
fn fix(&mut self) -> bool;
}
pub fn print_checks() {
println!(
"{}",
style("Die folgenden Probleme sind bekannt\n")
.yellow()
.bold()
);
struct Problem<'a> {
code: &'a str,
name: &'a str,
description: &'a str,
fixable: bool,
}
impl<'a> Display for Problem<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {} {}\n\n{}",
style(self.code).bold(),
style(self.name).underlined(),
match self.fixable {
true => style("(Behebbar)").green(),
false => style("(Nicht behebbar)").red(),
},
self.description
)
}
}
vec![Problem {
code: "2023-0001",
name: "Leerzeichen am Ende der Plausibilitätsregel-Bezeichnung (OSTARSUPP-13334)",
description: "Treten Leerzeichen am Ende der Plausibilitätsregel-Bezeichnung auf,\n\
führt dies zu Fehlern beim Import der OSC-Datei.\n\
\n\
Das Problem wird beim Verwenden des Unterbefehls 'modify' automatisch\n\
behoben und Leerzeichen entfernt.
",
fixable: true,
}]
.iter()
.for_each(|problem| println!("{}\n", problem))
}

87
src/checks/osc.rs Normal file
View File

@ -0,0 +1,87 @@
/*
* MIT License
*
* Copyright (c) 2023 Comprehensive Cancer Center Mainfranken
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
use crate::checks::{CheckNotice, Checkable};
use crate::model::onkostar_editor::OnkostarEditor;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::str::FromStr;
pub fn check(file: &Path) -> Vec<CheckNotice> {
let mut result = match File::open(file) {
Ok(file) => BufReader::new(file)
.lines()
.enumerate()
.flat_map(|(line, content)| match content {
Ok(content) => check_line(line, content),
_ => {
return vec![CheckNotice::Error {
description: "Cannot read line".to_string(),
line: Some(line),
}]
}
})
.collect::<Vec<_>>(),
_ => {
return vec![CheckNotice::Error {
description: "Kann Datei nicht lesen".to_string(),
line: None,
}]
}
};
let inner_checks = &mut match fs::read_to_string(file) {
Ok(content) => match OnkostarEditor::from_str(content.as_str()) {
Ok(data) => data.check(),
Err(err) => vec![CheckNotice::Error {
description: format!("Interner Fehler: {}", err),
line: None,
}],
},
_ => vec![CheckNotice::Error {
description: "Kann Datei nicht lesen".to_string(),
line: None,
}],
};
result.append(inner_checks);
result
}
fn check_line(line: usize, content: String) -> Vec<CheckNotice> {
let mut result = vec![];
if content.contains(" </Bezeichnung>") {
result.append(&mut vec![CheckNotice::ErrorWithCode {
code: "OSTARSUPP-13334".to_string(),
description: "Leerzeichen am Ende der Plausibilitätsregel-Bezeichnung".to_string(),
line: Some(line),
example: Some(content.trim().to_string()),
}])
}
result
}

View File

@ -93,6 +93,11 @@ pub enum SubCommand {
help = "Starte interaktiven Dialog zum Modifizieren von OSC-Dateien" help = "Starte interaktiven Dialog zum Modifizieren von OSC-Dateien"
)] )]
interactive: bool, interactive: bool,
#[arg(
long = "fix",
help = "Erweiterte Problembehandlung und Reparatur der OSC-Datei"
)]
fix: bool,
}, },
#[command(about = "Vergleiche zwei Dateien anhand der Revision der enthaltenen Inhalte")] #[command(about = "Vergleiche zwei Dateien anhand der Revision der enthaltenen Inhalte")]
Diff { Diff {
@ -101,6 +106,15 @@ pub enum SubCommand {
#[arg(long = "strict", help = "Strikter Vergleich des Inhalts")] #[arg(long = "strict", help = "Strikter Vergleich des Inhalts")]
strict: bool, strict: bool,
}, },
#[command(about = "Überprüfe OSC-Datei auf bekannte Problemen")]
Check {
file: String,
#[arg(
long = "list",
help = "Prüfe nicht und zeige Liste mit Checks auf bekannte Problemen"
)]
list: bool,
},
#[cfg(feature = "unzip-osb")] #[cfg(feature = "unzip-osb")]
#[command(about = "Entpackt eine OSB-Datei")] #[command(about = "Entpackt eine OSB-Datei")]
UnzipOsb { UnzipOsb {

View File

@ -28,9 +28,11 @@ use std::fs;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::Write; use std::io::Write;
use std::ops::Add; use std::ops::Add;
use std::path::PathBuf; use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use crate::checks::osc::check;
use crate::checks::print_checks;
use clap::Parser; use clap::Parser;
use console::style; use console::style;
use dialoguer::Confirm; use dialoguer::Confirm;
@ -42,6 +44,7 @@ use crate::cli::{Cli, SubCommand};
use crate::model::onkostar_editor::OnkostarEditor; use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile; use crate::profile::Profile;
mod checks;
mod cli; mod cli;
mod model; mod model;
mod profile; mod profile;
@ -153,6 +156,7 @@ fn main() -> Result<(), Box<dyn Error>> {
sorted, sorted,
strip, strip,
interactive, interactive,
fix,
} => { } => {
let data = &mut read_inputfile(inputfile)?; let data = &mut read_inputfile(inputfile)?;
@ -189,6 +193,10 @@ fn main() -> Result<(), Box<dyn Error>> {
.unwrap(); .unwrap();
} }
if fix {
// No operation as of now
}
if sorted { if sorted {
data.sorted(); data.sorted();
} }
@ -257,6 +265,15 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
}; };
} }
SubCommand::Check { file, list } => {
if list {
print_checks();
} else {
check(Path::new(file.as_str()))
.iter()
.for_each(|check_notice| println!("{}", check_notice));
}
}
#[cfg(feature = "unzip-osb")] #[cfg(feature = "unzip-osb")]
SubCommand::UnzipOsb { SubCommand::UnzipOsb {
file, file,

View File

@ -28,6 +28,7 @@ use std::collections::HashSet;
use console::style; use console::style;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::checks::{CheckNotice, Checkable};
use crate::model::onkostar_editor::OnkostarEditor; use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::requirements::{Requirement, Requires}; use crate::model::requirements::{Requirement, Requires};
use crate::model::{ use crate::model::{
@ -380,6 +381,12 @@ impl FolderContent for DataForm {
} }
} }
impl Checkable for DataForm {
fn check(&self) -> Vec<CheckNotice> {
vec![]
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct DataCatalogues { pub struct DataCatalogues {

View File

@ -27,9 +27,9 @@ use std::collections::hash_map::DefaultHasher;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use crate::model::requirements::Requires;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::model::requirements::Requires;
use crate::profile::{FormField, FormReference, Profile}; use crate::profile::{FormField, FormReference, Profile};
pub mod data_catalogue; pub mod data_catalogue;

View File

@ -30,6 +30,7 @@ use console::style;
use quick_xml::de::from_str; use quick_xml::de::from_str;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::checks::{CheckNotice, Checkable};
use crate::model::data_catalogue::DataCatalogue; use crate::model::data_catalogue::DataCatalogue;
use crate::model::data_form::DataForm; use crate::model::data_form::DataForm;
use crate::model::property_catalogue::PropertyCatalogue; use crate::model::property_catalogue::PropertyCatalogue;
@ -409,6 +410,28 @@ impl FromStr for OnkostarEditor {
} }
} }
impl Checkable for OnkostarEditor {
fn check(&self) -> Vec<CheckNotice> {
let mut result = self
.editor
.data_form
.iter()
.flat_map(|entity| entity.check())
.collect::<Vec<_>>();
let other = &mut self
.editor
.unterformular
.iter()
.flat_map(|entity| entity.check())
.collect::<Vec<_>>();
result.append(other);
result
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct InfoXML { pub struct InfoXML {

View File

@ -25,6 +25,7 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashSet; use std::collections::HashSet;
use crate::checks::{CheckNotice, Checkable};
use console::style; use console::style;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -377,6 +378,12 @@ impl FolderContent for Unterformular {
} }
} }
impl Checkable for Unterformular {
fn check(&self) -> Vec<CheckNotice> {
vec![]
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct DataCatalogues { pub struct DataCatalogues {