From: Andrea Zagli Date: Wed, 20 Sep 2023 15:34:12 +0000 (+0200) Subject: libzakform conversion: moved source in bin directory. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c19dfe1bc0672c6a43879e4c79644e120c4cb2b8;p=rust%2Fzakform libzakform conversion: moved source in bin directory. --- diff --git a/src/bin/c2rs.rs b/src/bin/c2rs.rs new file mode 100644 index 0000000..84e26b1 --- /dev/null +++ b/src/bin/c2rs.rs @@ -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 +} + +#[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, + #[serde(rename = "long-name")] + pub long_name: Option, + #[serde(rename = "type")] + pub dbtype: String, + #[serde(rename = "maxlength")] + pub max_length: i32, + pub rows: i32, + pub format: Option, + #[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, + + #[serde(rename = "zak-cgi-options")] + pub zakcgi_options: Option, + + #[serde(rename = "zak-cgi-text")] + pub zakcgi_text: String, + + #[serde(rename = "zak-cgi-inline")] + pub zakcgi_inline: String, + + #[serde(rename = "filter")] + pub filters: Vec, + + #[serde(rename = "validator")] + pub validators: Vec, +} + +#[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, +} + +#[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 index 84e26b1..0000000 --- a/src/main.rs +++ /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 -} - -#[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, - #[serde(rename = "long-name")] - pub long_name: Option, - #[serde(rename = "type")] - pub dbtype: String, - #[serde(rename = "maxlength")] - pub max_length: i32, - pub rows: i32, - pub format: Option, - #[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, - - #[serde(rename = "zak-cgi-options")] - pub zakcgi_options: Option, - - #[serde(rename = "zak-cgi-text")] - pub zakcgi_text: String, - - #[serde(rename = "zak-cgi-inline")] - pub zakcgi_inline: String, - - #[serde(rename = "filter")] - pub filters: Vec, - - #[serde(rename = "validator")] - pub validators: Vec, -} - -#[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, -} - -#[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);"); - } -}