//! 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(succ: &bool, serializer: S) -> Result 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 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\"", )), } }