From 95ba0808753127fe8f574d2cdec1ba7123a61fd5 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Thu, 7 Nov 2024 19:54:18 +0100 Subject: [PATCH] feat: change form types to fix some OSC issues This will: * convert "Unterformular" to "DataForm" if flag `Hat_Unterformulare` is set to true * convert "DataForm" to "Unterformular" if required entry for procedure date is not set * sort forms by required form references --- src/commands.rs | 2 +- src/model/form.rs | 35 +++++++++++++++++----- src/model/onkostar_editor.rs | 57 ++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 80dab36..1a6a044 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -207,7 +207,7 @@ pub fn handle(command: SubCommand) -> Result<(), Box> { } if fix { - // No operation as of now + data = data.fix(); } if sorted { diff --git a/src/model/form.rs b/src/model/form.rs index bd01960..f23afb4 100644 --- a/src/model/form.rs +++ b/src/model/form.rs @@ -17,14 +17,6 @@ * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -use console::style; -use serde::{Deserialize, Serialize}; -use std::any::TypeId; -use std::cmp::Ordering; -use std::collections::HashSet; -use std::fmt::Debug; -use std::marker::PhantomData; - use crate::checks::CheckNotice::ErrorWithCode; use crate::checks::{CheckNotice, Checkable}; use crate::model::onkostar_editor::OnkostarEditor; @@ -37,6 +29,15 @@ use crate::model::{ }; use crate::model::{Haeufigkeiten, Ordner}; use crate::profile::Profile; +use console::style; +use quick_xml::de::from_str; +use quick_xml::se::Serializer; +use serde::{Deserialize, Serialize}; +use std::any::TypeId; +use std::cmp::Ordering; +use std::collections::HashSet; +use std::fmt::Debug; +use std::marker::PhantomData; #[derive(Debug)] pub struct DataFormType; @@ -201,6 +202,24 @@ pub struct Form { ansichten: Option, } +impl Form { + pub(crate) fn to_unterformular(&self) -> Form { + let mut buf = String::new(); + let serializer = Serializer::new(&mut buf); + self.serialize(serializer).expect("Generated XML"); + from_str::>(&buf).unwrap() + } +} + +impl Form { + pub(crate) fn to_dataform(&self) -> Form { + let mut buf = String::new(); + let serializer = Serializer::new(&mut buf); + self.serialize(serializer).expect("Generated XML"); + from_str::>(&buf).unwrap() + } +} + impl FormEntryContainer for Form { fn apply_profile(&mut self, profile: &Profile) { profile.forms.iter().for_each(|profile_form| { diff --git a/src/model/onkostar_editor.rs b/src/model/onkostar_editor.rs index bc7ae29..0cb7835 100644 --- a/src/model/onkostar_editor.rs +++ b/src/model/onkostar_editor.rs @@ -211,6 +211,63 @@ impl OnkostarEditor { } } + pub fn fix(mut self) -> Self { + fn has_error1(form: &Form, error_code: &str) -> bool { + form.check() + .iter() + .filter(|&check_notice| match check_notice { + CheckNotice::ErrorWithCode { code, .. } => code == error_code, + _ => false, + }) + .count() + > 0 + } + + fn has_error2(form: &Form, error_code: &str) -> bool { + form.check() + .iter() + .filter(|&check_notice| match check_notice { + CheckNotice::ErrorWithCode { code, .. } => code == error_code, + _ => false, + }) + .count() + > 0 + } + + self.editor + .data_form + .iter_mut() + .filter(|form| has_error2(form, "2023-0002")) + .map(|form| form.to_unterformular()) + .for_each(|form| self.editor.unterformular.push(form)); + + self.editor + .unterformular + .iter_mut() + .filter(|form| has_error1(form, "2023-0001")) + .map(|form| form.to_dataform()) + .for_each(|form| self.editor.data_form.push(form)); + + self.editor + .data_form + .retain(|form| !has_error2(form, "2023-0002")); + + self.editor + .unterformular + .retain(|form| !has_error1(form, "2023-0001")); + + // Sort forms by requirements + self.editor + .data_form + .sort_unstable_by(Form::compare_by_requirement); + + self.editor + .unterformular + .sort_unstable_by(Form::compare_by_requirement); + + self + } + pub fn sorted(&mut self) { self.editor .property_catalogue