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(())
      }
   }))
  }
}

Methods

impl<T> TrivialEnumVisitor<T> where T: Deserialize

fn new<F>(parser: F) -> Self where F: Fn(&str) -> Result<T, ()> + 'static

Trait Implementations

impl<T> Visitor for TrivialEnumVisitor<T> where T: Deserialize

type Value = T

fn visit_str<E>(&mut self, v: &str) -> Result<T, E> where E: DeserializationError

fn visit_string<E>(&mut self, v: String) -> Result<T, E> where E: DeserializationError

fn visit_bytes<E>(&mut self, v: &[u8]) -> Result<T, E> where E: DeserializationError

fn visit_byte_buf<E>(&mut self, v: Vec<u8>) -> Result<T, E> where E: DeserializationError

fn visit_bool<E>(&mut self, _v: bool) -> Result<Self::Value, E> where E: Error

fn visit_isize<E>(&mut self, v: isize) -> Result<Self::Value, E> where E: Error

fn visit_i8<E>(&mut self, v: i8) -> Result<Self::Value, E> where E: Error

fn visit_i16<E>(&mut self, v: i16) -> Result<Self::Value, E> where E: Error

fn visit_i32<E>(&mut self, v: i32) -> Result<Self::Value, E> where E: Error

fn visit_i64<E>(&mut self, _v: i64) -> Result<Self::Value, E> where E: Error

fn visit_usize<E>(&mut self, v: usize) -> Result<Self::Value, E> where E: Error

fn visit_u8<E>(&mut self, v: u8) -> Result<Self::Value, E> where E: Error

fn visit_u16<E>(&mut self, v: u16) -> Result<Self::Value, E> where E: Error

fn visit_u32<E>(&mut self, v: u32) -> Result<Self::Value, E> where E: Error

fn visit_u64<E>(&mut self, _v: u64) -> Result<Self::Value, E> where E: Error

fn visit_f32<E>(&mut self, v: f32) -> Result<Self::Value, E> where E: Error

fn visit_f64<E>(&mut self, _v: f64) -> Result<Self::Value, E> where E: Error

fn visit_char<E>(&mut self, v: char) -> Result<Self::Value, E> where E: Error

fn visit_unit<E>(&mut self) -> Result<Self::Value, E> where E: Error

fn visit_unit_struct<E>(&mut self, _name: &'static str) -> Result<Self::Value, E> where E: Error

fn visit_none<E>(&mut self) -> Result<Self::Value, E> where E: Error

fn visit_some<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error> where D: Deserializer

fn visit_newtype_struct<D>(&mut self, _deserializer: &mut D) -> Result<Self::Value, D::Error> where D: Deserializer

fn visit_seq<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error> where V: SeqVisitor

fn visit_map<V>(&mut self, _visitor: V) -> Result<Self::Value, V::Error> where V: MapVisitor