You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
989 B

//! Convert serialized "1"/"" to true/false.
//! This behaviour is extremely odd, and seemingly unique to Lunanode, so I had to use a custom
//! serialize/deserialize pair of functions.
use serde::{
de::{
self,
Unexpected,
},
Serializer,
Deserializer,
Deserialize,
};
/// Seiralize bool into "1"/"", just like the LunaNode API does.
pub fn serialize<S>(succ: &bool, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer {
match succ {
true => serializer.serialize_str("1"),
false => serializer.serialize_str(""),
}
}
/// Deserialize bool from String with custom value mapping "1" => true, "" => false
pub fn deserialize<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
match String::deserialize(deserializer)?.as_ref() {
"1" => Ok(true),
"" => Ok(false),
other => Err(de::Error::invalid_value(
Unexpected::Str(other),
&"\"1\" or \"\"",
)),
}
}