Compare commits

..

4 Commits

Author SHA1 Message Date
Max Bradbury 1d7e3f0704 following some clippy recommendations 2020-10-15 23:54:23 +01:00
Max Bradbury 11c9430ccf concise syntax 2020-10-15 23:29:48 +01:00
Max Bradbury 45e113717e concise syntax 2020-10-15 23:27:47 +01:00
Max Bradbury fa6f5e1bcb remove outdated syntax 2020-10-15 23:27:22 +01:00
7 changed files with 32 additions and 37 deletions

View File

@ -11,7 +11,7 @@ impl From<String> for Dialogue {
#[inline] #[inline]
fn from(string: String) -> Dialogue { fn from(string: String) -> Dialogue {
let mut lines: Vec<&str> = string.lines().collect(); 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 ") { let name = if lines.last().unwrap().starts_with("NAME ") {
Some(lines.pop().unwrap().replace("NAME ", "")) Some(lines.pop().unwrap().replace("NAME ", ""))

View File

@ -16,7 +16,7 @@ impl FromStr for Ending {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let lines: Vec<&str> = s.lines().collect(); 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"); let dialogue = lines[1..].join("\n");
Ok(Ending { id, dialogue }) Ok(Ending { id, dialogue })

View File

@ -6,7 +6,7 @@ use std::str::FromStr;
use std::collections::HashMap; use std::collections::HashMap;
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use std::fmt; 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 - /// 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. /// so there was a maximum of 36 unique tiles. later versions are comma-separated.
@ -25,12 +25,14 @@ impl RoomFormat {
_ => Err(InvalidRoomFormat), _ => Err(InvalidRoomFormat),
} }
} }
}
fn to_string(&self) -> String { impl Display for RoomFormat {
match &self { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", match &self {
RoomFormat::Contiguous => "0", RoomFormat::Contiguous => "0",
RoomFormat::CommaSeparated => "1", RoomFormat::CommaSeparated => "1",
}.to_string() })
} }
} }
@ -826,22 +828,17 @@ impl Game {
} }
fn font_line(&self) -> String { fn font_line(&self) -> String {
if self.font == Font::AsciiSmall { match self.font {
"".to_string() Font::AsciiSmall => "".to_string(),
} else { Font::Custom => format!("\n\nDEFAULT_FONT {}", self.custom_font.as_ref().unwrap()),
if self.font == Font::Custom { _ => format!("\n\nDEFAULT_FONT {}", self.font.to_string().unwrap()),
format!("\n\nDEFAULT_FONT {}", self.custom_font.as_ref().unwrap())
} else {
format!("\n\nDEFAULT_FONT {}", self.font.to_string().unwrap())
}
} }
} }
fn text_direction_line(&self) -> &str { fn text_direction_line(&self) -> &str {
if self.text_direction == TextDirection::RightToLeft { match self.text_direction {
"\n\nTEXT_DIRECTION RTL" TextDirection::RightToLeft => "\n\nTEXT_DIRECTION RTL",
} else { _ => "",
""
} }
} }

View File

@ -1,5 +1,3 @@
extern crate loe;
use std::fmt::Display; use std::fmt::Display;
use std::io::Cursor; use std::io::Cursor;
@ -162,7 +160,7 @@ fn new_unique_id(ids: Vec<String>) -> String {
new_id += 1; new_id += 1;
} }
return to_base36(new_id); to_base36(new_id)
} }
pub trait Quote { pub trait Quote {
@ -171,7 +169,7 @@ pub trait Quote {
impl Quote for String { impl Quote for String {
fn quote(&self) -> String { fn quote(&self) -> String {
format!("\"\"\"\n{}\n\"\"\"", self).to_string() format!("\"\"\"\n{}\n\"\"\"", self)
} }
} }

View File

@ -67,7 +67,7 @@ impl From<String> for Room {
if last_line.starts_with("WAL") { if last_line.starts_with("WAL") {
let last_line = last_line.replace("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(); walls = ids.iter().map(|&id| id.to_string()).collect();
} else if last_line.starts_with("NAME") { } else if last_line.starts_with("NAME") {
name = Some(last_line.replace("NAME ", "").to_string()); name = Some(last_line.replace("NAME ", "").to_string());
@ -80,8 +80,7 @@ impl From<String> for Room {
let position = item_position[1]; let position = item_position[1];
let position = Position::from_str(position); let position = Position::from_str(position);
if position.is_ok() { if let Ok(position) = position {
let position = position.unwrap();
items.push(Instance { position, id: item_id.to_string() }); items.push(Instance { position, id: item_id.to_string() });
} }
} else if last_line.starts_with("EXT") { } else if last_line.starts_with("EXT") {
@ -89,14 +88,12 @@ impl From<String> for Room {
let parts: Vec<&str> = last_line.split(' ').collect(); let parts: Vec<&str> = last_line.split(' ').collect();
let position = Position::from_str(parts[0]); let position = Position::from_str(parts[0]);
if position.is_ok() { if let Ok(position) = position {
let position = position.unwrap();
let exit = Exit::from_str( let exit = Exit::from_str(
&format!("{} {}", parts[1], parts[2]) &format!("{} {}", parts[1], parts[2])
); );
if exit.is_ok() { if let Ok(exit) = exit {
let exit = exit.unwrap();
let mut transition = None; let mut transition = None;
let mut dialogue_id = None; let mut dialogue_id = None;
let chunks = parts[3..].chunks(2); let chunks = parts[3..].chunks(2);
@ -117,8 +114,7 @@ impl From<String> for Room {
let position = ending_position[1]; let position = ending_position[1];
let position = Position::from_str(position); let position = Position::from_str(position);
if position.is_ok() { if let Ok(position) = position {
let position = position.unwrap();
endings.push(Instance { position, id: ending }); endings.push(Instance { position, id: ending });
} }
} else { } else {
@ -131,8 +127,8 @@ impl From<String> for Room {
let dimension = lines.len(); // x or y, e.g. `16` for 16x16 let dimension = lines.len(); // x or y, e.g. `16` for 16x16
let mut tiles: Vec<String> = Vec::new(); let mut tiles: Vec<String> = Vec::new();
for line in lines.into_iter() { for line in lines.iter() {
let comma_separated = line.contains(","); // old room format? let comma_separated = line.contains(','); // old room format?
let mut line: Vec<&str> = line let mut line: Vec<&str> = line
.split(if comma_separated {","} else {""}) .split(if comma_separated {","} else {""})
.collect(); .collect();
@ -181,8 +177,9 @@ impl Room {
tiles.pop(); // remove trailing comma tiles.pop(); // remove trailing comma
} }
tiles.push_str("\n"); tiles.push('\n');
} }
tiles.pop(); // remove trailing newline tiles.pop(); // remove trailing newline
for instance in &self.items { for instance in &self.items {

View File

@ -40,10 +40,13 @@ impl Sprite {
} }
fn item_lines(&self) -> String { fn item_lines(&self) -> String {
if self.items.len() == 0 { if self.items.is_empty() {
"".to_string() "".to_string()
} else { } else {
let lines: Vec<String> = self.items.iter().map(|item| format!("ITM {}", item)).collect(); let lines: Vec<String> = self.items.iter().map(
|item| format!("ITM {}", item)
).collect();
format!("\n{}", lines.join("\n")) format!("\n{}", lines.join("\n"))
} }
} }

View File

@ -7,7 +7,7 @@ pub struct Variable {
impl From<String> for Variable { impl From<String> for Variable {
fn from(string: String) -> Variable { fn from(string: String) -> Variable {
let id_value: Vec<&str> = string.lines().collect(); let id_value: Vec<&str> = string.lines().collect();
let id = id_value[0].replace("VAR ", "").to_string(); let id = id_value[0].replace("VAR ", "");
let initial_value = if id_value.len() == 1 { let initial_value = if id_value.len() == 1 {
"".to_string() "".to_string()