From 748674fe7ac371602445ba184185ab502c59cc07 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Sun, 21 Aug 2022 08:30:49 +0000 Subject: [PATCH] forgot to git add this --- web3_proxy/src/frontend/axum_ext.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 web3_proxy/src/frontend/axum_ext.rs diff --git a/web3_proxy/src/frontend/axum_ext.rs b/web3_proxy/src/frontend/axum_ext.rs new file mode 100644 index 00000000..c4aa1a38 --- /dev/null +++ b/web3_proxy/src/frontend/axum_ext.rs @@ -0,0 +1,18 @@ +use axum::{extract::Query, routing::get, Router}; +use serde::{de, Deserialize, Deserializer}; +use std::{fmt, str::FromStr}; + +/// Serde deserialization decorator to map empty Strings to None, +/// https://github.com/tokio-rs/axum/blob/1fe45583626a4c9c890cc01131d38c57f8728686/examples/query-params-with-empty-strings/src/main.rs +pub fn empty_string_as_none<'de, D, T>(de: D) -> Result, D::Error> +where + D: Deserializer<'de>, + T: FromStr, + T::Err: fmt::Display, +{ + let opt = Option::::deserialize(de)?; + match opt.as_deref() { + None | Some("") => Ok(None), + Some(s) => FromStr::from_str(s).map_err(de::Error::custom).map(Some), + } +}