diff --git a/Cargo.lock b/Cargo.lock index fc4ca3b..f2f0f1b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "bitflags" version = "1.3.2" @@ -54,12 +60,34 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" + [[package]] name = "memchr" version = "2.5.0" @@ -79,6 +107,7 @@ dependencies = [ "clap", "quick-xml", "serde", + "serde_yaml", "xml-rs", ] @@ -110,6 +139,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "ryu" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" + [[package]] name = "serde" version = "1.0.163" @@ -130,6 +165,19 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_yaml" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "syn" version = "2.0.18" @@ -147,6 +195,12 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" +[[package]] +name = "unsafe-libyaml" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6" + [[package]] name = "xml-rs" version = "0.8.13" diff --git a/Cargo.toml b/Cargo.toml index b8d9373..634e199 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] clap = { version = "4.3", features = ["std", "help", "usage", "derive", "error-context" ], default-features = false } serde = { version = "1.0", features = ["derive"] } +serde_yaml = "0.9" quick-xml = { version = "0.28", features = ["escape-html", "serialize"], default-features=false } xml-rs = "0.8" diff --git a/src/main.rs b/src/main.rs index faaf21e..e95f52a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,7 @@ use serde::Serialize; use crate::model::onkostar_editor::OnkostarEditor; mod model; +mod profile; #[derive(Parser)] #[command(author, version, about, long_about = None)] @@ -45,6 +46,11 @@ struct Cli { help = "Eingabedatei" )] input: String, + #[arg( + long = "profile", + help = "Profildatei (Optional)" + )] + profile: Option, #[arg( long = "output", help = "Ausgabedatei (Optional)" diff --git a/src/profile.rs b/src/profile.rs new file mode 100644 index 0000000..2b00018 --- /dev/null +++ b/src/profile.rs @@ -0,0 +1,95 @@ +/* + * MIT License + * + * Copyright (c) 2023 Comprehensive Cancer Center Mainfranken + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +use std::str::FromStr; +use serde::Deserialize; + +#[derive(Deserialize)] +struct Profile { + forms: Vec
+} + +impl FromStr for Profile { + type Err = String; + + fn from_str(s: &str) -> Result { + match serde_yaml::from_str::(s) { + Ok(profile) => Ok(profile), + Err(err) => Err(err.to_string()) + } + } +} + +#[derive(Deserialize)] +struct Form { + name: String, + fields: Vec +} + +#[derive(Deserialize)] +struct FieldChanges { + name: String, + referenced_data_form: Option +} + +#[cfg(test)] +mod tests { + use std::str::FromStr; + use crate::profile::Profile; + + #[test] + fn should_deserialize_profile() { + let content = + "forms: + - name: 'DNPM Therapieplan' + fields: + - name: ref_first_mtb + referenced_data_form: 'OS.Tumorkonferenz.VarianteUKW' + "; + + match Profile::from_str(content) { + Ok(profile) => { + assert_eq!(profile.forms.len(), 1); + assert_eq!(profile.forms[0].name, "DNPM Therapieplan"); + assert_eq!(profile.forms[0].fields.len(), 1); + assert_eq!(profile.forms[0].fields[0].name, "ref_first_mtb"); + assert_eq!(profile.forms[0].fields[0].referenced_data_form, Some("OS.Tumorkonferenz.VarianteUKW".to_string())); + }, + Err(e) => panic!("Cannot deserialize profile: {}", e) + } + } + + #[test] + fn should_not_deserialize_bad_profile() { + let content = + "forms: + - name: 'DNPM Therapieplan' + # incomplete profile ... + "; + + let actual = Profile::from_str(content); + assert!(actual.is_err()); + } + +} \ No newline at end of file