From 13f1126619f49352952d9765e2fda9bc7c60187d Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Thu, 15 Jun 2023 22:54:25 +0200 Subject: [PATCH] Use traits for catalogues, forms and form entries --- src/main.rs | 2 +- src/model/data_catalogue.rs | 6 +-- src/model/data_form.rs | 65 ++++++++++++++++++++------------- src/model/mod.rs | 38 +++++++++++++++++++ src/model/onkostar_editor.rs | 3 +- src/model/property_catalogue.rs | 6 +-- src/model/unterformular.rs | 65 ++++++++++++++++++++------------- 7 files changed, 125 insertions(+), 60 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7fd84ce..d52dd7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,7 +111,7 @@ fn main() -> Result<(), Box> { match cli.command { Command::List { inputfile } => { let data = read_inputfile(inputfile)?; - data.list_forms(); + data.print_list(); } Command::Modify { inputfile, diff --git a/src/model/data_catalogue.rs b/src/model/data_catalogue.rs index 692fbd4..224d210 100644 --- a/src/model/data_catalogue.rs +++ b/src/model/data_catalogue.rs @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize}; -use crate::model::Ordner; +use crate::model::{Listable, Ordner}; #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] @@ -55,8 +55,8 @@ pub struct DataCatalogue { ordner: Ordner, } -impl DataCatalogue { - pub fn to_listed_string(&self) -> String { +impl Listable for DataCatalogue { + fn to_listed_string(&self) -> String { format!( "Datenkatalog '{}' in Revision '{}'", self.name, self.revision diff --git a/src/model/data_form.rs b/src/model/data_form.rs index c7ef07a..dc6c63e 100644 --- a/src/model/data_form.rs +++ b/src/model/data_form.rs @@ -24,7 +24,10 @@ 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::profile::Profile; @@ -149,8 +152,8 @@ pub struct DataForm { ansichten: Option, } -impl DataForm { - pub fn apply_profile(&mut self, profile: &Profile) { +impl FormEntryContainer for DataForm { + fn apply_profile(&mut self, profile: &Profile) { profile.forms.iter().for_each(|profile_form| { if self.name == profile_form.name { self.entries.entry.iter_mut().for_each(|entry| { @@ -158,28 +161,7 @@ impl DataForm { .form_references .iter() .for_each(|form_reference| { - if entry.type_ == "formReference" && entry.name == form_reference.name { - 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, - }) - } - } + apply_profile_to_form_entry(entry, form_reference) }); 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) } } @@ -399,6 +383,35 @@ pub struct Entry { einfuegen_verhindern: Option, } +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)] #[serde(deny_unknown_fields)] pub struct DataFormEntries { diff --git a/src/model/mod.rs b/src/model/mod.rs index 7433b4d..fee45db 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -22,6 +22,7 @@ * SOFTWARE. */ +use crate::profile::{FormReference, Profile}; use serde::{Deserialize, Serialize}; pub mod data_catalogue; @@ -204,3 +205,40 @@ pub struct Ordner { #[serde(skip_serializing_if = "Option::is_none")] parent_order: Option>, } + +fn apply_profile_to_form_entry(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); +} diff --git a/src/model/onkostar_editor.rs b/src/model/onkostar_editor.rs index d40f3e2..9e8007c 100644 --- a/src/model/onkostar_editor.rs +++ b/src/model/onkostar_editor.rs @@ -31,6 +31,7 @@ use crate::model::data_catalogue::DataCatalogue; use crate::model::data_form::DataForm; use crate::model::property_catalogue::PropertyCatalogue; use crate::model::unterformular::Unterformular; +use crate::model::{FormEntryContainer, Listable}; use crate::profile::Profile; #[derive(Serialize, Deserialize, Debug)] @@ -52,7 +53,7 @@ impl OnkostarEditor { }) } - pub fn list_forms(&self) { + pub fn print_list(&self) { println!( "Die Datei wurde am {} mit {} in Version {} erstellt.\n\nFolgende Inhalte sind gespeichert\n", style(&self.info_xml.datum_xml).yellow(), diff --git a/src/model/property_catalogue.rs b/src/model/property_catalogue.rs index 341b7ec..7e0bfab 100644 --- a/src/model/property_catalogue.rs +++ b/src/model/property_catalogue.rs @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize}; -use crate::model::Ordner; +use crate::model::{Listable, Ordner}; #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] @@ -52,8 +52,8 @@ pub struct PropertyCatalogue { ordner: Ordner, } -impl PropertyCatalogue { - pub fn to_listed_string(&self) -> String { +impl Listable for PropertyCatalogue { + fn to_listed_string(&self) -> String { format!( "Merkmalskatalog '{}' in Revision '{}'", self.name, self.revision diff --git a/src/model/unterformular.rs b/src/model/unterformular.rs index 2995b44..7858d99 100644 --- a/src/model/unterformular.rs +++ b/src/model/unterformular.rs @@ -25,7 +25,10 @@ use console::style; 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::profile::Profile; @@ -161,8 +164,8 @@ pub struct Unterformular { ansichten: Option, } -impl Unterformular { - pub fn apply_profile(&mut self, profile: &Profile) { +impl FormEntryContainer for Unterformular { + fn apply_profile(&mut self, profile: &Profile) { profile.forms.iter().for_each(|profile_form| { if self.name == profile_form.name { self.entries.entry.iter_mut().for_each(|entry| { @@ -170,28 +173,7 @@ impl Unterformular { .form_references .iter() .for_each(|form_reference| { - if entry.type_ == "formReference" && entry.name == form_reference.name { - 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, - }) - } - } + apply_profile_to_form_entry(entry, form_reference) }); 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 { return format!( "Unterformular '{}' in Revision '{}' {}", @@ -423,6 +407,35 @@ pub struct Entry { einfuegen_verhindern: Option, } +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)] #[serde(deny_unknown_fields)] pub struct DataFormEntries {