feat: mv form payloads to standalone lib

This commit is contained in:
Aravinth Manivannan 2022-12-29 13:32:45 +05:30
parent aa50dc1883
commit da3d3635b5
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
5 changed files with 159 additions and 0 deletions

View file

@ -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

1
env/libforms/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

89
env/libforms/Cargo.lock generated vendored Normal file
View file

@ -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"

14
env/libforms/Cargo.toml vendored Normal file
View file

@ -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 <realaravinth@batsense.net>"]
# 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"]}

51
env/libforms/src/lib.rs vendored Normal file
View file

@ -0,0 +1,51 @@
/*
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
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::<f64>() {
*self = Self::Num(num);
}
}
}
}
pub type FormValue = HashMap<String, FormDType>;
#[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<serde_json::Value>,
pub time: i64,
pub id: usize,
}