]> saetta.ns0.it Git - rust/zakform/commitdiff
Started conversion from libzakform file format.
authorAndrea Zagli <azagli@libero.it>
Wed, 20 Sep 2023 14:13:00 +0000 (16:13 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 20 Sep 2023 14:13:00 +0000 (16:13 +0200)
Cargo.toml
src/main.rs [new file with mode: 0644]

index ba086aa8fb73b598344a3110ce67b22452f33dac..f9912611f03bd23ba68028fce1ae4a7f0f0057c8 100644 (file)
@@ -9,6 +9,8 @@ edition = "2021"
 serde = "1"
 serde_derive = "1"
 serde_json = "1"
+yaserde = "0.8.0"
+yaserde_derive = "0.8.0"
 tera = "1.8.0"
 
 [dev-dependencies]
diff --git a/src/main.rs b/src/main.rs
new file mode 100644 (file)
index 0000000..1662fb1
--- /dev/null
@@ -0,0 +1,152 @@
+use std::{
+       env,
+       fs
+};
+
+use yaserde::*;
+use yaserde_derive::*;
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(root = "zakform")]
+pub struct ZakForm {
+       #[yaserde(rename = "element")]
+       pub elements: Vec<ZakFormElement>
+}
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(rename = "element")]
+pub struct ZakFormElement {
+       #[yaserde(attribute, rename = "type")]
+       pub etype: String,
+       pub id: String,
+       pub label: String,
+       #[yaserde(rename = "long-name")]
+       pub long_name: String,
+       #[yaserde(rename = "dbtype")]
+       pub dbtype: String,
+       #[yaserde(rename = "maxlength")]
+       pub max_length: i32,
+       pub rows: i32,
+       pub format: Option<ZakFormElementFormat>,
+       #[yaserde(rename = "default-value")]
+       pub default_value: String,
+       pub editable: String,
+       #[yaserde(rename = "to-load")]
+       pub to_load: String,
+       #[yaserde(rename = "to-save")]
+       pub to_save: String,
+       pub placeholder: String,
+
+       #[yaserde(rename = "zak-cgi-options")]
+       pub zakcgi_options: Option<ZakFormElementRadioOptions>,
+
+       #[yaserde(rename = "zak-cgi-text")]
+       pub zakcgi_text: String,
+
+       #[yaserde(rename = "zak-cgi-inline")]
+       pub zakcgi_inline: String,
+
+       #[yaserde(rename = "filter")]
+       pub filters: Vec<ZakFormElementFilter>,
+
+       #[yaserde(rename = "validator")]
+       pub validators: Vec<ZakFormElementValidator>,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(rename = "format")]
+pub struct ZakFormElementFormat {
+       #[yaserde(attribute)]
+       pub thousands_separator: String,
+       #[yaserde(attribute)]
+       pub decimals: i32,
+       #[yaserde(text)]
+       pub format: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(rename = "filter")]
+pub struct ZakFormElementFilter {
+       #[yaserde(attribute, rename = "type")]
+       pub ftype: String,
+       #[yaserde(text)]
+       pub text: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(rename = "validator")]
+pub struct ZakFormElementValidator {
+       #[yaserde(attribute, rename = "type")]
+       pub vtype: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(rename = "zak-cgi-options")]
+pub struct ZakFormElementRadioOptions {
+       #[yaserde(rename = "option")]
+       pub options: Vec<ZakFormElementRadioOption>,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, YaDeserialize)]
+#[yaserde(rename = "option")]
+pub struct ZakFormElementRadioOption {
+       pub value: String,
+       pub content: String,
+}
+
+fn main() {
+       let formfile = env::args().nth(1).expect("no form file given");
+
+       let content = fs::read_to_string(formfile).expect("something went wrong reading the file");
+
+       let form: ZakForm = de::from_str(&content).unwrap();
+
+       println!("{:?}", form);
+
+       println!("let mut f = zakform::form::Form::new();");
+
+       for e in form.elements {
+               match e.etype.as_str() {
+                       "zak_form_cgi_form_element_text" => { println!(r#"let mut fi = zakform::fields::FieldText::new("{}")"#, e.id); },
+                       "zak_form_cgi_form_element_text_area" => { println!(r#"let mut fi = zakform::fields::FieldTextArea::new("{}")"#, e.id); },
+                       "zak_form_cgi_form_element_radio" => { println!(r#"let mut fi = zakform::fields::FieldRadio::new("{}")"#, e.id); },
+                       "zak_form_cgi_form_element_select" => { println!(r#"let mut fi = zakform::fields::FieldSelect::new("{}")"#, e.id); },
+                       _ => { continue; },
+               };
+
+               if e.label != "" {
+                       println!(r#"fi.set_label("{}")"#, e.label);
+               }
+
+               if e.max_length > 0 {
+                       println!("fi.set_maxlen({})", e.max_length);
+               }
+               if e.rows > 0 {
+                       println!("fi.set_rows({})", e.rows);
+               }
+
+               match e.format {
+                       Some(f) => {
+
+                       },
+                       None => { },
+               };
+
+               for filter in e.filters {
+                       match filter.ftype.as_str() {
+                               "zak_form_element_filter_trim" => { println!("fi.add_filter(zakform::filters::filter_trim);"); },
+                               "zak_form_element_filter_luc" => {
+                                       match filter.text.as_str() {
+                                               "upper" => { println!("fi.add_filter(zakform::filters::filter_uppercase);"); },
+                                               "lower" => { println!("fi.add_filter(zakform::filters::filter_lowercase);"); },
+                                               "camel" => { println!("fi.add_filter(zakform::filters::filter_camelcase);"); },
+                                               _ => { },
+                                       };
+                               },
+                               _ => { },
+                       };
+               }
+
+               println!("f.add_field(Box::new(fi);");
+       }
+}