1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

use serde_am::*;
use error::Error;

/// All plane types present within airmash.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub enum PlaneType {
    Predator,
    Tornado,
    Mohawk,
    Goliath,
    Prowler
}

const PREDATOR: u8 = 1;
const GOLIATH: u8 = 2;
const MOHAWK: u8 = 3;
const TORNADO: u8 = 4;
const PROWLER: u8 = 5;

impl PlaneType {
    /// Get the associated numerical value
    pub fn to_u8(self) -> u8 {
        match self {
            PlaneType::Predator => PREDATOR,
            PlaneType::Tornado =>  TORNADO,
            PlaneType::Mohawk =>   MOHAWK,
            PlaneType::Goliath =>  GOLIATH,
            PlaneType::Prowler =>  PROWLER
        }
    }

    /// Get the plane associated with a numerical
    /// value, or `None` otherwise.
    pub fn from_u8(v: u8) -> Option<Self> {
        Some(match v {
            PREDATOR => PlaneType::Predator,
            TORNADO  => PlaneType::Tornado,
            MOHAWK   => PlaneType::Mohawk,
            GOLIATH  => PlaneType::Goliath,
            PROWLER  => PlaneType::Prowler,
            _ => return None
        })
    }
}

impl Serialize for PlaneType {
    fn serialize(&self, ser: &mut Serializer) -> Result<()> {
        self.to_u8().serialize(ser)
    }
}
impl<'de> Deserialize<'de> for PlaneType {
    fn deserialize(de: &mut Deserializer<'de>) -> Result<Self> {
        let ival = de.deserialize_u8()?;
        match Self::from_u8(ival) {
            Some(v) => Ok(v),
            None => Err(Error::InvalidPlaneType(ival))
        }
    }
}