From 1d7e3f070448ed2c2c48a40669077f98dee9d88b Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 15 Oct 2020 23:54:23 +0100 Subject: [PATCH] following some clippy recommendations --- src/dialogue.rs | 2 +- src/ending.rs | 2 +- src/game.rs | 29 +++++++++++++---------------- src/room.rs | 21 +++++++++------------ src/sprite.rs | 7 +++++-- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/dialogue.rs b/src/dialogue.rs index f8da0a3..17b7b70 100644 --- a/src/dialogue.rs +++ b/src/dialogue.rs @@ -11,7 +11,7 @@ impl From for Dialogue { #[inline] fn from(string: String) -> Dialogue { let mut lines: Vec<&str> = string.lines().collect(); - let id = lines[0].replace("DLG ", "").to_string(); + let id = lines[0].replace("DLG ", ""); let name = if lines.last().unwrap().starts_with("NAME ") { Some(lines.pop().unwrap().replace("NAME ", "")) diff --git a/src/ending.rs b/src/ending.rs index d3f789c..3f3228c 100644 --- a/src/ending.rs +++ b/src/ending.rs @@ -16,7 +16,7 @@ impl FromStr for Ending { fn from_str(s: &str) -> Result { let lines: Vec<&str> = s.lines().collect(); - let id = lines[0].replace("END ", "").to_string(); + let id = lines[0].replace("END ", ""); let dialogue = lines[1..].join("\n"); Ok(Ending { id, dialogue }) diff --git a/src/game.rs b/src/game.rs index a9b9166..b23d92f 100644 --- a/src/game.rs +++ b/src/game.rs @@ -6,7 +6,7 @@ use std::str::FromStr; use std::collections::HashMap; use std::borrow::BorrowMut; use std::fmt; -use std::fmt::Display; +use std::fmt::{Display, Formatter}; /// in very early versions of Bitsy, room tiles were defined as single alphanumeric characters - /// so there was a maximum of 36 unique tiles. later versions are comma-separated. @@ -25,12 +25,14 @@ impl RoomFormat { _ => Err(InvalidRoomFormat), } } +} - fn to_string(&self) -> String { - match &self { +impl Display for RoomFormat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", match &self { RoomFormat::Contiguous => "0", RoomFormat::CommaSeparated => "1", - }.to_string() + }) } } @@ -826,22 +828,17 @@ impl Game { } fn font_line(&self) -> String { - if self.font == Font::AsciiSmall { - "".to_string() - } else { - if self.font == Font::Custom { - format!("\n\nDEFAULT_FONT {}", self.custom_font.as_ref().unwrap()) - } else { - format!("\n\nDEFAULT_FONT {}", self.font.to_string().unwrap()) - } + match self.font { + Font::AsciiSmall => "".to_string(), + Font::Custom => format!("\n\nDEFAULT_FONT {}", self.custom_font.as_ref().unwrap()), + _ => format!("\n\nDEFAULT_FONT {}", self.font.to_string().unwrap()), } } fn text_direction_line(&self) -> &str { - if self.text_direction == TextDirection::RightToLeft { - "\n\nTEXT_DIRECTION RTL" - } else { - "" + match self.text_direction { + TextDirection::RightToLeft => "\n\nTEXT_DIRECTION RTL", + _ => "", } } diff --git a/src/room.rs b/src/room.rs index 31d1037..980ebef 100644 --- a/src/room.rs +++ b/src/room.rs @@ -67,7 +67,7 @@ impl From for Room { if last_line.starts_with("WAL") { let last_line = last_line.replace("WAL ", ""); - let ids: Vec<&str> = last_line.split(",").collect(); + let ids: Vec<&str> = last_line.split(',').collect(); walls = ids.iter().map(|&id| id.to_string()).collect(); } else if last_line.starts_with("NAME") { name = Some(last_line.replace("NAME ", "").to_string()); @@ -80,8 +80,7 @@ impl From for Room { let position = item_position[1]; let position = Position::from_str(position); - if position.is_ok() { - let position = position.unwrap(); + if let Ok(position) = position { items.push(Instance { position, id: item_id.to_string() }); } } else if last_line.starts_with("EXT") { @@ -89,14 +88,12 @@ impl From for Room { let parts: Vec<&str> = last_line.split(' ').collect(); let position = Position::from_str(parts[0]); - if position.is_ok() { - let position = position.unwrap(); + if let Ok(position) = position { let exit = Exit::from_str( &format!("{} {}", parts[1], parts[2]) ); - if exit.is_ok() { - let exit = exit.unwrap(); + if let Ok(exit) = exit { let mut transition = None; let mut dialogue_id = None; let chunks = parts[3..].chunks(2); @@ -117,8 +114,7 @@ impl From for Room { let position = ending_position[1]; let position = Position::from_str(position); - if position.is_ok() { - let position = position.unwrap(); + if let Ok(position) = position { endings.push(Instance { position, id: ending }); } } else { @@ -131,8 +127,8 @@ impl From for Room { let dimension = lines.len(); // x or y, e.g. `16` for 16x16 let mut tiles: Vec = Vec::new(); - for line in lines.into_iter() { - let comma_separated = line.contains(","); // old room format? + for line in lines.iter() { + let comma_separated = line.contains(','); // old room format? let mut line: Vec<&str> = line .split(if comma_separated {","} else {""}) .collect(); @@ -181,8 +177,9 @@ impl Room { tiles.pop(); // remove trailing comma } - tiles.push_str("\n"); + tiles.push('\n'); } + tiles.pop(); // remove trailing newline for instance in &self.items { diff --git a/src/sprite.rs b/src/sprite.rs index 14da659..a72d476 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -40,10 +40,13 @@ impl Sprite { } fn item_lines(&self) -> String { - if self.items.len() == 0 { + if self.items.is_empty() { "".to_string() } else { - let lines: Vec = self.items.iter().map(|item| format!("ITM {}", item)).collect(); + let lines: Vec = self.items.iter().map( + |item| format!("ITM {}", item) + ).collect(); + format!("\n{}", lines.join("\n")) } }