]> saetta.ns0.it Git - rust/zakform/commitdiff
c2rs: added form struct.
authorAndrea Zagli <azagli@libero.it>
Fri, 10 May 2024 07:12:10 +0000 (09:12 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 10 May 2024 07:12:10 +0000 (09:12 +0200)
src/bin/c2rs.rs

index 084f7271a2e6864c24888cf276b572f13311304b..9ce1c2f90d3ee5233ef624ac8e266337a8806908 100644 (file)
@@ -158,6 +158,9 @@ fn main() {
 
        println!("{:?}", form);
 
+       writeln!(output, "use serde::{{Serialize, Deserialize}};");
+       writeln!(output, "");
+
        writeln!(output, "use zakform::fields::{{TField, FOption}};");
        writeln!(output, "");
 
@@ -166,9 +169,17 @@ fn main() {
 
        writeln!(output, "let mut f = zakform::form::Form::new();");
 
+       let mut struct_form = String::new();
+
        for e in form.elements {
                writeln!(output, "");
 
+               if e.etype.as_str() == "zak_form_cgi_form_element_submit" {
+                       struct_form.push_str(&format!("pub {}: Option<String>,\n", e.id));
+               } else {
+                       struct_form.push_str(&format!("pub {}: String,\n", e.id));
+               }
+
                match e.etype.as_str() {
                        "zak_form_cgi_form_element_text"
                                | "zak_form_cgi_form_element_email" => { writeln!(output, r#"let mut fi = zakform::fields::FieldText::new("{}");"#, e.id); },
@@ -314,4 +325,11 @@ fn main() {
        writeln!(output, "");
        writeln!(output, "f");
        writeln!(output, "}}");
+
+       /* form struct */
+       writeln!(output, "");
+       writeln!(output, "#[derive(Debug, Serialize, Deserialize)]");
+       writeln!(output, "pub struct DomandaForm {{");
+       writeln!(output, "{}}}", struct_form);
+
 }