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.

40 lines
1.1 KiB

//! A module to convert "yes"/"no" from a string to a bool.
//! This is exceptionally strange behaviour from an API. Just use `bool`.
//! But anyway, since Lunanode does indeed use this system, a custom module to serialize and
//! deserialize their "yes"/"no" responses was necessary.
use serde::{
de::{
self,
Unexpected,
},
Serializer,
Deserializer,
Deserialize,
};
/// Seiralize bool into "yes"/"no", 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("yes"),
false => serializer.serialize_str("no"),
}
}
/// Deserialize bool from String with custom value mapping "yes" => true, "no" => false
pub fn deserialize<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
match String::deserialize(deserializer)?.as_ref() {
"yes" => Ok(true),
"no" => Ok(false),
other => Err(de::Error::invalid_value(
Unexpected::Str(other),
&"\"yes\" or \"no\"",
)),
}
}