1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-07-03 01:02:55 +00:00

Add subcommands 'list' and 'modify'

This commit is contained in:
2023-06-03 15:27:09 +02:00
parent 5681b1dee3
commit ca145f5e4b
9 changed files with 290 additions and 48 deletions

View File

@ -27,7 +27,7 @@ use std::fs::OpenOptions;
use std::io::Write;
use std::ops::Add;
use clap::Parser;
use clap::{Parser, Subcommand};
use quick_xml::de::from_str;
use quick_xml::se::Serializer;
use serde::Serialize;
@ -41,59 +41,91 @@ 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")]
input: String,
#[arg(long = "profile", help = "Profildatei (Optional)")]
profile: Option<String>,
#[arg(long = "output", help = "Ausgabedatei (Optional)")]
output: Option<String>,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
#[command(about = "Zeigt alle enthaltenen Kataloge und Formulare mit Revision an.")]
List { inputfile: String },
#[command(about = "Modifiziert die angegebene Datei anhand der Profildatei")]
Modify {
inputfile: String,
#[arg(long = "profile", help = "Profildatei (Optional)")]
profile: Option<String>,
#[arg(long = "output", help = "Ausgabedatei (Optional)")]
outputfile: 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");
match cli.command {
Command::List { inputfile } => {
let contents =
fs::read_to_string(inputfile).expect("Should have been able to read the file");
if let Ok(mut data) = from_str::<OnkostarEditor>(contents.as_str()) {
data.apply_variant();
let mut buf = String::new();
let mut serializer = Serializer::new(&mut buf);
serializer.indent(' ', 2);
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(),
);
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)
if let Ok(mut data) = from_str::<OnkostarEditor>(contents.as_str()) {
data.list_forms()
} else {
eprintln!("Kann Eingabedatei nicht lesen!");
eprintln!(
"Die Datei ist entweder keine OSC-Datei, fehlerhaft oder enthält zusätzliche Inhalte."
);
}
}
Command::Modify {
inputfile,
profile,
outputfile,
} => {
let contents =
fs::read_to_string(inputfile).expect("Should have been able to read the file");
if let Ok(mut data) = from_str::<OnkostarEditor>(contents.as_str()) {
data.apply_variant();
let mut buf = String::new();
let mut serializer = Serializer::new(&mut buf);
serializer.indent(' ', 2);
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(),
);
match outputfile {
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."
);
}
}
} else {
eprintln!("Kann Eingabedatei nicht lesen!");
eprintln!(
"Die Datei ist entweder keine OSC-Datei, fehlerhaft oder enthält zusätzliche Inhalte."
);
}
}

View File

@ -55,6 +55,15 @@ pub struct DataCatalogue {
ordner: Ordner,
}
impl DataCatalogue {
pub fn to_listed_string(&self) -> String {
format!(
"Datenkatalog '{}' in Revision '{}'",
self.name, self.revision
)
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Entries {

View File

@ -160,6 +160,10 @@ impl DataForm {
})
}
}
pub fn to_listed_string(&self) -> String {
format!("Formular '{}' in Revision '{}'", self.name, self.revision)
}
}
#[derive(Serialize, Deserialize, Debug)]

View File

@ -22,6 +22,7 @@
* SOFTWARE.
*/
use console::style;
use serde::{Deserialize, Serialize};
use crate::model::data_catalogue::DataCatalogue;
@ -47,6 +48,49 @@ impl OnkostarEditor {
data_form.apply_variant();
})
}
pub fn list_forms(&self) {
println!(
"{}",
style("In der Datei sind folgende Inhalte gespeichert\n").bold()
);
println!(
"{} {}",
self.editor.property_catalogue.len(),
style("Merkmalskataloge").underlined()
);
self.editor
.property_catalogue
.iter()
.for_each(|data_form| println!("{}", data_form.to_listed_string()));
println!(
"\n{} {}",
self.editor.data_catalogue.len(),
style("Datenkataloge").underlined()
);
self.editor
.data_catalogue
.iter()
.for_each(|data_form| println!("{}", data_form.to_listed_string()));
println!(
"\n{} {}",
self.editor.data_form.len(),
style("Formulare").underlined()
);
self.editor
.data_form
.iter()
.for_each(|data_form| println!("{}", data_form.to_listed_string()));
println!(
"\n{} {}",
self.editor.unterformular.len(),
style("Unterformulare").underlined()
);
self.editor
.unterformular
.iter()
.for_each(|data_form| println!("{}", data_form.to_listed_string()));
}
}
#[derive(Serialize, Deserialize, Debug)]

View File

@ -52,6 +52,15 @@ pub struct PropertyCatalogue {
ordner: Ordner,
}
impl PropertyCatalogue {
pub fn to_listed_string(&self) -> String {
format!(
"Merkmalskatalog '{}' in Revision '{}'",
self.name, self.revision
)
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Versions {

View File

@ -22,6 +22,7 @@
* SOFTWARE.
*/
use console::style;
use serde::{Deserialize, Serialize};
use crate::model::Ordner;
@ -168,6 +169,21 @@ impl Unterformular {
})
}
}
pub fn to_listed_string(&self) -> String {
if self.hat_unterformulare {
return format!(
"Unterformular '{}' in Revision '{}' {}",
self.name,
self.revision,
style("Unterformular mit Markierung 'hat Unterformulare'!").red()
);
}
format!(
"Unterformular '{}' in Revision '{}'",
self.name, self.revision
)
}
}
#[derive(Serialize, Deserialize, Debug)]