Check input file for additional entries

This will prevent the application from creating output without these
additional entities, which would be missing in output.

Instead, the application will exit with an error message.
This commit is contained in:
2023-06-02 23:05:35 +02:00
parent 31a1dcb63e
commit 1224f93764
7 changed files with 80 additions and 40 deletions

View File

@@ -41,59 +41,59 @@ mod profile;
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true, arg_required_else_help(true))]
struct Cli {
#[arg(
long = "input",
help = "Eingabedatei"
)]
#[arg(long = "input", help = "Eingabedatei")]
input: String,
#[arg(
long = "profile",
help = "Profildatei (Optional)"
)]
#[arg(long = "profile", help = "Profildatei (Optional)")]
profile: Option<String>,
#[arg(
long = "output",
help = "Ausgabedatei (Optional)"
)]
output: Option<String>
#[arg(long = "output", help = "Ausgabedatei (Optional)")]
output: Option<String>,
}
fn main() {
let cli = Cli::parse();
let contents = fs::read_to_string(cli.input)
.expect("Should have been able to read the file");
let contents = fs::read_to_string(cli.input).expect("Should have been able to read the file");
let mut data: OnkostarEditor = from_str(contents.as_str()).unwrap();
if let Ok(mut data) = from_str::<OnkostarEditor>(contents.as_str()) {
data.apply_variant();
data.apply_variant();
let mut buf = String::new();
let mut buf = String::new();
let mut serializer = Serializer::new(&mut buf);
serializer.indent(' ', 2);
let mut serializer = Serializer::new(&mut buf);
serializer.indent(' ', 2);
data.serialize(serializer).expect("Generated XML");
data.serialize(serializer).expect("Generated XML");
let output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
.to_string()
.add(
buf
// Replace &apos; and &quot; as used in original file
.replace("&apos;", "'")
.replace("&quot;", "\"")
.as_str(),
);
let output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".to_string()
.add(buf
// Replace &apos; and &quot; as used in original file
.replace("&apos;", "'")
.replace("&quot;", "\"")
.as_str());
match cli.output {
Some(filename) => {
let mut file = OpenOptions::new()
.read(false)
.write(true)
.create(true)
.truncate(true)
.open(filename).unwrap();
file.write_all(output.as_bytes()).expect("Should have written output file");
},
None => {
println!("{}", output)
match cli.output {
Some(filename) => {
let mut file = OpenOptions::new()
.read(false)
.write(true)
.create(true)
.truncate(true)
.open(filename)
.unwrap();
file.write_all(output.as_bytes())
.expect("Should have written output file");
}
None => {
println!("{}", output)
}
}
} else {
eprintln!("Kann Eingabedatei nicht lesen!");
eprintln!(
"Die Datei ist entweder keine OSC-Datei, fehlerhaft oder enthält zusätzliche Inhalte."
);
}
}