Struct foxbox_taxonomy::util::TrivialEnumVisitor
[−]
[src]
pub struct TrivialEnumVisitor<T> where T: Deserialize {
// some fields omitted
}
By default, the (de)serialization of trivial enums by Serde is surprising, e.g.
in JSON, enum Foo {A, B, C}
will produce {"\"A\": []"}
for A
, where "\"A\""
would be expected.
Implementing serialization is very simple, but deserialization is much more annoying. This struct lets us implement simply the deserialization to a predictable and well-specified list of strings.
Example
extern crate serde; use serde::de::{Deserialize, Deserializer}; extern crate foxbox_taxonomy; use foxbox_taxonomy::util::TrivialEnumVisitor; enum Foo { A, B, C } impl Deserialize for Foo { fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error> where D: Deserializer { deserializer.visit_string(TrivialEnumVisitor::new(|source| { match source { "A" => Ok(Foo::A), "B" => Ok(Foo::B), "C" => Ok(Foo::C), _ => Err(()) } })) } }