* all * all * optimize deserialize * all * all * fix issue * fix compile * fix compile * fix compile * fix compile * fix compile * fix compile * fix compile * fix compile * fix compile * fix compile
68 lines
1.6 KiB
Rust
68 lines
1.6 KiB
Rust
/*
|
|
* Author: Ruben Fiszel
|
|
* Copyright: Windmill Labs, Inc 2022
|
|
* This file and its contents are licensed under the AGPLv3 License.
|
|
* Please see the included NOTICE for copyright information and
|
|
* LICENSE-AGPL for a copy of the license.
|
|
*/
|
|
|
|
//! helpers for serde + serde derive attributes
|
|
|
|
use crate::utils::rd_string;
|
|
use serde::{Deserialize, Deserializer};
|
|
use serde_json::value::RawValue;
|
|
use std::{fmt::Display, str::FromStr};
|
|
|
|
pub fn default_true() -> bool {
|
|
true
|
|
}
|
|
|
|
pub fn default_false() -> bool {
|
|
false
|
|
}
|
|
|
|
pub fn default_null() -> Box<RawValue> {
|
|
RawValue::from_string("null".to_string()).unwrap()
|
|
}
|
|
|
|
pub fn default_empty_string() -> String {
|
|
String::new()
|
|
}
|
|
|
|
pub fn default_id() -> String {
|
|
rd_string(6)
|
|
}
|
|
|
|
pub fn is_default<T: Default + std::cmp::PartialEq>(t: &T) -> bool {
|
|
&T::default() == t
|
|
}
|
|
|
|
pub fn maybe_number_opt<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
T: FromStr + serde::Deserialize<'de>,
|
|
<T as FromStr>::Err: Display,
|
|
{
|
|
#[derive(Deserialize)]
|
|
#[serde(untagged)]
|
|
enum NumericOrNull<'a, T> {
|
|
String(String),
|
|
Str(&'a str),
|
|
RawT(T),
|
|
Null,
|
|
}
|
|
|
|
match NumericOrNull::<T>::deserialize(deserializer)? {
|
|
NumericOrNull::String(s) => match s.as_str() {
|
|
"" => Ok(None),
|
|
_ => T::from_str(&s).map(Some).map_err(serde::de::Error::custom),
|
|
},
|
|
NumericOrNull::Str(s) => match s {
|
|
"" => Ok(None),
|
|
_ => T::from_str(s).map(Some).map_err(serde::de::Error::custom),
|
|
},
|
|
NumericOrNull::RawT(i) => Ok(Some(i)),
|
|
NumericOrNull::Null => Ok(None),
|
|
}
|
|
}
|