From 6bc4ca36f47079a950e3541684a29fa555d4b1b1 Mon Sep 17 00:00:00 2001 From: Marrub Date: Mon, 1 Apr 2019 09:05:06 -0400 Subject: [PATCH] lint and format --- source/durandal/bin.rs | 29 ++++++------ source/durandal/err.rs | 6 ++- source/durandal/ffi.rs | 28 ++++------- source/durandal/fixed.rs | 4 +- source/durandal/image.rs | 7 +-- source/marathon/defl.rs | 80 ++++++++++++++++++-------------- source/marathon/machdr.rs | 6 ++- source/marathon/map/bonk.rs | 21 ++++++--- source/marathon/map/epnt.rs | 4 +- source/marathon/map/lite.rs | 2 +- source/marathon/map/medi.rs | 24 +++++++--- source/marathon/map/note.rs | 2 +- source/marathon/map/objs.rs | 3 +- source/marathon/map/plac.rs | 18 +++++-- source/marathon/map/pnts.rs | 5 +- source/marathon/map/poly.rs | 2 +- source/marathon/map/sids.rs | 2 +- source/marathon/map/term.rs | 11 +++-- source/marathon/phy/attk.rs | 4 +- source/marathon/phy/damg.rs | 19 +++++--- source/marathon/phy/fxpx.rs | 2 +- source/marathon/phy/mnpx.rs | 4 +- source/marathon/phy/prpx.rs | 4 +- source/marathon/phy/pxpx.rs | 2 +- source/marathon/phy/trig.rs | 2 +- source/marathon/phy/wppx.rs | 2 +- source/marathon/shp.rs | 6 +-- source/marathon/text.rs | 35 ++++++-------- source/marathon/wad.rs | 3 +- source/rozinante/editor.rs | 19 ++------ source/rozinante/editor/block.rs | 5 +- source/rozinante/editor/state.rs | 5 +- source/tycho/interfaces.rs | 12 +++-- tests/map.rs | 3 +- 34 files changed, 211 insertions(+), 170 deletions(-) diff --git a/source/durandal/bin.rs b/source/durandal/bin.rs index 619bd3e..3fd4319 100644 --- a/source/durandal/bin.rs +++ b/source/durandal/bin.rs @@ -115,8 +115,8 @@ macro_rules! rd_impl { /// evaluated many times, so be careful when specifying it. /// - `SIZE` is an expression specifying the last index that should be used by /// this macro in `BUFFER`. -/// - `START` is an expression specifying the index to start at in `BUFFER`. -/// All indices and sizes will have this added to them. +/// - `START` is an expression specifying the index to start at in `BUFFER`. All +/// indices and sizes will have this added to them. /// /// Following that is a block with the syntax `data { ... }`. All lines within /// this block have the syntax `let NAME = TYPE[INDEX] OPTS;` where: @@ -139,8 +139,8 @@ macro_rules! rd_impl { /// `fixed::Fixed::from_bits`, resulting in a `fixed::Fixed` object. /// - `Unit`: same as `u16`, but the result is passed to /// `fixed::Unit::from_bits`, resulting in a `fixed::Unit` object. -/// - `OptU16`: same as `u16`, but the result is passed to -/// `OptU16::from`, resulting in an `OptU16` object. +/// - `OptU16`: same as `u16`, but the result is passed to `OptU16::from`, +/// resulting in an `OptU16` object. /// - The name of a function, which is passed the index range as its only /// argument. The function's result has the `?` operator applied to it, /// unless `OPTS` is `no_try`. @@ -175,8 +175,8 @@ macro_rules! rd_impl { /// } /// /// assert_eq!(four, 4_u16); -/// assert_eq!(two, 2_u32); -/// assert_eq!(six, 6_u8); +/// assert_eq!(two, 2_u32); +/// assert_eq!(six, 6_u8); /// assert_eq!(byte, &[2, 0, 0, 0]); /// # Ok(()) /// # } @@ -226,7 +226,7 @@ pub const fn usize_from_u32(n: u32) -> usize {n as usize} /// # Examples /// /// ``` -/// use maraiah::durandal::bin::{Ident, ident}; +/// use maraiah::durandal::bin::{ident, Ident}; /// /// assert_eq!(ident(b"POLY"), Ident([b'P', b'O', b'L', b'Y'])); /// ``` @@ -316,7 +316,8 @@ pub fn i16b(b: &[u8]) -> i16 {i16::from_be_bytes([b[0], b[1]])} /// # Examples /// /// ``` -/// use maraiah::durandal::{err::*, bin::{rd_array, u16b}}; +/// use maraiah::durandal::{bin::{rd_array, u16b}, +/// err::*}; /// /// fn read_a_u16(b: &[u8]) -> ResultS<(u16, usize)> {Ok((u16b(b), 2))} /// @@ -523,7 +524,7 @@ impl<'a> PartialEq<[u8; 4]> for &'a Ident impl<'a> PartialEq<&'a [u8; 4]> for Ident { #[inline] - fn eq(&self, o: & &'a [u8; 4]) -> bool {PartialEq::eq(self, *o)} + fn eq(&self, o: &&'a [u8; 4]) -> bool {PartialEq::eq(self, *o)} } /// A four-character-code identifier. @@ -533,11 +534,11 @@ impl<'a> PartialEq<&'a [u8; 4]> for Ident /// ``` /// use maraiah::durandal::bin::Ident; /// -/// assert_eq!( Ident(*b"POLY").0, *b"POLY"); -/// assert_eq!( Ident(*b"POLY"), *b"POLY"); -/// assert_eq!( Ident(*b"POLY"), b"POLY"); -/// assert_eq!(&Ident(*b"POLY"), *b"POLY"); -/// assert_eq!(&Ident(*b"POLY"), b"POLY"); +/// assert_eq!(Ident(*b"POLY").0, *b"POLY"); +/// assert_eq!(Ident(*b"POLY"), *b"POLY"); +/// assert_eq!(Ident(*b"POLY"), b"POLY"); +/// assert_eq!(&Ident(*b"POLY"), *b"POLY"); +/// assert_eq!(&Ident(*b"POLY"), b"POLY"); /// ``` #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] #[cfg_attr(feature = "serde_obj", derive(serde::Serialize, serde::Deserialize))] diff --git a/source/durandal/err.rs b/source/durandal/err.rs index 6655063..1781b3b 100644 --- a/source/durandal/err.rs +++ b/source/durandal/err.rs @@ -35,7 +35,8 @@ macro_rules! bail { /// ``` /// use maraiah::durandal::err::err_msg; /// -/// assert_eq!(format!("{}", err_msg("oh no not things")), "oh no not things"); +/// assert_eq!(format!("{}", err_msg("oh no not things")), +/// "oh no not things"); /// ``` pub fn err_msg(msg: &'static str) -> Error {ErrMsg(msg).into()} @@ -46,7 +47,8 @@ pub fn err_msg(msg: &'static str) -> Error {ErrMsg(msg).into()} /// ``` /// use maraiah::durandal::err::repr_error; /// -/// assert_eq!(format!("{}", repr_error(77)), "representation error (got 77)"); +/// assert_eq!(format!("{}", repr_error(77)), +/// "representation error (got 77)"); /// ``` pub fn repr_error>(n: T) -> Error {ReprError::new(n).into()} diff --git a/source/durandal/ffi.rs b/source/durandal/ffi.rs index 0bf316c..ab1f8fc 100644 --- a/source/durandal/ffi.rs +++ b/source/durandal/ffi.rs @@ -1,25 +1,23 @@ //! Foreign function interface utilities. use crate::durandal::err::*; -pub use std::{ffi::*, os::raw::*, ptr::{null, null_mut}}; +pub use std::{ffi::*, + os::raw::*, + ptr::{null, null_mut}}; /// Creates a C string from a literal. #[macro_export] macro_rules! c_str { - ($s:expr) => {concat!($s, "\0").as_ptr() as $crate::durandal::ffi::NT}; + ($s:expr) => { + concat!($s, "\0").as_ptr() as $crate::durandal::ffi::NT + }; } #[inline] -pub const fn null_void() -> *const c_void -{ - null() -} +pub const fn null_void() -> *const c_void {null()} #[inline] -pub const fn null_mut_void() -> *mut c_void -{ - null_mut() -} +pub const fn null_mut_void() -> *mut c_void {null_mut()} impl CStringVec { @@ -54,17 +52,11 @@ impl CStringVec /// Returns the FFI pointer. #[inline] - pub fn as_ptr(&self) -> *const NT - { - self.cv.as_ptr() - } + pub fn as_ptr(&self) -> *const NT {self.cv.as_ptr()} /// Returns the FFI pointer mutably. #[inline] - pub fn as_mut_ptr(&mut self) -> *mut NT - { - self.cv.as_mut_ptr() - } + pub fn as_mut_ptr(&mut self) -> *mut NT {self.cv.as_mut_ptr()} } impl Default for CStringVec diff --git a/source/durandal/fixed.rs b/source/durandal/fixed.rs index 0a20d77..e7d7b53 100644 --- a/source/durandal/fixed.rs +++ b/source/durandal/fixed.rs @@ -1,7 +1,9 @@ //! Fixed point numbers. #![allow(clippy::use_self)] +#![allow(clippy::cast_lossless)] -use std::{fmt::{self, Write}, ops::*}; +use std::{fmt::{self, Write}, + ops::*}; macro_rules! fixed_ref_unop { (impl $imp:ident, $method:ident for $t:ty) => { diff --git a/source/durandal/image.rs b/source/durandal/image.rs index 637ea50..dc29b6f 100644 --- a/source/durandal/image.rs +++ b/source/durandal/image.rs @@ -5,7 +5,7 @@ /// # Examples /// /// ``` -/// use maraiah::durandal::image::{Color8, r5g5b5_to_rgb8}; +/// use maraiah::durandal::image::{r5g5b5_to_rgb8, Color8}; /// /// assert_eq!(r5g5b5_to_rgb8(0x2345), Color8::new(64, 208, 40)); /// ``` @@ -22,9 +22,10 @@ pub fn r5g5b5_to_rgb8(rgb: u16) -> Color8 /// # Examples /// /// ``` -/// use maraiah::durandal::image::{Color16, r5g5b5_to_rgb16}; +/// use maraiah::durandal::image::{r5g5b5_to_rgb16, Color16}; /// -/// assert_eq!(r5g5b5_to_rgb16(0x2345), Color16::new(0x4000, 0xD000, 0x2800)); +/// assert_eq!(r5g5b5_to_rgb16(0x2345), +/// Color16::new(0x4000, 0xD000, 0x2800)); /// ``` pub fn r5g5b5_to_rgb16(rgb: u16) -> Color16 { diff --git a/source/marathon/defl.rs b/source/marathon/defl.rs index 50904fe..76fd752 100644 --- a/source/marathon/defl.rs +++ b/source/marathon/defl.rs @@ -47,7 +47,7 @@ pub fn load_gzip_header(b: &[u8]) -> ResultS const FEXTRA: u8 = 1 << 2; const FNAME: u8 = 1 << 3; const FCOMMENT: u8 = 1 << 4; - const FRESERVED: u8 = 0xE0; + const FRESERVED: u8 = 0xe0; read_data! { endian: LITTLE, buf: b, size: 10, start: 0, data { @@ -57,7 +57,7 @@ pub fn load_gzip_header(b: &[u8]) -> ResultS } } - if id != 0x8B1F || cm != 8 { + if id != 0x8b1f || cm != 8 { bail!("not gzip format"); } @@ -112,8 +112,10 @@ pub fn load_deflate(b: &[u8]) -> ResultS<(usize, Vec)> let mut p = 0; loop { - let bfinal = read_bits_l(b, p, 1)?; p += 1; - let btype = read_bits_l(b, p, 2)?; p += 2; + let bfinal = read_bits_l(b, p, 1)?; + p += 1; + let btype = read_bits_l(b, p, 2)?; + p += 2; match btype { 0b10 => p = stream_dynamic(&mut v, b, p)?, @@ -130,19 +132,21 @@ pub fn load_deflate(b: &[u8]) -> ResultS<(usize, Vec)> fn stream_dynamic(v: &mut Vec, b: &[u8], mut p: usize) -> ResultS { - const CODE_ORDERING: [usize; 19] = [ - 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 - ]; + const CODE_ORDERING: [usize; 19] = + [16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]; // read header (number of literal alphabet codes, number of distance // alphabet codes, and number of lengths for decoding the alphabet) - let hlit = read_bits_l(b, p, 5)?; p += 5; - let hdist = read_bits_l(b, p, 5)?; p += 5; - let hclen = read_bits_l(b, p, 4)?; p += 4; + let hlit = read_bits_l(b, p, 5)?; + p += 5; + let hdist = read_bits_l(b, p, 5)?; + p += 5; + let hclen = read_bits_l(b, p, 4)?; + p += 4; - let hlit = 257 + hlit as usize; - let hdist = 1 + hdist as usize; - let hclen = 4 + hclen as usize; + let hlit = 257 + hlit as usize; + let hdist = 1 + hdist as usize; + let hclen = 4 + hclen as usize; // first, get the huffman coding for the alphabet (which is also compressed) let mut code_table = [0; 19]; @@ -166,7 +170,7 @@ fn stream_dynamic(v: &mut Vec, b: &[u8], mut p: usize) -> ResultS } // build the length and distance tables from this information - let table_len = HuffmanTable::new(&alphabet[ 0..hlit ])?; + let table_len = HuffmanTable::new(&alphabet[0..hlit])?; let table_dst = HuffmanTable::new(&alphabet[hlit..hlit + hdist])?; output_tables(v, b, p, table_len, table_dst) @@ -226,27 +230,38 @@ fn read_alphabet(b: &[u8], } 16 => { // copy previous code 3-6 times - if i == 0 {bail!("cannot copy on first alphabet code");} + if i == 0 { + bail!("cannot copy on first alphabet code"); + } let len = usize::from(read_bits_l(b, p, 2)? as u8 + 3); let lst = alphabet[i - 1]; p += 2; - for _ in 0..len {alphabet[i] = lst; i += 1;} + for _ in 0..len { + alphabet[i] = lst; + i += 1; + } } 17 => { // repeat '0' 3-10 times let len = usize::from(read_bits_l(b, p, 3)? as u8 + 3); p += 3; - for _ in 0..len {alphabet[i] = 0; i += 1;} + for _ in 0..len { + alphabet[i] = 0; + i += 1; + } } 18 => { // repeat '0' 11-138 times let len = usize::from(read_bits_l(b, p, 7)? as u8 + 11); p += 7; - for _ in 0..len {alphabet[i] = 0; i += 1;} + for _ in 0..len { + alphabet[i] = 0; + i += 1; + } } _ => { bail!("bad symbol in alphabet"); @@ -268,25 +283,22 @@ fn output_tables(v: &mut Vec, table_dst: HuffmanTable) -> ResultS { - const LEN_BASE: [usize; 29] = [ - 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, - 67, 83, 99, 115, 131, 163, 195, 227, 258 - ]; + const LEN_BASE: [usize; 29] = [3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, + 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, + 131, 163, 195, 227, 258]; - const LEN_EXTRA_BITS: [u8; 29] = [ - 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, - 5, 5, 5, 5, 0 - ]; + const LEN_EXTRA_BITS: [u8; 29] = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, + 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, + 0]; - const DST_BASE: [usize; 30] = [ - 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, - 769, 1025, 1537, 2049, 3073, 4097, 0x1801, 0x2001, 0x3001, 0x4001, 0x6001 - ]; + const DST_BASE: [usize; 30] = [1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, + 97, 129, 193, 257, 385, 513, 769, 1025, + 1537, 2049, 3073, 4097, 0x1801, 0x2001, + 0x3001, 0x4001, 0x6001]; - const DST_EXTRA_BITS: [u8; 30] = [ - 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, - 11, 11, 12, 12, 13, 13 - ]; + const DST_EXTRA_BITS: [u8; 30] = [0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, + 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, + 12, 12, 13, 13]; loop { let (bits, sym) = table_len.decode(b, p)?; diff --git a/source/marathon/machdr.rs b/source/marathon/machdr.rs index c080eda..c1b6e05 100644 --- a/source/marathon/machdr.rs +++ b/source/marathon/machdr.rs @@ -57,7 +57,11 @@ pub fn check_macbin(b: &[u8]) -> Option } // if ok, resource fork follows - if crc == u16b(&b[124..]) {Some(128)} else {None} + if crc == u16b(&b[124..]) { + Some(128) + } else { + None + } } /// Reads a `MacBin` or `AppleSingle` header if there is one and returns the diff --git a/source/marathon/map/bonk.rs b/source/marathon/map/bonk.rs index 0a7be1d..89a73c1 100644 --- a/source/marathon/map/bonk.rs +++ b/source/marathon/map/bonk.rs @@ -1,13 +1,15 @@ //! `SoundRand` type. -use crate::durandal::{err::*, fixed::{Angle, Fixed}}; +use crate::durandal::{err::*, + fixed::{Angle, Fixed}}; +use bitflags::bitflags; /// Reads a `bonk` chunk. pub fn read(b: &[u8]) -> ResultS<(SoundRand, usize)> { read_data! { endian: BIG, buf: b, size: 32, start: 0, data { - let flags = u16[0]; + let flags = u16[0] flag SoundRandFlags; let index = u16[2]; let vol_nrm = u16[4]; let vol_dta = u16[6]; @@ -20,9 +22,7 @@ pub fn read(b: &[u8]) -> ResultS<(SoundRand, usize)> } } - let no_dir = flags != 0; - - Ok((SoundRand{no_dir, index, vol_nrm, vol_dta, prd_nrm, prd_dta, yaw_nrm, + Ok((SoundRand{flags, index, vol_nrm, vol_dta, prd_nrm, prd_dta, yaw_nrm, yaw_dta, pit_nrm, pit_dta}, 32)) } @@ -31,7 +31,7 @@ pub fn read(b: &[u8]) -> ResultS<(SoundRand, usize)> #[derive(Debug, Eq, PartialEq)] pub struct SoundRand { - pub no_dir: bool, + pub flags: SoundRandFlags, pub index: u16, pub vol_nrm: u16, pub vol_dta: u16, @@ -43,4 +43,13 @@ pub struct SoundRand pub pit_dta: Fixed, } +bitflags! { + /// Flags for `SoundRand`. + #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] + pub struct SoundRandFlags: u16 + { + const NO_DIRECTION = 1; + } +} + // EOF diff --git a/source/marathon/map/epnt.rs b/source/marathon/map/epnt.rs index 2ff5cda..536e81b 100644 --- a/source/marathon/map/epnt.rs +++ b/source/marathon/map/epnt.rs @@ -1,7 +1,7 @@ //! `EndPoint` type. -use crate::durandal::{err::*, fixed::Unit}; use super::pnts; +use crate::durandal::{err::*, fixed::Unit}; use bitflags::bitflags; /// Reads an `EPNT` chunk. @@ -21,7 +21,7 @@ pub fn read(b: &[u8]) -> ResultS<(Endpoint, usize)> } /// Converts a vector of `Endpoint`s to a vector of `Point`s. -pub fn to_pnts(v: &Vec) -> Vec +pub fn to_pnts(v: &[Endpoint]) -> Vec { v.iter().map(|p| p.pos).collect() } diff --git a/source/marathon/map/lite.rs b/source/marathon/map/lite.rs index 3152d1d..ee00c0c 100644 --- a/source/marathon/map/lite.rs +++ b/source/marathon/map/lite.rs @@ -1,7 +1,7 @@ //! `Light` type. -use crate::durandal::{err::*, fixed::Fixed}; use super::{ltfn, TICKS_PER_SECOND}; +use crate::durandal::{err::*, fixed::Fixed}; use bitflags::bitflags; /// Reads a `LITE` chunk. diff --git a/source/marathon/map/medi.rs b/source/marathon/map/medi.rs index f2acf14..57c38d2 100644 --- a/source/marathon/map/medi.rs +++ b/source/marathon/map/medi.rs @@ -1,8 +1,11 @@ //! `Light` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::{Angle, Fixed, Unit}}, - marathon::xfer::TransferMode}; use super::pnts; +use crate::{durandal::{bin::OptU16, + err::*, + fixed::{Angle, Fixed, Unit}}, + marathon::xfer::TransferMode}; +use bitflags::bitflags; /// Reads a `medi` chunk. pub fn read(b: &[u8]) -> ResultS<(Media, usize)> @@ -10,7 +13,7 @@ pub fn read(b: &[u8]) -> ResultS<(Media, usize)> read_data! { endian: BIG, buf: b, size: 32, start: 0, data { let mtype = u16[0] enum MediaType; - let flags = u16[2]; + let flags = u16[2] flag MediaFlags; let control = u16[4]; let dir = Angle[6]; let mag = Unit[8]; @@ -24,9 +27,7 @@ pub fn read(b: &[u8]) -> ResultS<(Media, usize)> } } - let flr_obs = flags != 0; - - Ok((Media{mtype, flr_obs, control, dir, mag, hei_lo, hei_hi, orig, hei_nrm, + Ok((Media{mtype, flags, control, dir, mag, hei_lo, hei_hi, orig, hei_nrm, min_lt, texture, xfer}, 32)) } @@ -36,7 +37,7 @@ pub fn read(b: &[u8]) -> ResultS<(Media, usize)> pub struct Media { pub mtype: MediaType, - pub flr_obs: bool, + pub flags: MediaFlags, pub control: u16, pub dir: Angle, pub mag: Unit, @@ -62,4 +63,13 @@ c_enum! { } } +bitflags! { + /// Flags for `Media`. + #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] + pub struct MediaFlags: u16 + { + const OBSTRUCT = 1; + } +} + // EOF diff --git a/source/marathon/map/note.rs b/source/marathon/map/note.rs index 933d237..171cc5e 100644 --- a/source/marathon/map/note.rs +++ b/source/marathon/map/note.rs @@ -1,7 +1,7 @@ //! `Note` type. -use crate::{durandal::err::*, marathon::text::*}; use super::pnts; +use crate::{durandal::err::*, marathon::text::*}; /// Reads a `NOTE` chunk. pub fn read(b: &[u8]) -> ResultS<(Note, usize)> diff --git a/source/marathon/map/objs.rs b/source/marathon/map/objs.rs index 6616c99..618f1dd 100644 --- a/source/marathon/map/objs.rs +++ b/source/marathon/map/objs.rs @@ -1,6 +1,7 @@ //! `Object` type. -use crate::durandal::{err::*, fixed::{Angle, Unit}}; +use crate::durandal::{err::*, + fixed::{Angle, Unit}}; use bitflags::bitflags; /// Reads an `OBJS` chunk. diff --git a/source/marathon/map/plac.rs b/source/marathon/map/plac.rs index 6c8bc25..20b3cdf 100644 --- a/source/marathon/map/plac.rs +++ b/source/marathon/map/plac.rs @@ -1,13 +1,14 @@ //! `ObjectFreq` type. use crate::durandal::err::*; +use bitflags::bitflags; /// Reads a `plac` chunk. pub fn read(b: &[u8]) -> ResultS<(ObjectFreq, usize)> { read_data! { endian: BIG, buf: b, size: 12, start: 0, data { - let flags = u16[0]; + let flags = u16[0] flag ObjectFreqFlags; let cnt_ini = u16[2]; let cnt_min = u16[4]; let cnt_max = u16[6]; @@ -16,9 +17,7 @@ pub fn read(b: &[u8]) -> ResultS<(ObjectFreq, usize)> } } - let rnd_loc = flags != 0; - - Ok((ObjectFreq{rnd_loc, cnt_ini, cnt_min, cnt_max, cnt_rnd, chance}, 12)) + Ok((ObjectFreq{flags, cnt_ini, cnt_min, cnt_max, cnt_rnd, chance}, 12)) } /// The difficulty definition for various object types. @@ -26,7 +25,7 @@ pub fn read(b: &[u8]) -> ResultS<(ObjectFreq, usize)> #[derive(Debug, Eq, PartialEq)] pub struct ObjectFreq { - pub rnd_loc: bool, + pub flags: ObjectFreqFlags, pub cnt_ini: u16, pub cnt_min: u16, pub cnt_max: u16, @@ -34,4 +33,13 @@ pub struct ObjectFreq pub chance: u16, } +bitflags! { + /// Flags for `ObjectFreq`. + #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] + pub struct ObjectFreqFlags: u16 + { + const RANDOM_LOCATION = 1; + } +} + // EOF diff --git a/source/marathon/map/pnts.rs b/source/marathon/map/pnts.rs index e896cd9..812f472 100644 --- a/source/marathon/map/pnts.rs +++ b/source/marathon/map/pnts.rs @@ -25,10 +25,7 @@ pub fn write_o(v: Point) -> Vec } /// Reads a `PNTS` chunk. -pub fn read(b: &[u8]) -> ResultS<(Point, usize)> -{ - Ok((read_o(b)?, 4)) -} +pub fn read(b: &[u8]) -> ResultS<(Point, usize)> {Ok((read_o(b)?, 4))} /// A point in world-space. #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] diff --git a/source/marathon/map/poly.rs b/source/marathon/map/poly.rs index ed83eaf..c9337eb 100644 --- a/source/marathon/map/poly.rs +++ b/source/marathon/map/poly.rs @@ -1,8 +1,8 @@ //! `Polygon` type. +use super::pnts; use crate::{durandal::{bin::OptU16, err::*, fixed::Unit}, marathon::xfer::TransferMode}; -use super::pnts; use bitflags::bitflags; /// Reads a polygon for either M1 or M2. diff --git a/source/marathon/map/sids.rs b/source/marathon/map/sids.rs index e4a3a74..77a8a90 100644 --- a/source/marathon/map/sids.rs +++ b/source/marathon/map/sids.rs @@ -1,8 +1,8 @@ //! `Side` type. +use super::stex; use crate::{durandal::{bin::OptU16, err::*, fixed::Fixed}, marathon::xfer::TransferMode}; -use super::stex; use bitflags::bitflags; /// Reads a `SIDS` chunk. diff --git a/source/marathon/map/term.rs b/source/marathon/map/term.rs index 522b09b..a93ba94 100644 --- a/source/marathon/map/term.rs +++ b/source/marathon/map/term.rs @@ -1,7 +1,8 @@ //! `Terminal` type. -use crate::{durandal::{bin, err::*}, marathon::text}; use super::{trmf, trmg}; +use crate::{durandal::{bin, err::*}, + marathon::text}; /// Reads a `term` chunk. pub fn read(b: &[u8]) -> ResultS<(Terminal, usize)> @@ -16,13 +17,15 @@ pub fn read(b: &[u8]) -> ResultS<(Terminal, usize)> } } - let encoded = encoded != 0; - let (i_grp, x) = bin::rd_array_num(&b[10..], group_n, trmg::read)?; let (faces, y) = bin::rd_array_num(&b[10 + x..], face_n, trmf::read)?; let text = ok!(b.get(10 + x + y..end), "not enough data")?; - let text = if encoded {text::fuck_string(text)} else {text.to_vec()}; + let text = if encoded != 0 { + text::fuck_string(text) + } else { + text.to_vec() + }; let mut groups = Vec::with_capacity(group_n); diff --git a/source/marathon/phy/attk.rs b/source/marathon/phy/attk.rs index 73764cc..6a707dc 100644 --- a/source/marathon/phy/attk.rs +++ b/source/marathon/phy/attk.rs @@ -1,6 +1,8 @@ //! `Attack` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::{Angle, Unit}}}; +use crate::durandal::{bin::OptU16, + err::*, + fixed::{Angle, Unit}}; /// Reads an `Attack` object. pub fn read(b: &[u8]) -> ResultS diff --git a/source/marathon/phy/damg.rs b/source/marathon/phy/damg.rs index decaf26..15a0d40 100644 --- a/source/marathon/phy/damg.rs +++ b/source/marathon/phy/damg.rs @@ -1,6 +1,6 @@ //! `Damage` type. -use crate::{durandal::{err::*, fixed::Fixed}}; +use crate::durandal::{err::*, fixed::Fixed}; use bitflags::bitflags; /// Reads a `Damage` object. @@ -9,16 +9,14 @@ pub fn read(b: &[u8]) -> ResultS read_data! { endian: BIG, buf: b, size: 12, start: 0, data { let dtype = u16[0] enum DamageType; - let flags = u16[2]; + let flags = u16[2] flag DamageFlags; let dmg_base = u16[4]; let dmg_rand = u16[6]; let scale = Fixed[8]; } } - let alien = flags != 0; - - Ok(Damage{dtype, alien, dmg_base, dmg_rand, scale}) + Ok(Damage{dtype, flags, dmg_base, dmg_rand, scale}) } /// A damage definition. @@ -27,7 +25,7 @@ pub fn read(b: &[u8]) -> ResultS pub struct Damage { pub dtype: DamageType, - pub alien: bool, + pub flags: DamageFlags, pub dmg_base: u16, pub dmg_rand: u16, pub scale: Fixed, @@ -65,6 +63,15 @@ bitflags! { } } +bitflags! { + /// Flags for `Damage`. + #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] + pub struct DamageFlags: u16 + { + const ALIEN = 1; + } +} + c_enum! { /// A named type of damage taken by something. #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))] diff --git a/source/marathon/phy/fxpx.rs b/source/marathon/phy/fxpx.rs index 7d91b2d..a8793d0 100644 --- a/source/marathon/phy/fxpx.rs +++ b/source/marathon/phy/fxpx.rs @@ -1,6 +1,6 @@ //! `Effect` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::Fixed}}; +use crate::durandal::{bin::OptU16, err::*, fixed::Fixed}; use bitflags::bitflags; /// Reads a `FXpx` chunk. diff --git a/source/marathon/phy/mnpx.rs b/source/marathon/phy/mnpx.rs index 9c0ff41..ca6aa94 100644 --- a/source/marathon/phy/mnpx.rs +++ b/source/marathon/phy/mnpx.rs @@ -1,6 +1,8 @@ //! `Monster` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::{Fixed, Unit}}}; +use crate::durandal::{bin::OptU16, + err::*, + fixed::{Fixed, Unit}}; use bitflags::bitflags; use super::{attk, damg}; diff --git a/source/marathon/phy/prpx.rs b/source/marathon/phy/prpx.rs index eb31ba7..98c25f5 100644 --- a/source/marathon/phy/prpx.rs +++ b/source/marathon/phy/prpx.rs @@ -1,6 +1,8 @@ //! `Projectile` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::{Fixed, Unit}}}; +use crate::durandal::{bin::OptU16, + err::*, + fixed::{Fixed, Unit}}; use bitflags::bitflags; use super::damg; diff --git a/source/marathon/phy/pxpx.rs b/source/marathon/phy/pxpx.rs index 1a83e0f..8e2d779 100644 --- a/source/marathon/phy/pxpx.rs +++ b/source/marathon/phy/pxpx.rs @@ -1,6 +1,6 @@ //! `Physics` type. -use crate::{durandal::{err::*, fixed::Fixed}}; +use crate::durandal::{err::*, fixed::Fixed}; /// Reads a `PXpx` chunk. pub fn read(b: &[u8]) -> ResultS<(Physics, usize)> diff --git a/source/marathon/phy/trig.rs b/source/marathon/phy/trig.rs index f9ec60b..39e551d 100644 --- a/source/marathon/phy/trig.rs +++ b/source/marathon/phy/trig.rs @@ -1,6 +1,6 @@ //! `Monster` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::Unit}}; +use crate::durandal::{bin::OptU16, err::*, fixed::Unit}; /// Reads a `Trigger` object. pub fn read(b: &[u8]) -> ResultS diff --git a/source/marathon/phy/wppx.rs b/source/marathon/phy/wppx.rs index 91b61aa..679bbd6 100644 --- a/source/marathon/phy/wppx.rs +++ b/source/marathon/phy/wppx.rs @@ -1,6 +1,6 @@ //! `Weapon` type. -use crate::{durandal::{bin::OptU16, err::*, fixed::Fixed}}; +use crate::durandal::{bin::OptU16, err::*, fixed::Fixed}; use bitflags::bitflags; use super::trig; diff --git a/source/marathon/shp.rs b/source/marathon/shp.rs index 93aa1ae..44a1f0d 100644 --- a/source/marathon/shp.rs +++ b/source/marathon/shp.rs @@ -32,9 +32,9 @@ fn read_color(b: &[u8], clut: &mut [ColorShp]) -> ResultS<()> /// Reads all color tables. fn color_tables(b: &[u8], - tab_ofs: usize, - tab_num: usize, - clr_num: usize) + tab_ofs: usize, + tab_num: usize, + clr_num: usize) -> ResultS>> { let end = tab_num * clr_num * 8; diff --git a/source/marathon/text.rs b/source/marathon/text.rs index 2b537e1..75ce830 100644 --- a/source/marathon/text.rs +++ b/source/marathon/text.rs @@ -104,8 +104,8 @@ pub fn mac_roman_conv(s: &[u8]) -> String /// use maraiah::marathon::text::mac_roman_cstr; /// /// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken\0ed"), "I’ve awaken"); -/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken\0"), "I’ve awaken"); -/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken"), "I’ve awaken"); +/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken\0"), "I’ve awaken"); +/// assert_eq!(mac_roman_cstr(b"I\xd5ve awaken"), "I’ve awaken"); /// ``` #[inline] pub fn mac_roman_cstr(s: &[u8]) -> String @@ -133,12 +133,8 @@ pub fn to_mac_roman(s: &str) -> Vec for c in s.chars() { let c = match c { - '\n' => { - b'\r' - } - '\0'..='\x7f' => { - c as u8 - } + '\n' => b'\r', + '\0'..='\x7f' => c as u8, c => { if let Some(c) = TR.iter().position(|&o| o == c) { c as u8 + 0x80 @@ -205,19 +201,18 @@ const TR: [char; 128] = #[test] fn to_binsize_integrals() { - assert_eq!(to_binsize(0), "empty"); - assert_eq!(to_binsize(1), "1 byte"); - assert_eq!(to_binsize(2), "2 bytes"); - assert_eq!(to_binsize(999), "999 bytes"); - assert_eq!(to_binsize(1000), "1kB"); - assert_eq!(to_binsize(1000 * 7), "7kB"); - assert_eq!(to_binsize(1000 * 1000), "1MB"); - assert_eq!(to_binsize(1000 * 1000 * 7), "7MB"); - assert_eq!(to_binsize(1000 * 1000 * 1000), "1GB"); - assert_eq!(to_binsize(1000 * 1000 * 1000 * 7), "7GB"); - assert_eq!(to_binsize(1000 * 1000 * 1000 * 1000), "1TB"); + assert_eq!(to_binsize(0), "empty"); + assert_eq!(to_binsize(1), "1 byte"); + assert_eq!(to_binsize(2), "2 bytes"); + assert_eq!(to_binsize(999), "999 bytes"); + assert_eq!(to_binsize(1000), "1kB"); + assert_eq!(to_binsize(1000 * 7), "7kB"); + assert_eq!(to_binsize(1000 * 1000), "1MB"); + assert_eq!(to_binsize(1000 * 1000 * 7), "7MB"); + assert_eq!(to_binsize(1000 * 1000 * 1000), "1GB"); + assert_eq!(to_binsize(1000 * 1000 * 1000 * 7), "7GB"); + assert_eq!(to_binsize(1000 * 1000 * 1000 * 1000), "1TB"); assert_eq!(to_binsize(1000 * 1000 * 1000 * 1000 * 7), "7TB"); } - // EOF diff --git a/source/marathon/wad.rs b/source/marathon/wad.rs index 488314f..01a54ec 100644 --- a/source/marathon/wad.rs +++ b/source/marathon/wad.rs @@ -5,8 +5,7 @@ use crate::{durandal::{bin::*, err::*, image}, use std::collections::BTreeMap; /// Reads all chunks in an entry. -pub fn read_chunks(b: &[u8], old: bool, siz_cnk: usize) - -> ResultS> +pub fn read_chunks(b: &[u8], old: bool, siz_cnk: usize) -> ResultS> { let mut chunks = Vec::new(); let mut p = 0; diff --git a/source/rozinante/editor.rs b/source/rozinante/editor.rs index 54e6162..6572db1 100644 --- a/source/rozinante/editor.rs +++ b/source/rozinante/editor.rs @@ -7,17 +7,14 @@ mod block; mod state; -use crate::durandal::image::*; use super::{color, draw::*}; +use crate::durandal::image::*; impl MapEditor { /// Opens the editor with a new empty map. #[inline] - pub fn open_new(&mut self) - { - self.map = Some(state::OpenMap::default()); - } + pub fn open_new(&mut self) {self.map = Some(state::OpenMap::default());} /// Opens the editor with an existing map. pub fn open_buf(&mut self, b: &[u8]) @@ -54,24 +51,18 @@ impl MapEditor let text = &format!("tool: {:?}", st.tool()); d.clear(Color16::new(0, 0, 0)); - d.text((dw/2, dh/2), text, color::RED); + d.text((dw / 2, dh / 2), text, color::RED); } } } /// Returns `true` if `self` is closed. #[inline] - pub fn is_closed(&self) -> bool - { - self.map.is_none() - } + pub fn is_closed(&self) -> bool {self.map.is_none()} /// Returns `true` if `self` is opened. #[inline] - pub fn is_opened(&self) -> bool - { - self.map.is_some() - } + pub fn is_opened(&self) -> bool {self.map.is_some()} } impl Default for MapEditor diff --git a/source/rozinante/editor/block.rs b/source/rozinante/editor/block.rs index b5e9903..383f969 100644 --- a/source/rozinante/editor/block.rs +++ b/source/rozinante/editor/block.rs @@ -3,10 +3,7 @@ use crate::marathon::map; impl Default for Block { #[inline] - fn default() -> Self - { - Self{info: map::minf::Minf::default()} - } + fn default() -> Self {Self{info: map::minf::Minf::default()}} } /// Copyable, versioned map state. diff --git a/source/rozinante/editor/state.rs b/source/rozinante/editor/state.rs index eaa6c69..7e680af 100644 --- a/source/rozinante/editor/state.rs +++ b/source/rozinante/editor/state.rs @@ -37,10 +37,7 @@ impl OpenMap self.blocks.last_mut().unwrap() } - pub(super) fn tool(&self) -> &Tool - { - &self.tools.0 - } + pub(super) fn tool(&self) -> &Tool {&self.tools.0} pub(super) fn set_tool(&mut self, t: Tool) -> &Tool { diff --git a/source/tycho/interfaces.rs b/source/tycho/interfaces.rs index a245b3f..e6cc1bd 100644 --- a/source/tycho/interfaces.rs +++ b/source/tycho/interfaces.rs @@ -128,14 +128,18 @@ impl MapEditor /// Propagates updated information to widgets. pub fn cause_update(&mut self) { - unsafe {gtk_widget_queue_draw(self.draw);} + unsafe { + gtk_widget_queue_draw(self.draw); + } } /// Sets the drawing area widget. pub fn set_draw(&mut self, draw: *mut GtkWidget) { self.draw = draw; - unsafe {g_object_ref(self.draw as _);} + unsafe { + g_object_ref(self.draw as _); + } } } @@ -152,7 +156,9 @@ impl Drop for MapEditor { fn drop(&mut self) { - unsafe {g_object_unref(self.draw as _);} + unsafe { + g_object_unref(self.draw as _); + } } } diff --git a/tests/map.rs b/tests/map.rs index 0b99863..5d7dc6c 100644 --- a/tests/map.rs +++ b/tests/map.rs @@ -1,4 +1,5 @@ -use maraiah::{durandal::{bin, fixed::*}, marathon::map}; +use maraiah::{durandal::{bin, fixed::*}, + marathon::map}; include!("data/rand.rs");