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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

/// Creates an enum with various traits. 
/// The first key-value pair is the default used if any conversion would fail.
#[macro_export]
macro_rules! extended_enum_default {
    ( $name:ident, $ty:ty, $var_def:ident => $val_def:expr,
      $( $var:ident => $val:expr ),+ $(,)* ) => (
        
        #[derive(Clone,Debug,Eq,PartialEq)]
        pub enum $name {
            $var_def,
            $($var,)*
        }

        impl From<$ty> for $name {
            fn from(v: $ty) -> Self {
                match v {
                    $val_def => $name::$var_def,
                    $( $val => $name::$var,)*
                    _ => $name::$var_def,
                }
            }
        }

        impl From<$name> for $ty {
            fn from(v: $name) -> Self {
                match v {
                    $name::$var_def => $val_def,
                    $( $name::$var => $val, )*
                }
            }
        }

        impl ConvertFrom<$ty> for $name {
            fn convert_from(v: $ty) -> Option<Self> {
                match v {
                    $val_def => Some($name::$var_def),
                    $( $val => Some($name::$var),)*
                    _ => None,
                }
            }
        }

        impl PartialEq<$name> for $ty {
            fn eq(&self, other: &$name) -> bool {
                match *other {
                    $name::$var_def => *self == $val_def,
                    $( $name::$var => *self == $val, )*
                }
            }

            fn ne(&self, other: &$name) -> bool {
                match *other {
                    $name::$var_def => *self != $val_def,
                    $( $name::$var => *self != $val, )*
                }
            }
        }
    );
}

/// Creates an enum with various traits. 
/// The first key-value pair is the default used if any conversion would fail.
#[macro_export]
macro_rules! extended_enum {
    ( $name:ident, $ty:ty, $( $var:ident => $val:expr ),+ $(,)* ) => (
        
        #[derive(Clone,Debug,Eq,PartialEq)]
        pub enum $name {
            $($var,)*
        }

        impl From<$ty> for $name {
            fn from(v: $ty) -> Self {
                match v {
                    $( $val => $name::$var,)*
                    _ => panic!("Bad Value"),
                }
            }
        }

        impl From<$name> for $ty {
            fn from(v: $name) -> Self {
                match v {
                    $( $name::$var => $val, )*
                }
            }
        }

        impl ConvertFrom<$ty> for $name {
            fn convert_from(v: $ty) -> Option<Self> {
                match v {
                    $( $val => Some($name::$var),)*
                    _ => None,
                }
            }
        }

        impl PartialEq<$name> for $ty {
            fn eq(&self, other: &$name) -> bool {
                match *other {
                    $( $name::$var => *self == $val, )*
                }
            }

            fn ne(&self, other: &$name) -> bool {
                match *other {
                    $( $name::$var => *self != $val, )*
                }
            }
        }
    );
}