]> saetta.ns0.it Git - rust/zakform/commitdiff
libzakform conversion: moved source in bin directory.
authorAndrea Zagli <azagli@libero.it>
Wed, 20 Sep 2023 15:34:12 +0000 (17:34 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 22 Sep 2023 09:04:42 +0000 (11:04 +0200)
src/bin/c2rs.rs [new file with mode: 0644]
src/main.rs [deleted file]

diff --git a/src/bin/c2rs.rs b/src/bin/c2rs.rs
new file mode 100644 (file)
index 0000000..84e26b1
--- /dev/null
@@ -0,0 +1,184 @@
+use std::{
+       env,
+       fs
+};
+
+use quick_xml::de::from_str;
+use serde::Deserialize;
+use serde_derive::Deserialize;
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "zakform")]
+pub struct ZakForm {
+       #[serde(rename = "element")]
+       pub elements: Vec<ZakFormElement>
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "element")]
+pub struct ZakFormElement {
+       #[serde(rename = "@type")]
+       pub etype: String,
+       pub id: String,
+       pub label: Option<String>,
+       #[serde(rename = "long-name")]
+       pub long_name: Option<String>,
+       #[serde(rename = "type")]
+       pub dbtype: String,
+       #[serde(rename = "maxlength")]
+       pub max_length: i32,
+       pub rows: i32,
+       pub format: Option<ZakFormElementFormat>,
+       #[serde(rename = "default-value")]
+       pub default_value: String,
+       pub editable: String,
+       #[serde(rename = "to-load")]
+       pub to_load: String,
+       #[serde(rename = "to-save")]
+       pub to_save: String,
+       pub placeholder: String,
+
+       #[serde(rename = "option")]
+       pub options: Vec<ZakFormElementSelectOption>,
+
+       #[serde(rename = "zak-cgi-options")]
+       pub zakcgi_options: Option<ZakFormElementRadioOptions>,
+
+       #[serde(rename = "zak-cgi-text")]
+       pub zakcgi_text: String,
+
+       #[serde(rename = "zak-cgi-inline")]
+       pub zakcgi_inline: String,
+
+       #[serde(rename = "filter")]
+       pub filters: Vec<ZakFormElementFilter>,
+
+       #[serde(rename = "validator")]
+       pub validators: Vec<ZakFormElementValidator>,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "format")]
+pub struct ZakFormElementFormat {
+       #[serde(rename = "@thousands_separator")]
+       pub thousands_separator: String,
+       #[serde(rename = "@decimals")]
+       pub decimals: i32,
+       #[serde(rename = "$text")]
+       pub format: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "filter")]
+pub struct ZakFormElementFilter {
+       #[serde(rename = "@type")]
+       pub ftype: String,
+       #[serde(rename = "$text")]
+       pub text: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "validator")]
+pub struct ZakFormElementValidator {
+       #[serde(rename = "@type")]
+       pub vtype: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "option")]
+pub struct ZakFormElementSelectOption {
+       #[serde(rename = "@id")]
+       pub id: String,
+       #[serde(rename = "$text")]
+       pub text: String,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "zak-cgi-options")]
+pub struct ZakFormElementRadioOptions {
+       #[serde(rename = "option")]
+       pub options: Vec<ZakFormElementRadioOption>,
+}
+
+#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
+#[serde(default, rename = "option")]
+pub struct ZakFormElementRadioOption {
+       #[serde(rename = "@value")]
+       pub value: String,
+       #[serde(rename = "@content")]
+       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 = from_str(&content).unwrap();
+
+       println!("{:?}", form);
+
+       println!("let mut f = zakform::form::Form::new();");
+
+       for e in form.elements {
+               println!("");
+
+               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 let Some(l) = e.label {
+                       if l != "" {
+                               println!(r#"fi.set_label("{}")"#, l);
+                       }
+               }
+
+               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 option in e.options {
+                       println!(r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.id, option.text);
+               }
+
+               match e.zakcgi_options {
+                       Some(zako) => {
+                               for option in zako.options {
+                                       println!(r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.value, option.content);
+                               }
+                       },
+                       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);");
+       }
+}
diff --git a/src/main.rs b/src/main.rs
deleted file mode 100644 (file)
index 84e26b1..0000000
+++ /dev/null
@@ -1,184 +0,0 @@
-use std::{
-       env,
-       fs
-};
-
-use quick_xml::de::from_str;
-use serde::Deserialize;
-use serde_derive::Deserialize;
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "zakform")]
-pub struct ZakForm {
-       #[serde(rename = "element")]
-       pub elements: Vec<ZakFormElement>
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "element")]
-pub struct ZakFormElement {
-       #[serde(rename = "@type")]
-       pub etype: String,
-       pub id: String,
-       pub label: Option<String>,
-       #[serde(rename = "long-name")]
-       pub long_name: Option<String>,
-       #[serde(rename = "type")]
-       pub dbtype: String,
-       #[serde(rename = "maxlength")]
-       pub max_length: i32,
-       pub rows: i32,
-       pub format: Option<ZakFormElementFormat>,
-       #[serde(rename = "default-value")]
-       pub default_value: String,
-       pub editable: String,
-       #[serde(rename = "to-load")]
-       pub to_load: String,
-       #[serde(rename = "to-save")]
-       pub to_save: String,
-       pub placeholder: String,
-
-       #[serde(rename = "option")]
-       pub options: Vec<ZakFormElementSelectOption>,
-
-       #[serde(rename = "zak-cgi-options")]
-       pub zakcgi_options: Option<ZakFormElementRadioOptions>,
-
-       #[serde(rename = "zak-cgi-text")]
-       pub zakcgi_text: String,
-
-       #[serde(rename = "zak-cgi-inline")]
-       pub zakcgi_inline: String,
-
-       #[serde(rename = "filter")]
-       pub filters: Vec<ZakFormElementFilter>,
-
-       #[serde(rename = "validator")]
-       pub validators: Vec<ZakFormElementValidator>,
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "format")]
-pub struct ZakFormElementFormat {
-       #[serde(rename = "@thousands_separator")]
-       pub thousands_separator: String,
-       #[serde(rename = "@decimals")]
-       pub decimals: i32,
-       #[serde(rename = "$text")]
-       pub format: String,
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "filter")]
-pub struct ZakFormElementFilter {
-       #[serde(rename = "@type")]
-       pub ftype: String,
-       #[serde(rename = "$text")]
-       pub text: String,
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "validator")]
-pub struct ZakFormElementValidator {
-       #[serde(rename = "@type")]
-       pub vtype: String,
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "option")]
-pub struct ZakFormElementSelectOption {
-       #[serde(rename = "@id")]
-       pub id: String,
-       #[serde(rename = "$text")]
-       pub text: String,
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "zak-cgi-options")]
-pub struct ZakFormElementRadioOptions {
-       #[serde(rename = "option")]
-       pub options: Vec<ZakFormElementRadioOption>,
-}
-
-#[derive(Default, Debug, Clone, PartialEq, Deserialize)]
-#[serde(default, rename = "option")]
-pub struct ZakFormElementRadioOption {
-       #[serde(rename = "@value")]
-       pub value: String,
-       #[serde(rename = "@content")]
-       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 = from_str(&content).unwrap();
-
-       println!("{:?}", form);
-
-       println!("let mut f = zakform::form::Form::new();");
-
-       for e in form.elements {
-               println!("");
-
-               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 let Some(l) = e.label {
-                       if l != "" {
-                               println!(r#"fi.set_label("{}")"#, l);
-                       }
-               }
-
-               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 option in e.options {
-                       println!(r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.id, option.text);
-               }
-
-               match e.zakcgi_options {
-                       Some(zako) => {
-                               for option in zako.options {
-                                       println!(r#"fi.add_option(zakform::fields::FOption{{ value: String::from("{}"), label: String::from("{}") }});"#, option.value, option.content);
-                               }
-                       },
-                       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);");
-       }
-}