Derive Macro EnumRepr

Source
#[derive(EnumRepr)]
{
    // Attributes available to this derive:
    #[enum_repr]
}
Expand description

Automatically implements [std::convert::From] and [std::convert::Into] for repr on enums.

§Where to use:

  • Managing Type
  • Easy to understand constants

§Note:

  • require #[repr(u8)] or #[repr(u16)] or …
  • default is require explicit

§Enum Attribute:

  • serde: automatically implements [serde::Serialize] and [serde::Deserialize]
  • implicit: make it less explicit

§Variant Attribute:

( Currency no. )

§Examples

§Normal

use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
pub enum Test {
    Zero = 0,
    Three = 3,
    Four = 4,
}

assert_eq!(0u8, Test::Zero.into());
assert_eq!(3u8, Test::Three.into());
assert_eq!(4u8, Test::Four.into());

assert_eq!(0u8.try_into(), Ok(Test::Zero));
assert_eq!(3u8.try_into(), Ok(Test::Three));
assert_eq!(4u8.try_into(), Ok(Test::Four));

assert_eq!(TryInto::<Test>::try_into(1u8).unwrap_err(), String::from("invalid Test: 1"));

§serde

use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
#[enum_repr(serde)]
pub enum Test {
    Zero = 0,
    Three = 3,
    Four = 4,
}


assert_eq!(serde_json::to_string(&Test::Zero).unwrap(), "0");
assert_eq!(serde_json::to_string(&Test::Three).unwrap(), "3");
assert_eq!(serde_json::to_string(&Test::Four).unwrap(), "4");

assert_eq!(serde_json::from_str::<Test>("0").unwrap(), Test::Zero);
assert_eq!(serde_json::from_str::<Test>("3").unwrap(), Test::Three);
assert_eq!(serde_json::from_str::<Test>("4").unwrap(), Test::Four);

assert_eq!(serde_json::from_str::<Test>("1").unwrap_err().to_string(), String::from("invalid Test: 1"));

§implicit

use more_convert::EnumRepr;
#[derive(EnumRepr, Clone, Copy, Debug, PartialEq)]
#[repr(u8)]
#[enum_repr(implicit)]
pub enum Test {
    Zero,
    Three = 3,
    Four,
}

assert_eq!(0u8, Test::Zero.into());
assert_eq!(3u8, Test::Three.into());
assert_eq!(4u8, Test::Four.into());

assert_eq!(0u8.try_into(), Ok(Test::Zero));
assert_eq!(3u8.try_into(), Ok(Test::Three));
assert_eq!(4u8.try_into(), Ok(Test::Four));

assert_eq!(TryInto::<Test>::try_into(1u8).unwrap_err(), String::from("invalid Test: 1"));