From da3d3635b567e9daa9d11167495c05078c747515 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Thu, 29 Dec 2022 13:32:45 +0530 Subject: [PATCH] feat: mv form payloads to standalone lib --- Makefile | 4 ++ env/libforms/.gitignore | 1 + env/libforms/Cargo.lock | 89 +++++++++++++++++++++++++++++++++++++++++ env/libforms/Cargo.toml | 14 +++++++ env/libforms/src/lib.rs | 51 +++++++++++++++++++++++ 5 files changed, 159 insertions(+) create mode 100644 env/libforms/.gitignore create mode 100644 env/libforms/Cargo.lock create mode 100644 env/libforms/Cargo.toml create mode 100644 env/libforms/src/lib.rs diff --git a/Makefile b/Makefile index bd87eac..4140b3c 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ default: ## Build app in debug mode check: ## Check for syntax errors on all workspaces cargo check --workspace --tests --all-features + cd env/libforms && cargo check --workspace --tests --all-features clean: ## Delete build artifacts @cargo clean @@ -28,6 +29,9 @@ env: ## Setup development environtment lint: ## Lint codebase cargo fmt -v --all -- --emit files cargo clippy --workspace --tests --all-features + cd env/libforms && cargo fmt -v --all -- --emit files + cd env/libforms && cargo clippy --workspace --tests --all-features + migrate: ## run migrations unset DATABASE_URL && cargo build diff --git a/env/libforms/.gitignore b/env/libforms/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/env/libforms/.gitignore @@ -0,0 +1 @@ +/target diff --git a/env/libforms/Cargo.lock b/env/libforms/Cargo.lock new file mode 100644 index 0000000..debf20d --- /dev/null +++ b/env/libforms/Cargo.lock @@ -0,0 +1,89 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "itoa" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" + +[[package]] +name = "libforms" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "proc-macro2" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" + +[[package]] +name = "serde" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.152" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" diff --git a/env/libforms/Cargo.toml b/env/libforms/Cargo.toml new file mode 100644 index 0000000..07a1524 --- /dev/null +++ b/env/libforms/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "libforms" +version = "0.1.0" +edition = "2021" +repository = "https://git.batsense.net/librepages/forms" +readme = "README.md" +license = "AGPLv3 or later version" +authors = ["realaravinth "] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +serde = { version = "1", features=["derive"]} +serde_json = { version ="1", features = ["raw_value"]} diff --git a/env/libforms/src/lib.rs b/env/libforms/src/lib.rs new file mode 100644 index 0000000..1454317 --- /dev/null +++ b/env/libforms/src/lib.rs @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2022 Aravinth Manivannan + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, Debug)] +#[serde(untagged)] +pub enum FormDType { + Num(f64), + Str(String), +} + +impl FormDType { + pub fn apply_types(&mut self) { + if let Self::Str(data) = self { + if let Ok(num) = data.parse::() { + *self = Self::Num(num); + } + } + } +} + +pub type FormValue = HashMap; + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct Table { + pub host: String, + pub path: String, +} + +#[derive(Serialize, Deserialize, Clone, Debug, Default)] +pub struct FormSubmissionResp { + pub value: Option, + pub time: i64, + pub id: usize, +}