simplify c_enum API

gui-branch
an 2019-03-18 12:22:10 -04:00
parent ee35332b23
commit b830718258
8 changed files with 151 additions and 151 deletions

View File

@ -6,7 +6,7 @@
/// The syntax is similar to the `bitflags` macro, but each value has the
/// syntax `value => enumeration`. `enum` is used instead of `struct`.
///
/// This will generate an `enum $t` as well as a function `$t::from_repr` which
/// This will generate an `enum $t` as well as a function `$t::try_from` which
/// will return `Result<$t, ReprError>`.
///
/// # Examples
@ -18,18 +18,18 @@
/// #[derive(Debug)]
/// enum MyEnum: u16
/// {
/// 0 => Zero,
/// 1 => One,
/// 2 => Two,
/// Zero = 0,
/// One = 1,
/// Two = 2
/// }
/// }
///
/// assert_eq!(MyEnum::from_repr(0), Ok(MyEnum::Zero));
/// assert_eq!(MyEnum::from_repr(1), Ok(MyEnum::One));
/// assert_eq!(MyEnum::from_repr(2), Ok(MyEnum::Two));
/// assert_eq!(MyEnum::from_repr(3), Err(ReprError::new(3)));
/// assert_eq!(MyEnum::from_repr(4), Err(ReprError::new(4)));
/// assert_eq!(MyEnum::from_repr(5), Err(ReprError::new(5)));
/// assert_eq!(MyEnum::try_from(0), Ok(MyEnum::Zero));
/// assert_eq!(MyEnum::try_from(1), Ok(MyEnum::One));
/// assert_eq!(MyEnum::try_from(2), Ok(MyEnum::Two));
/// assert_eq!(MyEnum::try_from(3), Err(ReprError::new(3)));
/// assert_eq!(MyEnum::try_from(4), Err(ReprError::new(4)));
/// assert_eq!(MyEnum::try_from(5), Err(ReprError::new(5)));
/// ```
#[macro_export]
macro_rules! c_enum
@ -38,12 +38,12 @@ macro_rules! c_enum
$(#[$outer:meta])*
$vi:vis enum $t:ident: $ti:ident
{
$($va:expr => $en:ident,)+
$($en:ident = $va:expr),+ $(,)?
}
) => {
$(#[$outer])*
#[repr($ti)]
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd)]
#[repr($ti)]
$vi enum $t
{
$($en = $va,)+
@ -52,7 +52,7 @@ macro_rules! c_enum
impl $t
{
/// Returns, if representable, the variant of `Self` from `n`.
$vi fn from_repr(n: $ti) -> Result<Self, ReprError>
$vi fn try_from(n: $ti) -> Result<Self, ReprError>
{
match n {
$($va => Ok($t::$en),)+
@ -72,26 +72,26 @@ mod test
#[derive(Debug)]
enum TestEnum: u16
{
0 => Zero,
1 => One,
2 => Two,
Zero = 0,
One = 1,
Two = 2,
}
}
#[test]
fn c_enum_should_be_ok()
{
assert_eq!(TestEnum::from_repr(0), Ok(TestEnum::Zero));
assert_eq!(TestEnum::from_repr(1), Ok(TestEnum::One));
assert_eq!(TestEnum::from_repr(2), Ok(TestEnum::Two));
assert_eq!(TestEnum::from_repr(3), Err(ReprError::new(3)));
assert_eq!(TestEnum::from_repr(4), Err(ReprError::new(4)));
assert_eq!(TestEnum::from_repr(5), Err(ReprError::new(5)));
assert_eq!(TestEnum::try_from(0), Ok(TestEnum::Zero));
assert_eq!(TestEnum::try_from(1), Ok(TestEnum::One));
assert_eq!(TestEnum::try_from(2), Ok(TestEnum::Two));
assert_eq!(TestEnum::try_from(3), Err(ReprError::new(3)));
assert_eq!(TestEnum::try_from(4), Err(ReprError::new(4)));
assert_eq!(TestEnum::try_from(5), Err(ReprError::new(5)));
}
#[test]
#[should_panic]
fn c_enum_should_error() {TestEnum::from_repr(3).unwrap();}
fn c_enum_should_error() {TestEnum::try_from(3).unwrap();}
}
// EOF

View File

@ -16,7 +16,7 @@ pub fn read_lightfunc(b: &[u8]) -> ResultS<LightFunc>
val_dta = Fixed[10];
}
let ftype = LightFuncType::from_repr(ftype)?;
let ftype = LightFuncType::try_from(ftype)?;
Ok(LightFunc{ftype, prd_nrm, prd_dta, val_nrm, val_dta})
}
@ -148,10 +148,10 @@ pub fn read_sids(b: &[u8]) -> ResultS<(Side, usize)>
}
let flags = flag_ok!(SideFlags, flags)?;
let xfer_pri = TransferMode::from_repr(xfer_pri)?;
let xfer_sec = TransferMode::from_repr(xfer_sec)?;
let xfer_tra = TransferMode::from_repr(xfer_tra)?;
let stype = SideType::from_repr(stype)?;
let xfer_pri = TransferMode::try_from(xfer_pri)?;
let xfer_sec = TransferMode::try_from(xfer_sec)?;
let xfer_tra = TransferMode::try_from(xfer_tra)?;
let stype = SideType::try_from(stype)?;
Ok((Side{stype, flags, tex_pri, tex_sec, tex_tra, paneltyp, paneldat,
xfer_pri, xfer_sec, xfer_tra, shade}, 64))
@ -183,8 +183,8 @@ fn read_poly_inter(b: &[u8]) -> ResultS<Polygon>
xfr_cei = u16[66];
}
let xfr_flr = TransferMode::from_repr(xfr_flr)?;
let xfr_cei = TransferMode::from_repr(xfr_cei)?;
let xfr_flr = TransferMode::try_from(xfr_flr)?;
let xfr_cei = TransferMode::try_from(xfr_cei)?;
Ok(Polygon{tex_flr, tex_cei, hei_flr, hei_cei, lit_flr, lit_cei, xfr_flr,
xfr_cei, ..Default::default()})
@ -246,7 +246,7 @@ pub fn read_lite(b: &[u8]) -> ResultS<(Light, usize)>
}
let flags = flag_ok!(LightFlags, flags)?;
let ltype = LightType::from_repr(ltype)?;
let ltype = LightType::try_from(ltype)?;
Ok((Light{ltype, flags, phase, act_pri, act_sec, act_mid, ina_pri, ina_sec,
ina_mid, tag}, 100))
@ -390,8 +390,8 @@ pub fn read_medi(b: &[u8]) -> ResultS<(Media, usize)>
xfer = u16[26];
}
let mtype = MediaType::from_repr(mtype)?;
let xfer = TransferMode::from_repr(xfer)?;
let mtype = MediaType::try_from(mtype)?;
let xfer = TransferMode::try_from(xfer)?;
let flr_obs = flags != 0;
Ok((Media{mtype, flr_obs, control, dir, mag, hei_lo, hei_hi, orig, hei_nrm,
@ -908,11 +908,11 @@ c_enum! {
#[derive(Debug)]
pub enum SideType: u16
{
0 => Full,
1 => High,
2 => Low,
3 => Composite,
4 => Split,
Full = 0,
High = 1,
Low = 2,
Composite = 3,
Split = 4,
}
}
@ -922,12 +922,12 @@ c_enum! {
#[derive(Debug)]
pub enum LightFuncType: u16
{
0 => Constant,
1 => Linear,
2 => Smooth,
3 => Flicker,
4 => Random,
5 => Fluorescent,
Constant = 0,
Linear = 1,
Smooth = 2,
Flicker = 3,
Random = 4,
Fluorescent = 5,
}
}
@ -937,9 +937,9 @@ c_enum! {
#[derive(Debug)]
pub enum LightType: u16
{
0 => Normal,
1 => Strobe,
2 => Media,
Normal = 0,
Strobe = 1,
Media = 2,
}
}
@ -949,10 +949,10 @@ c_enum! {
#[derive(Debug)]
pub enum MediaType: u16
{
0 => Water,
1 => Lava,
2 => Goo,
3 => Sewage,
Water = 0,
Lava = 1,
Goo = 2,
Sewage = 3,
}
}

View File

@ -92,7 +92,7 @@ pub fn read_wppx(b: &[u8]) -> ResultS<(Weapon, usize)>
trig_sec = read_trigger[98..134];
}
let typ_weapon = WeaponType::from_repr(typ_weapon)?;
let typ_weapon = WeaponType::try_from(typ_weapon)?;
let flags = flag_ok!(WeaponFlags, flags)?;
Ok((Weapon{amp_bob, amp_horz, collection, flags, frm_charge, frm_charged,
@ -239,7 +239,7 @@ fn read_trigger(b: &[u8]) -> ResultS<Trigger>
burst = u16[34];
}
let typ_casing = CasingType::from_repr(typ_casing)?;
let typ_casing = CasingType::try_from(typ_casing)?;
Ok(Trigger{burst, dx, dz, magazine, recoil, snd_casing, snd_charge,
snd_charged, snd_click, snd_fire, snd_reload, theta, tic_charge,
@ -258,7 +258,7 @@ fn read_damage(b: &[u8]) -> ResultS<Damage>
scale = Fixed[8];
}
let dtype = DamageType::from_repr(dtype)?;
let dtype = DamageType::try_from(dtype)?;
let alien = flags != 0;
Ok(Damage{dtype, alien, dmg_base, dmg_rand, scale})
@ -653,12 +653,12 @@ c_enum! {
#[derive(Debug)]
pub enum CasingType: u16
{
0 => Rifle,
1 => Pistol,
2 => PistolLeft,
3 => PistolRight,
4 => SMG,
0xFFFF => None,
Rifle = 0,
Pistol = 1,
PistolLeft = 2,
PistolRight = 3,
SMG = 4,
None = 0xFFFF,
}
}
@ -668,11 +668,11 @@ c_enum! {
#[derive(Debug)]
pub enum WeaponType: u16
{
0 => Melee,
1 => Normal,
2 => DualFunc,
3 => DualPistol,
4 => Multipurpose,
Melee = 0,
Normal = 1,
DualFunc = 2,
DualPistol = 3,
Multipurpose = 4,
}
}
@ -682,31 +682,31 @@ c_enum! {
#[derive(Debug)]
pub enum DamageType: u16
{
0 => Explosion,
1 => ElectricalStaff,
2 => Projectile,
3 => Absorbed,
4 => Flame,
5 => HoundClaws,
6 => AlienProjectile,
7 => HulkSlap,
8 => CompilerBolt,
9 => FusionBolt,
10 => HunterBolt,
11 => Fist,
12 => Teleporter,
13 => Defender,
14 => YetiClaws,
15 => YetiProjectile,
16 => Crushing,
17 => Lava,
18 => Suffocation,
19 => Goo,
20 => EnergyDrain,
21 => OxygenDrain,
22 => HummerBolt,
23 => ShotgunProjectile,
0xFFFF => None,
Explosion = 0,
ElectricalStaff = 1,
Projectile = 2,
Absorbed = 3,
Flame = 4,
HoundClaws = 5,
AlienProjectile = 6,
HulkSlap = 7,
CompilerBolt = 8,
FusionBolt = 9,
HunterBolt = 10,
Fist = 11,
Teleporter = 12,
Defender = 13,
YetiClaws = 14,
YetiProjectile = 15,
Crushing = 16,
Lava = 17,
Suffocation = 18,
Goo = 19,
EnergyDrain = 20,
OxygenDrain = 21,
HummerBolt = 22,
ShotgunProjectile = 23,
None = 0xFFFF,
}
}

View File

@ -20,8 +20,8 @@ fn read_pm_header<'a>(b: &'a [u8],
depth = u16[28];
}
let pack_t = PackType::from_repr(pack_t)?;
let depth = Depth::from_repr(depth)?;
let pack_t = PackType::try_from(pack_t)?;
let depth = Depth::try_from(depth)?;
if pt_fl & 0x8000 == 0 {
bail!("PICT1 not supported");
@ -481,23 +481,23 @@ struct Header
c_enum! {
enum Depth: u16
{
1 => Bits1,
2 => Bits2,
4 => Bits4,
8 => Bits8,
16 => Bits16,
32 => Bits32,
Bits1 = 1,
Bits2 = 2,
Bits4 = 4,
Bits8 = 8,
Bits16 = 16,
Bits32 = 32,
}
}
c_enum! {
enum PackType: u16
{
0 => Default,
1 => None,
2 => NoPad,
3 => Rle16,
4 => Rle32,
Default = 0,
None = 1,
NoPad = 2,
Rle16 = 3,
Rle32 = 4,
}
}

View File

@ -158,8 +158,8 @@ pub fn read_sequence(b: &[u8]) -> ResultS<Sequence>
}
let name = mac_roman_conv(ok!(pascal_str(name), "bad string")?);
let xfer = TransferMode::from_repr(xfer)?;
let v_type = ViewType::from_repr(v_type)?;
let xfer = TransferMode::try_from(xfer)?;
let v_type = ViewType::try_from(v_type)?;
Ok(Sequence{name, v_type, frames, ticks, key, xfer, xfer_pd, snd_beg,
snd_key, snd_end, loop_f})
@ -183,7 +183,7 @@ pub fn read_collection(b: &[u8]) -> ResultS<Collection>
bmp_ofs = u32[28] usize;
}
let cl_type = CollectionType::from_repr(cl_type)?;
let cl_type = CollectionType::try_from(cl_type)?;
if version != 3 {
bail!("invalid collection definition");
@ -478,11 +478,11 @@ c_enum! {
#[derive(Debug)]
pub enum CollectionType: u16
{
0 => Unused,
1 => Wall,
2 => Object,
3 => Interface,
4 => Scenery,
Unused = 0,
Wall = 1,
Object = 2,
Interface = 3,
Scenery = 4,
}
}
@ -492,15 +492,15 @@ c_enum! {
#[derive(Debug)]
pub enum ViewType: u16
{
1 => Anim,
2 => Anim8from2,
3 => Anim4from3,
4 => Anim4,
5 => Anim8from5,
8 => Anim8,
9 => Anim5from3,
10 => Still,
11 => Anim5,
Anim = 1,
Anim8from2 = 2,
Anim4from3 = 3,
Anim4 = 4,
Anim8from5 = 5,
Anim8 = 8,
Anim5from3 = 9,
Still = 10,
Anim5 = 11,
}
}

View File

@ -60,7 +60,7 @@ pub fn read_sound_def(b: &[u8]) -> ResultS<Option<(Vec<usize>, u16, SoundDef)>>
}
let flags = flag_ok!(SoundFlags, flags)?;
let volume = Volume::from_repr(volume)?;
let volume = Volume::try_from(volume)?;
if index == u16::max_value() {
return Ok(None);
@ -177,9 +177,9 @@ c_enum! {
#[derive(Debug)]
pub enum Volume: u16
{
0 => Quiet,
1 => Normal,
2 => Loud,
Quiet = 0,
Normal = 1,
Loud = 2,
}
}

View File

@ -112,7 +112,7 @@ pub fn read_wad(b: &[u8]) -> ResultS<Wad>
}
let old_dat = ver_dat == 0;
let old_wad = match Ver::from_repr(ver_wad)? {
let old_wad = match Ver::try_from(ver_wad)? {
Ver::Base => true,
_ => false,
};
@ -217,10 +217,10 @@ c_enum! {
#[derive(Debug)]
enum Ver: u16
{
0 => Base,
1 => Dir,
2 => Over,
4 => Inf,
Base = 0,
Dir = 1,
Over = 2,
Inf = 4,
}
}

View File

@ -13,28 +13,28 @@ c_enum! {
#[derive(Debug)]
pub enum TransferMode: u16
{
0 => Normal,
1 => FadeBlack,
2 => Invisibility,
3 => Invisibility2,
4 => Pulsate,
5 => Wobble,
6 => Wobble2,
7 => Static,
8 => Static2,
9 => Sky,
10 => Smear,
11 => StaticFade,
12 => StaticPulse,
13 => FoldIn,
14 => FoldOut,
15 => SlideHorz,
16 => SlideHorz2,
17 => SlideVert,
18 => SlideVert2,
19 => Wander,
20 => Wander2,
21 => BigSky,
Normal = 0,
FadeBlack = 1,
Invisibility = 2,
Invisibility2 = 3,
Pulsate = 4,
Wobble = 5,
Wobble2 = 6,
Static = 7,
Static2 = 8,
Sky = 9,
Smear = 10,
StaticFade = 11,
StaticPulse = 12,
FoldIn = 13,
FoldOut = 14,
SlideHorz = 15,
SlideHorz2 = 16,
SlideVert = 17,
SlideVert2 = 18,
Wander = 19,
Wander2 = 20,
BigSky = 21,
}
}