From de30149ce53c4b97830dd243fadfc8e30827e7c4 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 4 Jan 2023 14:53:17 +0100 Subject: [PATCH] Function row_from_json. --- Cargo.toml | 4 +++- examples/grid.db | Bin 0 -> 8192 bytes examples/grid.rs | 38 +++++++++++++++++++++++++++++++++++--- src/lib.rs | 23 +++++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 examples/grid.db diff --git a/Cargo.toml b/Cargo.toml index 036a3f9..d6c75d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,10 +9,12 @@ edition = "2021" [dependencies] serde = "1" serde_derive = "1" +serde_json = "1" tera = "1.8.0" [dev-dependencies] config = "0.13" - actix-web = "4" +futures = "0.3" +sqlx = { version = "0.6", features = [ "runtime-async-std-native-tls", "sqlite" ] } diff --git a/examples/grid.db b/examples/grid.db new file mode 100644 index 0000000000000000000000000000000000000000..9a0e89050f8c5ef8c9e6dc56e7e3aa1824c56335 GIT binary patch literal 8192 zcmeI#u}Z^07zgnClG30cCx{Kw<;E7&#aT#FvPd1QeStH%w87*8>9yeK=H?5y34H=z zKzAnxU%}DE-HWt{ldItW&mDL7-FM4xyYsVQZj{KXx|Bv7v28}g0wEYz@TVQQy6}PAc1-6_qso z=5Oxu2gcv{vt1AfKmY;|fB*y_009U<00Izz00jPsfbX_xcZWPTOl27ra~^cEuk1PY r!Y9u;mGxCr$mH@Mcv-&Rziu9PsJ+p=IjN?4q*6b4?BDf1o^C$@b}C6p literal 0 HcmV?d00001 diff --git a/examples/grid.rs b/examples/grid.rs index d1f6189..09dc5e9 100644 --- a/examples/grid.rs +++ b/examples/grid.rs @@ -12,6 +12,10 @@ use actix_web::{ web, App, Error, HttpResponse, HttpServer, Responder, Result, }; +use futures::TryStreamExt; +use sqlx::{Connection, SqliteConnection, Row, Column}; +use std::str::FromStr; + #[derive(Debug, Deserialize)] #[allow(unused)] struct Http { @@ -53,18 +57,26 @@ async fn index( s.push_str(""); s.push_str(""); - let mut col = zakgrid::Column::new("col name"); + let mut col = zakgrid::Column::new("id"); s.push_str(format!("column added: {}
\n", col.name()).as_str()); col.set_title("the column title"); s.push_str(format!("column changed title: {}
\n", col.title()).as_str()); grid.add_column(col); - let mut col = zakgrid::Column::new("col2 name"); + let mut col = zakgrid::Column::new("name"); col.set_title("the second column title"); grid.add_column(col); - let mut col = grid.get_column("col2 name").unwrap(); + let mut col = zakgrid::Column::new("surname"); + grid.add_column(col); + + let mut col = zakgrid::Column::new("age"); col.set_align(zakgrid::Align::Right); + grid.add_column(col); + + let mut col = zakgrid::Column::new("income"); + col.set_align(zakgrid::Align::Right); + grid.add_column(col); let cols = grid.columns(); @@ -76,6 +88,26 @@ async fn index( s.push_str("\n"); let header = grid.header(); s.push_str(header.as_str()); + + let mut conn = SqliteConnection::connect("examples/grid.db").await.unwrap(); + + let mut rows = sqlx::query("SELECT * FROM clients").fetch(&mut conn); + while let Some(row) = rows.try_next().await.unwrap() { + let cols = row.columns(); + + let mut r = String::new(); + r.push_str("{"); + for col in cols { + let s: &str = row.try_get_unchecked(col.name()).unwrap(); + r.push_str(format!("\"{}\": \"{}\",", col.name(), s).as_str()); + } + r.truncate(r.len() - 1); + r.push_str("}"); + + let val = serde_json::Value::from_str(r.as_str()).unwrap(); + s.push_str(grid.row_from_json(&val).as_str()); + } + s.push_str("
\n"); s.push_str("\n"); diff --git a/src/lib.rs b/src/lib.rs index 7088ef4..b161b66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +use serde_json::Value; use serde_derive::Serialize; use tera::Tera; @@ -128,4 +129,26 @@ impl Grid { context.insert("row", s.as_str()); String::from(tera.render("header", &context).unwrap().as_str()) } + + pub fn row_from_json(&self, value: &serde_json::Value) -> String { + let mut s = String::new(); + + let mut tera = Tera::default(); + + tera.add_raw_template("row", &self.row_tmpl); + tera.add_raw_template("row_col", &self.row_col_tmpl); + + for c in &self.columns { + if !c.hidden { + let mut context = tera::Context::new(); + context.insert("value", &value[c.name.as_str()]); + context.insert("align", &c.align); + s.push_str(tera.render("row_col", &context).unwrap().as_str()); + } + } + + let mut context = tera::Context::new(); + context.insert("row", s.as_str()); + String::from(tera.render("row", &context).unwrap().as_str()) + } } -- 2.49.0