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

index 9ce1c2f90d3ee5233ef624ac8e266337a8806908..1ad980883d68a80d319d7ee0f4cd766ad34ab565 100644 (file)
@@ -161,6 +161,12 @@ fn main() {
        writeln!(output, "use serde::{{Serialize, Deserialize}};");
        writeln!(output, "");
 
+       writeln!(output, "use sqlx::FromRow;");
+       writeln!(output, "");
+
+       writeln!(output, "use chrono::NaiveDate;");
+       writeln!(output, "");
+
        writeln!(output, "use zakform::fields::{{TField, FOption}};");
        writeln!(output, "");
 
@@ -169,6 +175,7 @@ fn main() {
 
        writeln!(output, "let mut f = zakform::form::Form::new();");
 
+       let mut struct_db = String::new();
        let mut struct_form = String::new();
 
        for e in form.elements {
@@ -203,12 +210,28 @@ fn main() {
                }
 
                match e.dbtype.as_str() {
-                       "text" => { },
-                       "integer" => { writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::Integer);"); },
-                       "float" => { writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::Float);"); },
-                       "date" => { writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::Date);"); },
-                       "datetime" => { writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::DateTime);"); },
-                       _ => { },
+                       "text" => {
+                               struct_db.push_str(&format!("pub {}: String,\n", e.id));
+                       },
+                       "integer" => {
+                               struct_db.push_str(&format!("pub {}: i32,\n", e.id));
+                               writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::Integer);");
+                       },
+                       "float" => {
+                               struct_db.push_str(&format!("pub {}: f32,\n", e.id));
+                               writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::Float);");
+                       },
+                       "date" => {
+                               struct_db.push_str(&format!("pub {}: NaiveDate,\n", e.id));
+                               writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::Date);");
+                       },
+                       "datetime" => {
+                               struct_db.push_str(&format!("pub {}: NaiveDateTime,\n", e.id));
+                               writeln!(output, "fi.set_db_type(zakform::fields::FieldDbType::DateTime);");
+                       },
+                       _ => {
+                               struct_db.push_str(&format!("pub {}: String,\n", e.id));
+                       },
                }
 
                if e.max_length > 0 {
@@ -326,6 +349,12 @@ fn main() {
        writeln!(output, "f");
        writeln!(output, "}}");
 
+       /* db struct */
+       writeln!(output, "");
+       writeln!(output, "#[derive(Debug, Serialize, Deserialize, FormRow)]");
+       writeln!(output, "pub struct DomandaDb {{");
+       writeln!(output, "{}}}", struct_db);
+
        /* form struct */
        writeln!(output, "");
        writeln!(output, "#[derive(Debug, Serialize, Deserialize)]");