Skip to content

Commit

Permalink
refactor: clippy warnings, clarify and simplify some numbers and expr…
Browse files Browse the repository at this point in the history
…essions
  • Loading branch information
hexjelly committed Jul 18, 2018
1 parent 0c58ab0 commit 08508ca
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 58 deletions.
62 changes: 28 additions & 34 deletions src/lev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub trait BoundingBox {
}

/// Top10 save option.
#[derive(Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Top10Save {
/// Yes. Will save best times into the file's top10 list.
Yes,
Expand Down Expand Up @@ -365,18 +365,18 @@ impl Level {
level.sky = trim_string(sky)?;

// Polygons.
let poly_count = (remaining.read_f64::<LE>()? - 0.4643643).round() as usize;
let poly_count = (remaining.read_f64::<LE>()? - 0.464_364_3).round() as usize;
let (polygons, read_bytes) = Level::parse_polygons(remaining, poly_count)?;
level.polygons = polygons;
let (_, mut remaining) = remaining.split_at(read_bytes);

// Objects.
let object_count = (remaining.read_f64::<LE>()? - 0.4643643).round() as usize;
let object_count = (remaining.read_f64::<LE>()? - 0.464_364_3).round() as usize;
let (object_data, mut remaining) = remaining.split_at(object_count * 28);
level.objects = Level::parse_objects(object_data, object_count)?;

// Pictures.
let picture_count = (remaining.read_f64::<LE>()? - 0.2345672).round() as usize;
let picture_count = (remaining.read_f64::<LE>()? - 0.234_567_2).round() as usize;
let (picture_data, mut remaining) = remaining.split_at(picture_count * 54);
level.pictures = Level::parse_pictures(picture_data, picture_count)?;

Expand Down Expand Up @@ -419,12 +419,9 @@ impl Level {
read_bytes += 16;
let x = buffer.read_f64::<LE>()?;
let y = buffer.read_f64::<LE>()?;
vertices.push(Position { x: x, y: y });
vertices.push(Position { x, y });
}
polygons.push(Polygon {
grass: grass,
vertices: vertices,
});
polygons.push(Polygon { grass, vertices });
}
Ok((polygons, read_bytes))
}
Expand All @@ -434,10 +431,10 @@ impl Level {
for _ in 0..n {
let x = buffer.read_f64::<LE>()?;
let y = buffer.read_f64::<LE>()?;
let position = Position { x: x, y: y };
let position = Position { x, y };
let object_type = buffer.read_i32::<LE>()?;
let gravity = buffer.read_i32::<LE>()?;
let gravity_direction = match gravity {
let gravity = match gravity {
0 => Direction::None,
1 => Direction::Up,
2 => Direction::Down,
Expand All @@ -446,20 +443,17 @@ impl Level {
other => return Err(ElmaError::InvalidGravity(other)),
};
let animation = buffer.read_i32::<LE>()? + 1;
let object = match object_type {
let object_type = match object_type {
1 => ObjectType::Exit,
2 => ObjectType::Apple {
gravity: gravity_direction,
animation: animation,
},
2 => ObjectType::Apple { gravity, animation },
3 => ObjectType::Killer,
4 => ObjectType::Player,
other => return Err(ElmaError::InvalidObject(other)),
};

objects.push(Object {
position: position,
object_type: object,
position,
object_type,
});
}
Ok(objects)
Expand Down Expand Up @@ -487,12 +481,12 @@ impl Level {
};

pictures.push(Picture {
name: name,
texture: texture,
mask: mask,
position: Position { x: x, y: y },
distance: distance,
clip: clip,
name,
texture,
mask,
position: Position { x, y },
distance,
clip,
});
}
Ok(pictures)
Expand Down Expand Up @@ -559,7 +553,7 @@ impl Level {
let top10_bytes = write_top10(&best_times)?;
buffer.extend_from_slice(&crypt_top10(&top10_bytes));
}
Top10Save::No => buffer.extend(crypt_top10(&vec![0; TOP10_SIZE])),
Top10Save::No => buffer.extend(crypt_top10(&[0; TOP10_SIZE])),
}

// EOF marker.
Expand All @@ -571,7 +565,7 @@ impl Level {
fn write_polygons(&self) -> Result<Vec<u8>, ElmaError> {
let mut buffer = vec![];
// Number of polygons.
buffer.write_f64::<LE>(self.polygons.len() as f64 + 0.4643643_f64)?;
buffer.write_f64::<LE>(self.polygons.len() as f64 + 0.464_364_3_f64)?;
for poly in &self.polygons {
// Grass poly.
buffer.write_i32::<LE>(if poly.grass { 1 } else { 0 })?;
Expand All @@ -589,7 +583,7 @@ impl Level {
fn write_objects(&self) -> Result<Vec<u8>, ElmaError> {
let mut buffer = vec![];
// Number of objects.
buffer.write_f64::<LE>(self.objects.len() as f64 + 0.4643643_f64)?;
buffer.write_f64::<LE>(self.objects.len() as f64 + 0.464_364_3_f64)?;
for obj in &self.objects {
// Position.
buffer.write_f64::<LE>(obj.position.x)?;
Expand Down Expand Up @@ -633,7 +627,7 @@ impl Level {
fn write_pictures(&self) -> Result<Vec<u8>, ElmaError> {
let mut buffer = vec![];
// Number of pictures.
buffer.write_f64::<LE>(self.pictures.len() as f64 + 0.2345672_f64)?;
buffer.write_f64::<LE>(self.pictures.len() as f64 + 0.234_567_2_f64)?;
for pic in &self.pictures {
// Picture name.
buffer.extend_from_slice(&string_null_pad(&pic.name, 10)?);
Expand Down Expand Up @@ -757,23 +751,23 @@ impl Level {
ObjectType::Killer => 3,
ObjectType::Player => 4,
};
obj_sum += obj.position.x + obj.position.y + (obj_type as f64);
obj_sum += obj.position.x + obj.position.y + f64::from(obj_type);
}

for pic in &self.pictures {
pic_sum += pic.position.x + pic.position.y;
}

let sum = (pol_sum + obj_sum + pic_sum) * 3247.764325643;
let sum = (pol_sum + obj_sum + pic_sum) * 3_247.764_325_643;
[
sum,
(random::<u32>() % 5871) as f64 + 11877. - sum,
f64::from(random::<u32>() % 5871) + 11877. - sum,
if valid_topology {
(random::<u32>() % 5871) as f64 + 11877. - sum
f64::from(random::<u32>() % 5871) + 11877. - sum
} else {
(random::<u32>() % 4982) as f64 + 20961. - sum
f64::from(random::<u32>() % 4982) + 20961. - sum
},
(random::<u32>() % 6102) as f64 + 12112. - sum,
f64::from(random::<u32>() % 6102) + 12112. - sum,
]
}

Expand Down
6 changes: 3 additions & 3 deletions src/lgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ impl LGR {
let mut clippings = vec![];
let mut transparencies = vec![];

for picture in self.picture_list.iter() {
for picture in &self.picture_list {
names.extend_from_slice(&string_null_pad(&picture.name, 10)?);
picture_types.write_u32::<LE>(picture.picture_type as u32)?;
distances.write_u32::<LE>(picture.distance as u32)?;
distances.write_u32::<LE>(u32::from(picture.distance))?;
clippings.write_u32::<LE>(picture.clipping as u32)?;
transparencies.write_u32::<LE>(picture.transparency as u32)?;
}
Expand All @@ -279,7 +279,7 @@ impl LGR {
fn write_picture_data(&self) -> Result<Vec<u8>, ElmaError> {
let mut bytes = vec![];

for picture in self.picture_data.iter() {
for picture in &self.picture_data {
bytes.extend_from_slice(&string_null_pad(&picture.name, 20)?);
bytes.write_u32::<LE>(picture.data.len() as u32)?;
bytes.extend_from_slice(&picture.data);
Expand Down
10 changes: 5 additions & 5 deletions src/rec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Ride {
},
None => 0_f64,
};
time * 2289.37728938
time * 2_289.377_289_38
}
}

Expand Down Expand Up @@ -285,8 +285,8 @@ named!(event<Event>,
info: le_i16 >>
event_type: le_u8 >>
add_return_error!(
ErrorKind::Custom(event_type as u32),
cond_reduce!([0, 1, 4, 5, 6, 7].iter().any(|x| *x == event_type as i32), take!(0))) >>
ErrorKind::Custom(u32::from(event_type)),
cond_reduce!([0, 1, 4, 5, 6, 7].iter().any(|x| *x == i32::from(event_type)), take!(0))) >>
take!(1) >>
info2: le_f32 >>
(Event {
Expand Down Expand Up @@ -366,8 +366,8 @@ impl Replay {
fn parse_replay(buffer: &[u8]) -> Result<Self, ElmaError> {
match parse_replay(buffer) {
Ok((_, replay)) => Ok(replay),
Err(Failure(List(v))) => match v.as_slice() {
&[_, (_, Custom(event_type)), (_, Custom(EVENT_ERROR))] => {
Err(Failure(List(v))) => match *v.as_slice() {
[_, (_, Custom(event_type)), (_, Custom(EVENT_ERROR))] => {
Err(ElmaError::InvalidEvent(event_type as u8))
}
_ => Err(ElmaError::InvalidReplayFile),
Expand Down
8 changes: 4 additions & 4 deletions src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub struct Time(pub i32);

impl Time {
/// Returns a tuple with `negative?`, `hours`, `mins`, `secs`, `hundredths`.
pub fn to_parts(&self) -> (bool, i32, i32, i32, i32) {
pub fn to_parts(self) -> (bool, i32, i32, i32, i32) {
let h = self.0 % 100;
let s = (self.0 / 100) % 60;
let m = (self.0 / (100 * 60)) % 60;
let hr = self.0 / (100 * 60 * 60);
let neg = if self.0 < 0 { true } else { false };
let neg = self.0 < 0;
(neg, hr.abs(), m.abs(), s.abs(), h.abs())
}

Expand All @@ -97,8 +97,8 @@ impl Time {
n if n == 0 => time += val,
n if n == 1 => time += val * 100,
n if n == 2 => time += val * 6000,
n if n == 3 => time += val * 360000,
n if n == 4 => time += val * 8640000,
n if n == 3 => time += val * 360_000,
n if n == 4 => time += val * 8_640_000,
_ => time = time.saturating_add(i32::MAX),
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const LEVEL_NAME_SIZE: usize = 20;
const NUM_PLAYERS: usize = 50;
const NUM_LEVELS: usize = 90;
const STATE_START: u32 = 200;
const STATE_END: u32 = 123432221;
const STATE_END_ALT: u32 = 123432112;
const STATE_END: u32 = 123_432_221;
const STATE_END_ALT: u32 = 123_432_112;
const TOP10_ENTRIES: usize = 10;

#[derive(Debug, Clone, PartialEq, Copy)]
Expand Down Expand Up @@ -177,7 +177,7 @@ named!(playerentry<PlayerEntry>,
do_parse!(
name: apply!(null_padded_string, PLAYERENTRY_NAME_SIZE) >>
cond_reduce!(!name.is_empty(), take!(0)) >>
skipped_internals: many_m_n!(NUM_INTERNALS, NUM_INTERNALS, map!(le_u8, |x| to_bool(x as i32))) >>
skipped_internals: many_m_n!(NUM_INTERNALS, NUM_INTERNALS, map!(le_u8, |x| to_bool(i32::from(x)))) >>
take!(PLAYERENTRY_PADDING) >>
last_internal: le_i32 >>
selected_internal: le_i32 >>
Expand Down Expand Up @@ -377,7 +377,7 @@ impl State {
let mut buffer = vec![];
buffer.write_u32::<LE>(STATE_START)?;

for level in self.times.iter() {
for level in &self.times {
let top10_bytes = write_top10(&level)?;
buffer.extend(top10_bytes);
}
Expand Down Expand Up @@ -408,7 +408,7 @@ impl State {
buffer.write_i32::<LE>(self.video_detail as i32)?;
buffer.write_i32::<LE>(self.animated_objects as i32)?;
buffer.write_i32::<LE>(self.animated_menus as i32)?;
for k in [&self.player_a_keys, &self.player_b_keys].iter() {
for k in &[&self.player_a_keys, &self.player_b_keys] {
buffer.write_u32::<LE>(k.throttle)?;
buffer.write_u32::<LE>(k.brake)?;
buffer.write_u32::<LE>(k.rotate_right)?;
Expand Down Expand Up @@ -476,7 +476,7 @@ fn crypt_whole_state(buf: &mut [u8]) {
LEVEL_NAME_SIZE,
];
let mut curr = 0;
for p in state_pieces.iter() {
for p in &state_pieces {
crypt_state(&mut buf[curr..curr + p]);
curr += p;
}
Expand Down
8 changes: 2 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,11 @@ pub(crate) fn boolean(input: &[u8]) -> IResult<&[u8], bool> {
}

pub(crate) fn to_bool(i: i32) -> bool {
if i == 0 {
false
} else {
true
}
i != 0
}

pub(crate) fn is_nonzero(u: u8) -> bool {
to_bool(u as i32)
to_bool(i32::from(u))
}

#[cfg(test)]
Expand Down

0 comments on commit 08508ca

Please sign in to comment.