noosphere_core/api/
data.rs1use std::str::FromStr;
2
3use anyhow::Result;
4
5use serde::{Deserialize, Deserializer};
6
7pub trait AsQuery {
10 fn as_query(&self) -> Result<Option<String>>;
12}
13
14impl AsQuery for () {
15 fn as_query(&self) -> Result<Option<String>> {
16 Ok(None)
17 }
18}
19
20pub(crate) fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
23where
24 D: Deserializer<'de>,
25 T: FromStr,
26 T::Err: std::fmt::Display,
27{
28 let opt = Option::<String>::deserialize(de)?;
29 match opt.as_deref() {
30 None | Some("") => Ok(None),
31 Some(s) => FromStr::from_str(s)
32 .map_err(serde::de::Error::custom)
33 .map(Some),
34 }
35}