Skip to content

Instantly share code, notes, and snippets.

@bjorn3

bjorn3/lib.rs Secret

Last active February 18, 2017 10:39
extern crate serde;
#[macro_use]
extern crate serde_derive;
#[test]
fn main() {
println!("Hello, world!");
}
use std::fmt;
use std::collections::HashMap;
use std::sync::Mutex;
use serde::de::{Deserialize, Deserializer, Visitor};
/// World
#[derive(Debug, Hash, Eq, PartialEq, Copy, Clone, Serialize)]
pub enum World {
/// Fire Peaks
Fire = 3,
/// Burning Sands
Sand = 1,
/// Green
Grass = 0,
/// EW
Ice = 2,
/// Special Event
SpecialEvent = 4,
}
impl Deserialize for World{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer{
struct WorldVisitor;
impl Visitor for WorldVisitor{
type Value = World;
fn visit_u8<E>(self, v: u8) -> Result<Self::Value, E>{
match v {
0 => World::Grass,
1 => World::Sand,
2 => World::Ice,
3 => World::Fire,
4 => World::SpecialEvent,
_ => D::Error::custom(arguments!("Unrecognized world number {}", v)),
}
}
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result{
write!(f, "an integer between 0 and 5")
}
}
deserializer.deserialize_u8(WorldVisitor)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment