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

Use traits for catalogues, forms and form entries

This commit is contained in:
Paul-Christian Volkmer 2023-06-15 22:54:25 +02:00
parent 2c11690edf
commit 13f1126619
7 changed files with 125 additions and 60 deletions

View File

@ -111,7 +111,7 @@ fn main() -> Result<(), Box<dyn Error>> {
match cli.command { match cli.command {
Command::List { inputfile } => { Command::List { inputfile } => {
let data = read_inputfile(inputfile)?; let data = read_inputfile(inputfile)?;
data.list_forms(); data.print_list();
} }
Command::Modify { Command::Modify {
inputfile, inputfile,

View File

@ -24,7 +24,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::model::Ordner; use crate::model::{Listable, Ordner};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
@ -55,8 +55,8 @@ pub struct DataCatalogue {
ordner: Ordner, ordner: Ordner,
} }
impl DataCatalogue { impl Listable for DataCatalogue {
pub fn to_listed_string(&self) -> String { fn to_listed_string(&self) -> String {
format!( format!(
"Datenkatalog '{}' in Revision '{}'", "Datenkatalog '{}' in Revision '{}'",
self.name, self.revision self.name, self.revision

View File

@ -24,7 +24,10 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::model::{Ansichten, Entries, Filter, MenuCategory, PlausibilityRules, Script}; use crate::model::{
apply_profile_to_form_entry, Ansichten, Entries, Filter, FormEntry, FormEntryContainer,
Listable, MenuCategory, PlausibilityRules, Script,
};
use crate::model::{Haeufigkeiten, Ordner}; use crate::model::{Haeufigkeiten, Ordner};
use crate::profile::Profile; use crate::profile::Profile;
@ -149,8 +152,8 @@ pub struct DataForm {
ansichten: Option<Ansichten>, ansichten: Option<Ansichten>,
} }
impl DataForm { impl FormEntryContainer for DataForm {
pub fn apply_profile(&mut self, profile: &Profile) { fn apply_profile(&mut self, profile: &Profile) {
profile.forms.iter().for_each(|profile_form| { profile.forms.iter().for_each(|profile_form| {
if self.name == profile_form.name { if self.name == profile_form.name {
self.entries.entry.iter_mut().for_each(|entry| { self.entries.entry.iter_mut().for_each(|entry| {
@ -158,28 +161,7 @@ impl DataForm {
.form_references .form_references
.iter() .iter()
.for_each(|form_reference| { .for_each(|form_reference| {
if entry.type_ == "formReference" && entry.name == form_reference.name { apply_profile_to_form_entry(entry, form_reference)
if let Some(profile_referenced_data_form) =
&form_reference.referenced_data_form
{
entry.referenced_data_form =
Some(profile_referenced_data_form.clone())
}
if let Some(profile_anzeige) = &form_reference.anzeige {
entry.anzeige = profile_anzeige.clone()
}
if let Some(profile_anzeige_auswahl) =
&form_reference.anzeige_auswahl
{
entry.anzeige_auswahl = Some(profile_anzeige_auswahl.clone())
}
if let Some(scripts_code) = &form_reference.escaped_scripts_code() {
entry.scripts = Some(Script {
code: scripts_code.clone(),
valid: true,
})
}
}
}); });
if let Some(menu_category) = &profile_form.menu_category { if let Some(menu_category) = &profile_form.menu_category {
@ -193,8 +175,10 @@ impl DataForm {
} }
}); });
} }
}
pub fn to_listed_string(&self) -> String { impl Listable for DataForm {
fn to_listed_string(&self) -> String {
format!("Formular '{}' in Revision '{}'", self.name, self.revision) format!("Formular '{}' in Revision '{}'", self.name, self.revision)
} }
} }
@ -399,6 +383,35 @@ pub struct Entry {
einfuegen_verhindern: Option<String>, einfuegen_verhindern: Option<String>,
} }
impl FormEntry for Entry {
fn get_name(&self) -> String {
self.name.clone()
}
fn get_type(&self) -> String {
self.type_.clone()
}
fn update_referenced_data_form(&mut self, value: String) {
self.referenced_data_form = Some(value);
}
fn update_anzeige(&mut self, value: String) {
self.anzeige = value;
}
fn update_anzeige_auswahl(&mut self, value: String) {
self.anzeige_auswahl = Some(value);
}
fn update_scripts_code(&mut self, value: String) {
self.scripts = Some(Script {
code: value,
valid: true,
});
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct DataFormEntries { pub struct DataFormEntries {

View File

@ -22,6 +22,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
use crate::profile::{FormReference, Profile};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
pub mod data_catalogue; pub mod data_catalogue;
@ -204,3 +205,40 @@ pub struct Ordner {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
parent_order: Option<Box<Ordner>>, parent_order: Option<Box<Ordner>>,
} }
fn apply_profile_to_form_entry<E>(entry: &mut E, form_reference: &FormReference)
where
E: FormEntry,
{
if entry.get_type() == "formReference" && entry.get_name() == form_reference.name {
if let Some(profile_referenced_data_form) = &form_reference.referenced_data_form {
entry.update_referenced_data_form(profile_referenced_data_form.clone());
}
if let Some(profile_anzeige) = &form_reference.anzeige {
entry.update_anzeige(profile_anzeige.clone());
}
if let Some(profile_anzeige_auswahl) = &form_reference.anzeige_auswahl {
entry.update_anzeige_auswahl(profile_anzeige_auswahl.clone());
}
if let Some(scripts_code) = &form_reference.escaped_scripts_code() {
entry.update_scripts_code(scripts_code.clone());
}
}
}
pub trait FormEntryContainer {
fn apply_profile(&mut self, profile: &Profile);
}
pub trait Listable {
fn to_listed_string(&self) -> String;
}
pub trait FormEntry {
fn get_name(&self) -> String;
fn get_type(&self) -> String;
fn update_referenced_data_form(&mut self, value: String);
fn update_anzeige(&mut self, value: String);
fn update_anzeige_auswahl(&mut self, value: String);
fn update_scripts_code(&mut self, value: String);
}

View File

@ -31,6 +31,7 @@ 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;
use crate::model::unterformular::Unterformular; use crate::model::unterformular::Unterformular;
use crate::model::{FormEntryContainer, Listable};
use crate::profile::Profile; use crate::profile::Profile;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -52,7 +53,7 @@ impl OnkostarEditor {
}) })
} }
pub fn list_forms(&self) { pub fn print_list(&self) {
println!( println!(
"Die Datei wurde am {} mit {} in Version {} erstellt.\n\nFolgende Inhalte sind gespeichert\n", "Die Datei wurde am {} mit {} in Version {} erstellt.\n\nFolgende Inhalte sind gespeichert\n",
style(&self.info_xml.datum_xml).yellow(), style(&self.info_xml.datum_xml).yellow(),

View File

@ -24,7 +24,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::model::Ordner; use crate::model::{Listable, Ordner};
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
@ -52,8 +52,8 @@ pub struct PropertyCatalogue {
ordner: Ordner, ordner: Ordner,
} }
impl PropertyCatalogue { impl Listable for PropertyCatalogue {
pub fn to_listed_string(&self) -> String { fn to_listed_string(&self) -> String {
format!( format!(
"Merkmalskatalog '{}' in Revision '{}'", "Merkmalskatalog '{}' in Revision '{}'",
self.name, self.revision self.name, self.revision

View File

@ -25,7 +25,10 @@
use console::style; use console::style;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::model::{Ansichten, Entries, Filter, MenuCategory, PlausibilityRules, Script}; use crate::model::{
apply_profile_to_form_entry, Ansichten, Entries, Filter, FormEntry, FormEntryContainer,
Listable, MenuCategory, PlausibilityRules, Script,
};
use crate::model::{Haeufigkeiten, Ordner}; use crate::model::{Haeufigkeiten, Ordner};
use crate::profile::Profile; use crate::profile::Profile;
@ -161,8 +164,8 @@ pub struct Unterformular {
ansichten: Option<Ansichten>, ansichten: Option<Ansichten>,
} }
impl Unterformular { impl FormEntryContainer for Unterformular {
pub fn apply_profile(&mut self, profile: &Profile) { fn apply_profile(&mut self, profile: &Profile) {
profile.forms.iter().for_each(|profile_form| { profile.forms.iter().for_each(|profile_form| {
if self.name == profile_form.name { if self.name == profile_form.name {
self.entries.entry.iter_mut().for_each(|entry| { self.entries.entry.iter_mut().for_each(|entry| {
@ -170,28 +173,7 @@ impl Unterformular {
.form_references .form_references
.iter() .iter()
.for_each(|form_reference| { .for_each(|form_reference| {
if entry.type_ == "formReference" && entry.name == form_reference.name { apply_profile_to_form_entry(entry, form_reference)
if let Some(profile_referenced_data_form) =
&form_reference.referenced_data_form
{
entry.referenced_data_form =
Some(profile_referenced_data_form.clone())
}
if let Some(profile_anzeige) = &form_reference.anzeige {
entry.anzeige = profile_anzeige.clone()
}
if let Some(profile_anzeige_auswahl) =
&form_reference.anzeige_auswahl
{
entry.anzeige_auswahl = Some(profile_anzeige_auswahl.clone())
}
if let Some(scripts_code) = &form_reference.escaped_scripts_code() {
entry.scripts = Some(Script {
code: scripts_code.clone(),
valid: true,
})
}
}
}); });
if let Some(menu_category) = &profile_form.menu_category { if let Some(menu_category) = &profile_form.menu_category {
@ -205,8 +187,10 @@ impl Unterformular {
} }
}); });
} }
}
pub fn to_listed_string(&self) -> String { impl Listable for Unterformular {
fn to_listed_string(&self) -> String {
if self.hat_unterformulare { if self.hat_unterformulare {
return format!( return format!(
"Unterformular '{}' in Revision '{}' {}", "Unterformular '{}' in Revision '{}' {}",
@ -423,6 +407,35 @@ pub struct Entry {
einfuegen_verhindern: Option<String>, einfuegen_verhindern: Option<String>,
} }
impl FormEntry for Entry {
fn get_name(&self) -> String {
self.name.clone()
}
fn get_type(&self) -> String {
self.type_.clone()
}
fn update_referenced_data_form(&mut self, value: String) {
self.referenced_data_form = Some(value);
}
fn update_anzeige(&mut self, value: String) {
self.anzeige = value;
}
fn update_anzeige_auswahl(&mut self, value: String) {
self.anzeige_auswahl = Some(value);
}
fn update_scripts_code(&mut self, value: String) {
self.scripts = Some(Script {
code: value,
valid: true,
});
}
}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct DataFormEntries { pub struct DataFormEntries {