]> saetta.ns0.it Git - rust/zakform/commitdiff
c2rs: added with_attachments.
authorAndrea Zagli <azagli@libero.it>
Wed, 1 Oct 2025 09:50:16 +0000 (11:50 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 1 Oct 2025 09:50:16 +0000 (11:50 +0200)
src/bin/c2rs.rs

index 5c8d8cacb4ad24514cba6078e4d50aa3618fc026..dc7830a85b8c010163ded785bf6a80bdc3f4900f 100644 (file)
@@ -170,12 +170,13 @@ fn main() {
        writeln!(output, "use zakform::fields::{{TField, FOption}};");
        writeln!(output, "");
 
-       writeln!(output, "pub fn get_form() -> (zakform::form::Form, String) {{");
+       writeln!(output, "pub fn get_form() -> (zakform::form::Form, String, String) {{");
        writeln!(output, "");
 
        writeln!(output, "let mut f = zakform::form::Form::new();");
        writeln!(output, "");
        writeln!(output, "let mut with_validators = String::new();");
+       writeln!(output, "let mut with_attachments = String::new();");
 
        let mut struct_db = String::new();
        let mut struct_form = String::new();
@@ -312,7 +313,7 @@ fn main() {
                        };
                }
 
-               for (i, validator) in e.validators.into_iter().enumerate() {
+               for (i, validator) in e.validators.iter().enumerate() {
                        match validator.vtype.as_str() {
                                "zak_form_element_validator_notempty" => { writeln!(output, r#"fi.add_validator(Box::new(zakform::validators::ValidatorNotEmpty::new("{}_notempty")));"#, e.id); },
                                "zak_form_element_validator_date" => {
@@ -353,11 +354,26 @@ fn main() {
                }
 
                writeln!(output, "f.add_field(Box::new(fi));");
-               writeln!(output, r#"with_validators.push_str(",'{}'");"#, e.id);
+
+               if !e.validators.is_empty() {
+                       writeln!(output, r#"with_validators.push_str(",'{}'");"#, e.id);
+               }
+
+               if e.etype == "zak_form_cgi_form_element_file" {
+                       writeln!(output, r#"with_attachments.push_str(",'{}'");"#, e.id);
+               }
        }
 
        writeln!(output, "");
-       writeln!(output, "(f, with_validators)");
+       writeln!(output, r#"if with_validators.starts_with(",") {{
+                       with_validators.remove(0);
+               }}"#);
+       writeln!(output, r#"if with_attachments.starts_with(",") {{
+                       with_attachments.remove(0);
+               }}"#);
+
+       writeln!(output, "");
+       writeln!(output, "(f, with_validators, with_attachments)");
        writeln!(output, "}}");
 
        /* db struct */