summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/json.rs10
-rw-r--r--src/main.rs1
-rw-r--r--src/models/model.rs5
3 files changed, 15 insertions, 1 deletions
diff --git a/src/json.rs b/src/json.rs
new file mode 100644
index 0000000..9515661
--- /dev/null
+++ b/src/json.rs
@@ -0,0 +1,10 @@
+use serde::{de, Deserialize, Deserializer};
+use serde_json::Value;
+
+pub fn number_from_string<'de, D: Deserializer<'de>>(deserializer: D) -> Result<f64, D::Error> {
+ Ok(match Value::deserialize(deserializer)? {
+ Value::String(s) => s.parse().map_err(de::Error::custom)?,
+ Value::Number(num) => num.as_f64().ok_or(de::Error::custom("Invalid number"))?,
+ _ => return Err(de::Error::custom("wrong type")),
+ })
+}
diff --git a/src/main.rs b/src/main.rs
index 66caecd..b13be75 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@ mod config;
mod db;
mod errors;
mod files;
+mod json;
mod logger;
mod models;
mod pagination;
diff --git a/src/models/model.rs b/src/models/model.rs
index 03f872e..469356f 100644
--- a/src/models/model.rs
+++ b/src/models/model.rs
@@ -1,4 +1,4 @@
-use crate::{config::CONFIG, db::get_client, errors::AppError};
+use crate::{config::CONFIG, db::get_client, errors::AppError, json::number_from_string};
use serde_json::json;
use sqlx::types::JsonValue;
use sqlx::Row;
@@ -29,8 +29,11 @@ pub struct Model {
pub struct ModelCreate {
pub name: String,
pub description: Option<String>,
+ #[serde(deserialize_with = "number_from_string")]
pub duration: f64,
+ #[serde(deserialize_with = "number_from_string")]
pub height: f64,
+ #[serde(deserialize_with = "number_from_string")]
pub weight: f64,
pub printer: Option<String>,
pub material: Option<String>,