From 441681fa8e31190018a816b776deb174d40736d1 Mon Sep 17 00:00:00 2001 From: sunce86 Date: Mon, 9 Dec 2024 15:44:54 +0100 Subject: [PATCH] Reveal and settle to receive integer solution id --- crates/driver/src/infra/api/routes/mod.rs | 31 +++++++++++++++++++ .../infra/api/routes/reveal/dto/solution.rs | 4 +-- .../infra/api/routes/settle/dto/solution.rs | 4 +-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/crates/driver/src/infra/api/routes/mod.rs b/crates/driver/src/infra/api/routes/mod.rs index ee1027b0e9..674619b418 100644 --- a/crates/driver/src/infra/api/routes/mod.rs +++ b/crates/driver/src/infra/api/routes/mod.rs @@ -15,3 +15,34 @@ pub(super) use { settle::settle, solve::{solve, AuctionError}, }; + +pub fn deserialize_solution_id<'de, D>(deserializer: D) -> Result +where + D: serde::Deserializer<'de>, +{ + struct SolutionIdVisitor; + + impl serde::de::Visitor<'_> for SolutionIdVisitor { + type Value = u64; + + fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + formatter.write_str("a string or integer representing a solution ID") + } + + fn visit_u64(self, value: u64) -> Result + where + E: serde::de::Error, + { + Ok(value) + } + + fn visit_str(self, value: &str) -> Result + where + E: serde::de::Error, + { + value.parse::().map_err(serde::de::Error::custom) + } + } + + deserializer.deserialize_any(SolutionIdVisitor) +} diff --git a/crates/driver/src/infra/api/routes/reveal/dto/solution.rs b/crates/driver/src/infra/api/routes/reveal/dto/solution.rs index 77a80e28b1..0c8d55fa49 100644 --- a/crates/driver/src/infra/api/routes/reveal/dto/solution.rs +++ b/crates/driver/src/infra/api/routes/reveal/dto/solution.rs @@ -1,11 +1,11 @@ -use {serde::Deserialize, serde_with::serde_as}; +use {super::super::super::deserialize_solution_id, serde::Deserialize, serde_with::serde_as}; #[serde_as] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Solution { /// Unique ID of the solution (per driver competition), to reveal. - #[serde_as(as = "serde_with::DisplayFromStr")] + #[serde(deserialize_with = "deserialize_solution_id")] pub solution_id: u64, /// Auction ID in which the specified solution ID is competing. #[serde_as(as = "Option")] diff --git a/crates/driver/src/infra/api/routes/settle/dto/solution.rs b/crates/driver/src/infra/api/routes/settle/dto/solution.rs index cbe0441ac3..6c62cd0cdd 100644 --- a/crates/driver/src/infra/api/routes/settle/dto/solution.rs +++ b/crates/driver/src/infra/api/routes/settle/dto/solution.rs @@ -1,11 +1,11 @@ -use {serde::Deserialize, serde_with::serde_as}; +use {super::super::super::deserialize_solution_id, serde::Deserialize, serde_with::serde_as}; #[serde_as] #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Solution { /// Unique ID of the solution (per driver competition), to settle. - #[serde_as(as = "serde_with::DisplayFromStr")] + #[serde(deserialize_with = "deserialize_solution_id")] pub solution_id: u64, /// The last block number in which the solution TX can be included pub submission_deadline_latest_block: u64,