🎨 Cargo fmt

main
Shad Amethyst 2 years ago
parent 9e32cd54b9
commit 6cee084486
Signed by: amethyst
GPG Key ID: D970C8DD1D6DEE36

@ -1,12 +1,11 @@
#![feature(iter_intersperse)]
use clap::Parser;
use stackline::prelude::*;
use stackline::tile::*;
use clap::Parser;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;
use std::io::Write;
#[derive(Parser, Debug)]
#[clap(author, version, about)]
@ -20,12 +19,10 @@ fn main() {
let mut world: World = if let Some(path) = &args.file {
match std::fs::read_to_string(path) {
Ok(raw) => {
serde_json::from_str(&raw).expect("Couldn't parse JSON")
}
Ok(raw) => serde_json::from_str(&raw).expect("Couldn't parse JSON"),
Err(err) => {
eprintln!("Couldn't open {}: {}", path.display(), err);
return
return;
}
}
} else {
@ -63,8 +60,16 @@ fn main() {
}
Some("pane") => {
if let (Some(name), Some(x), Some(y), Some(width), Some(height)) = (tokens.next(), tokens.next(), tokens.next(), tokens.next(), tokens.next()) {
if let (Ok(x), Ok(y), Ok(width), Ok(height)) = (x.parse(), y.parse(), width.parse(), height.parse()) {
if let (Some(name), Some(x), Some(y), Some(width), Some(height)) = (
tokens.next(),
tokens.next(),
tokens.next(),
tokens.next(),
tokens.next(),
) {
if let (Ok(x), Ok(y), Ok(width), Ok(height)) =
(x.parse(), y.parse(), width.parse(), height.parse())
{
pane(&mut world, name, x, y, width, height);
}
} else {
@ -82,7 +87,9 @@ fn main() {
}
}
Some("set") => {
if let (Some(x), Some(y), Some(name)) = (tokens.next(), tokens.next(), tokens.next()) {
if let (Some(x), Some(y), Some(name)) =
(tokens.next(), tokens.next(), tokens.next())
{
if let (Ok(x), Ok(y)) = (x.parse(), y.parse()) {
set(&mut world, x, y, name);
}
@ -100,16 +107,26 @@ fn main() {
}
}
Some("prop") => {
if let (Some(x), Some(y), Some(prop_name)) = (tokens.next(), tokens.next(), tokens.next()) {
if let (Some(x), Some(y), Some(prop_name)) =
(tokens.next(), tokens.next(), tokens.next())
{
if let (Ok(x), Ok(y)) = (x.parse(), y.parse()) {
prop(&mut world, x, y, prop_name, tokens.intersperse(" ").collect());
prop(
&mut world,
x,
y,
prop_name,
tokens.intersperse(" ").collect(),
);
}
} else {
eprintln!("Expected four arguments");
}
}
Some("state") => {
if let (Some(x), Some(y), Some(new_state)) = (tokens.next(), tokens.next(), tokens.next()) {
if let (Some(x), Some(y), Some(new_state)) =
(tokens.next(), tokens.next(), tokens.next())
{
if let (Ok(x), Ok(y)) = (x.parse(), y.parse()) {
state(&mut world, x, y, new_state);
}
@ -155,7 +172,9 @@ fn main() {
}
}
Some("dir") => {
if let (Some(x), Some(y), Some(direction)) = (tokens.next(), tokens.next(), tokens.next()) {
if let (Some(x), Some(y), Some(direction)) =
(tokens.next(), tokens.next(), tokens.next())
{
if let (Ok(x), Ok(y)) = (x.parse(), y.parse()) {
dir(&mut world, x, y, direction);
}
@ -184,8 +203,12 @@ fn main() {
}
Some("copy") => {
if let (Some(x), Some(y), Some(x2), Some(y2)) = (tokens.next(), tokens.next(), tokens.next(), tokens.next()) {
if let (Ok(x), Ok(y), Ok(x2), Ok(y2)) = (x.parse(), y.parse(), x2.parse(), y2.parse()) {
if let (Some(x), Some(y), Some(x2), Some(y2)) =
(tokens.next(), tokens.next(), tokens.next(), tokens.next())
{
if let (Ok(x), Ok(y), Ok(x2), Ok(y2)) =
(x.parse(), y.parse(), x2.parse(), y2.parse())
{
copy(&mut world, x, y, x2, y2);
}
} else {
@ -193,8 +216,12 @@ fn main() {
}
}
Some("move") => {
if let (Some(x), Some(y), Some(x2), Some(y2)) = (tokens.next(), tokens.next(), tokens.next(), tokens.next()) {
if let (Ok(x), Ok(y), Ok(x2), Ok(y2)) = (x.parse(), y.parse(), x2.parse(), y2.parse()) {
if let (Some(x), Some(y), Some(x2), Some(y2)) =
(tokens.next(), tokens.next(), tokens.next(), tokens.next())
{
if let (Ok(x), Ok(y), Ok(x2), Ok(y2)) =
(x.parse(), y.parse(), x2.parse(), y2.parse())
{
copy(&mut world, x, y, x2, y2);
remove(&mut world, x, y);
}
@ -207,12 +234,16 @@ fn main() {
println!("- `exit`: exits");
println!("- `print`: prints the current world");
println!("- `get <x> <y>`: prints the JSON-serialized data of the tile at (x, y)");
println!("- `set <x> <y> <tilename>`: sets the tile at (x, y) to a default tilename");
println!(
"- `set <x> <y> <tilename>`: sets the tile at (x, y) to a default tilename"
);
println!("- `remove <x> <y>`: removes the tile at (x, y)");
println!("- `copy <x_from> <y_from> <x_to> <y_to>`: copies a tile");
println!("- `move <x_from> <y_from> <x_to> <y_to>`: moves a tile");
println!("- `prop <x> <y> <prop_name> [data]`: sets the property of the tile at (x, y)");
println!(
"- `prop <x> <y> <prop_name> [data]`: sets the property of the tile at (x, y)"
);
println!(" if the tile is a single tuple struct, then prop_name is ignored.");
println!(" if the tile is a tuple struct, then prop_name should be the index of the property");
@ -262,12 +293,10 @@ fn step(world: &mut World) {
fn get(world: &World, x: i32, y: i32) {
match world.get((x, y)) {
Some(tile) => {
match serde_json::to_string_pretty(&*tile) {
Ok(serialized) => println!("{}", serialized),
Err(err) => eprintln!("Error while serializing tile at {}:{}; {}", x, y, err),
}
}
Some(tile) => match serde_json::to_string_pretty(&*tile) {
Ok(serialized) => println!("{}", serialized),
Err(err) => eprintln!("Error while serializing tile at {}:{}; {}", x, y, err),
},
None => {
eprintln!("No tile at {}:{}!", x, y);
}
@ -283,7 +312,7 @@ fn prop(world: &mut World, x: i32, y: i32, prop_name: &str, value: String) {
tile
} else {
eprintln!("Tile at {}:{} is empty!", x, y);
return
return;
}
}
None => {
@ -296,7 +325,7 @@ fn prop(world: &mut World, x: i32, y: i32, prop_name: &str, value: String) {
Ok(serialized) => serialized,
Err(err) => {
eprintln!("Error while serializing tile at {}:{}; {}", x, y, err);
return
return;
}
};
@ -304,11 +333,10 @@ fn prop(world: &mut World, x: i32, y: i32, prop_name: &str, value: String) {
Ok(parsed) => parsed,
Err(err) => {
eprintln!("Error while parsing value: {}", err);
return
return;
}
};
if let Value::Object(ref mut enum_map) = &mut tile_value {
if let Some(enum_value) = enum_map.values_mut().next() {
match enum_value {
@ -318,8 +346,12 @@ fn prop(world: &mut World, x: i32, y: i32, prop_name: &str, value: String) {
Value::Array(vec) => {
if let Ok(num) = prop_name.parse::<usize>() {
if num >= vec.len() {
eprintln!("Index out of bound: len is {} but index is {}", vec.len(), num);
return
eprintln!(
"Index out of bound: len is {} but index is {}",
vec.len(),
num
);
return;
}
vec[num] = parsed;
}
@ -330,11 +362,11 @@ fn prop(world: &mut World, x: i32, y: i32, prop_name: &str, value: String) {
}
} else {
eprintln!("Format error: expected enum to be encoded as a single-element map.");
return
return;
}
} else {
eprintln!("Format error: expected enum to be encoded as a single-element map.");
return
return;
}
match serde_json::from_value(tile_value) {
@ -406,7 +438,7 @@ fn push(world: &mut World, x: i32, y: i32, value: String) {
Ok(value) => value,
Err(err) => {
eprintln!("Error while parsing value: {}", err);
return
return;
}
};
@ -416,28 +448,26 @@ fn push(world: &mut World, x: i32, y: i32, value: String) {
Value::Number(f)
} else {
eprintln!("Unsupported value: {:?}", num);
return
return;
}
}
JValue::String(s) => Value::String(s),
x => {
eprintln!("Unsupported value: {:?}", x);
return
return;
}
};
match world.get_mut((x, y)) {
Some(tile) => {
match tile.take_signal() {
Some(mut signal) => {
signal.push(value);
tile.set_signal(Some(signal));
}
None => {
eprintln!("No signal at {}:{}!", x, y);
}
Some(tile) => match tile.take_signal() {
Some(mut signal) => {
signal.push(value);
tile.set_signal(Some(signal));
}
}
None => {
eprintln!("No signal at {}:{}!", x, y);
}
},
None => {
eprintln!("No tile at {}:{}!", x, y);
}
@ -446,28 +476,26 @@ fn push(world: &mut World, x: i32, y: i32, value: String) {
fn pop(world: &mut World, x: i32, y: i32) {
match world.get_mut((x, y)) {
Some(tile) => {
match tile.take_signal() {
Some(mut signal) => {
let popped = signal.pop();
tile.set_signal(Some(signal));
if let Some(popped) = popped {
match serde_json::to_string_pretty(&popped) {
Ok(pretty) => println!("{}", pretty),
Err(err) => {
eprintln!("Error while printing popped value: {}", err);
}
Some(tile) => match tile.take_signal() {
Some(mut signal) => {
let popped = signal.pop();
tile.set_signal(Some(signal));
if let Some(popped) = popped {
match serde_json::to_string_pretty(&popped) {
Ok(pretty) => println!("{}", pretty),
Err(err) => {
eprintln!("Error while printing popped value: {}", err);
}
} else {
eprintln!("Nothing to pop at {}:{}!", x, y);
}
}
None => {
eprintln!("No signal at {}:{}!", x, y);
} else {
eprintln!("Nothing to pop at {}:{}!", x, y);
}
}
}
None => {
eprintln!("No signal at {}:{}!", x, y);
}
},
None => {
eprintln!("No tile at {}:{}!", x, y);
}
@ -479,21 +507,19 @@ fn dir(world: &mut World, x: i32, y: i32, direction: &str) {
Ok(direction) => direction,
Err(err) => {
eprintln!("Error while parsing direction: {}", err);
return
return;
}
};
match world.get_mut((x, y)) {
Some(tile) => {
match tile.take_signal() {
Some(signal) => {
tile.set_signal(Some(signal.moved(direction)));
}
None => {
eprintln!("No signal at {}:{}!", x, y);
}
Some(tile) => match tile.take_signal() {
Some(signal) => {
tile.set_signal(Some(signal.moved(direction)));
}
}
None => {
eprintln!("No signal at {}:{}!", x, y);
}
},
None => {
eprintln!("No tile at {}:{}!", x, y);
}
@ -505,7 +531,7 @@ fn state(world: &mut World, x: i32, y: i32, state: &str) {
Ok(state) => state,
Err(err) => {
eprintln!("Error while parsing state: {}", err);
return
return;
}
};
@ -534,16 +560,14 @@ fn save(world: &World, path: impl AsRef<Path>) {
fn load(world: &mut World, path: impl AsRef<Path>) {
match std::fs::read_to_string(path.as_ref()) {
Ok(string) => {
match serde_json::from_str(&string) {
Ok(parsed) => {
*world = parsed;
}
Err(err) => {
eprintln!("Error while parsing file: {}", err);
}
Ok(string) => match serde_json::from_str(&string) {
Ok(parsed) => {
*world = parsed;
}
}
Err(err) => {
eprintln!("Error while parsing file: {}", err);
}
},
Err(err) => {
eprintln!("Error while reading file: {}", err);
}
@ -552,12 +576,10 @@ fn load(world: &mut World, path: impl AsRef<Path>) {
fn copy(world: &mut World, x: i32, y: i32, x2: i32, y2: i32) {
let mut from: FullTile = match world.get((x, y)) {
Some(tile) => {
(*tile).clone()
}
Some(tile) => (*tile).clone(),
None => {
eprintln!("No tile at {}:{}!", x, y);
return
return;
}
};
@ -581,7 +603,7 @@ fn pane(world: &mut World, name: &str, x: i32, y: i32, width: usize, height: usi
Some(pane) => pane,
None => {
eprintln!("Invalid pane dimensions!");
return
return;
}
};
@ -591,6 +613,13 @@ fn pane(world: &mut World, name: &str, x: i32, y: i32, width: usize, height: usi
fn panes(world: &World) {
for (name, pane) in world.panes() {
println!("- {}: {}x{}, at {}:{}", name, pane.width(), pane.height(), pane.position().0, pane.position().1);
println!(
"- {}: {}x{}, at {}:{}",
name,
pane.width(),
pane.height(),
pane.position().0,
pane.position().1
);
}
}

@ -9,7 +9,7 @@ use wasm_bindgen::prelude::*;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern {
extern "C" {
fn alert(s: &str);
}

@ -18,29 +18,13 @@ fn benchmark_step(c: &mut Criterion) {
pane.set_tile((3, n), Wire::new(Orientation::Vertical));
}
pane.set_signal((0, 0), stackline::signal!(
(0, 0),
Direction::Right,
[]
));
pane.set_signal((3, 0), stackline::signal!(
(3, 0),
Direction::Down,
[]
));
pane.set_signal((3, 3), stackline::signal!(
(3, 3),
Direction::Left,
[]
));
pane.set_signal((0, 3), stackline::signal!(
(0, 3),
Direction::Up,
[]
));
pane.set_signal((0, 0), stackline::signal!((0, 0), Direction::Right, []));
pane.set_signal((3, 0), stackline::signal!((3, 0), Direction::Down, []));
pane.set_signal((3, 3), stackline::signal!((3, 3), Direction::Left, []));
pane.set_signal((0, 3), stackline::signal!((0, 3), Direction::Up, []));
b.iter(|| pane.step());
});

@ -1,7 +1,7 @@
use std::env;
use std::fmt::Write;
use std::fs;
use std::path::{Path, PathBuf};
use std::fmt::Write;
use syn::{Item, ItemImpl, Type};
// This script reads the contents of any rust file in the `tiles/` directory,
@ -39,7 +39,8 @@ fn main() {
.unwrap_or_else(|err| panic!("Unable to parse file {:?}: {}", src_path, err));
for item in syntax.items.iter() {
#[allow(clippy::single_match)] // I'd like to keep the match for future-proofing, for instance if I need to match for a macro call
#[allow(clippy::single_match)]
// I'd like to keep the match for future-proofing, for instance if I need to match for a macro call
match item {
Item::Impl(item) => {
if let Some(name) = parse_impl_tile(item) {
@ -51,8 +52,9 @@ fn main() {
}
if !local_names.is_empty() {
let canonical = fs::canonicalize(src_path.clone())
.unwrap_or_else(|err| panic!("Couldn't canonicalize {}: {}", src_path.display(), err));
let canonical = fs::canonicalize(src_path.clone()).unwrap_or_else(|err| {
panic!("Couldn't canonicalize {}: {}", src_path.display(), err)
});
for name in local_names.iter() {
names.push(name.clone());
}
@ -105,10 +107,9 @@ fn generate_code(files: Vec<(PathBuf, Vec<String>)>, names: Vec<String>) -> Stri
.as_path()
.file_stem()
.and_then(|x| x.to_str())
.unwrap_or_else(|| panic!(
"Couldn't extract valid UTF-8 filename from path {:?}",
file
));
.unwrap_or_else(|| {
panic!("Couldn't extract valid UTF-8 filename from path {:?}", file)
});
let path = file.as_path().to_str().expect("Invalid UTF-8 path");
writeln!(res, "#[path = \"{}\"]\nmod {};", path, module_name).unwrap();
@ -137,7 +138,12 @@ fn generate_code(files: Vec<(PathBuf, Vec<String>)>, names: Vec<String>) -> Stri
res += " match name {\n";
for name in names.iter() {
writeln!(res, " \"{0}\" => Some(Self::{0}(<{0} as Default>::default())),", name).unwrap();
writeln!(
res,
" \"{0}\" => Some(Self::{0}(<{0} as Default>::default())),",
name
)
.unwrap();
}
res += " _ => None\n";
@ -145,7 +151,8 @@ fn generate_code(files: Vec<(PathBuf, Vec<String>)>, names: Vec<String>) -> Stri
for name in names {
// impl<T: Tile> TryInto<&T> for &AnyTile
writeln!(res,
writeln!(
res,
concat!(
"impl<'a> TryInto<&'a {0}> for &'a AnyTile {{\n",
" type Error = ();\n",
@ -158,10 +165,12 @@ fn generate_code(files: Vec<(PathBuf, Vec<String>)>, names: Vec<String>) -> Stri
"}}",
),
name
).unwrap();
)
.unwrap();
// impl<T: Tile> TryInto<&mut T> for &mut AnyTile
writeln!(res,
writeln!(
res,
concat!(
"impl<'a> TryInto<&'a mut {0}> for &'a mut AnyTile {{\n",
" type Error = ();\n",
@ -174,7 +183,8 @@ fn generate_code(files: Vec<(PathBuf, Vec<String>)>, names: Vec<String>) -> Stri
"}}",
),
name
).unwrap();
)
.unwrap();
}
res

@ -222,7 +222,10 @@ impl<'a> UpdateContext<'a> {
/// Shortcut for calling both `ctx.offset(offset)` and `ctx.get(pos)`
#[inline]
pub fn get_offset<'b>(&'b self, offset: (i8, i8)) -> Option<((usize, usize), VecRef<'b, FullTile>)>
pub fn get_offset<'b>(
&'b self,
offset: (i8, i8),
) -> Option<((usize, usize), VecRef<'b, FullTile>)>
where
'a: 'b,
{
@ -284,7 +287,11 @@ impl<'a> UpdateContext<'a> {
///
/// The actions of this function will only be executed *after* all the tiles of the [`Pane`] were [`updated`](Pane::step).
/// See [`keep`](UpdateContext::keep) for a variant of this method that takes effect immediately.
pub fn force_send(&mut self, position: (usize, usize), mut signal: Signal) -> Result<(), SendError> {
pub fn force_send(
&mut self,
position: (usize, usize),
mut signal: Signal,
) -> Result<(), SendError> {
if !self.in_bounds(position) {
return Err(SendError(signal));
}
@ -303,12 +310,16 @@ impl<'a> UpdateContext<'a> {
///
/// The actions of this function will only be executed *after* all the tiles of the [`Pane`] were [`updated`](Pane::step).
/// See [`keep`](UpdateContext::keep) for a variant of this method that takes effect immediately.
pub fn send(&mut self, position: (usize, usize), direction: Direction, signal: Signal) -> Result<(), SendError> {
pub fn send(
&mut self,
position: (usize, usize),
direction: Direction,
signal: Signal,
) -> Result<(), SendError> {
if self.accepts_signal(position, direction) {
let original_direction = signal.direction();
self.force_send(position, signal.moved(direction)).map_err(|e| {
SendError(e.0.moved(original_direction))
})
self.force_send(position, signal.moved(direction))
.map_err(|e| SendError(e.0.moved(original_direction)))
} else {
Err(SendError(signal))
}

@ -1,6 +1,6 @@
use super::*;
use serde::{Deserialize, Serialize};
use veccell::{VecCell, VecRef, VecRefMut};
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct Pane {
@ -257,10 +257,7 @@ impl Pane {
/// This function does not need a mutable reference to `self`, and makes use
/// of [`VecCell`]'s ability to provide interior mutability for one item at a time.
#[inline]
pub(crate) fn borrow_mut(
&self,
position: (usize, usize),
) -> Option<VecRefMut<'_, FullTile>> {
pub(crate) fn borrow_mut(&self, position: (usize, usize)) -> Option<VecRefMut<'_, FullTile>> {
if !self.in_bounds(position) {
return None;
}

@ -1,5 +1,5 @@
use super::*;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Value {

@ -1,5 +1,5 @@
use super::*;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
/** Represents a tile that may be empty and may have a signal. The tile may only have a signal if it isn't empty.
Cloning a `FullTile` results in a `FullTile` that does not have any signal.
@ -103,7 +103,8 @@ impl Default for FullTile {
}
impl<T: Tile + 'static> From<T> for FullTile
where AnyTile: From<T>
where
AnyTile: From<T>,
{
#[inline]
fn from(tile: T) -> Self {

@ -24,7 +24,9 @@ macro_rules! test_tile_setup {
#[macro_export]
macro_rules! test_set_signal {
( $pane:expr, $pos:expr, $dir:expr ) => {
$pane.set_signal($pos, crate::signal::Signal::empty($pos, $dir)).unwrap();
$pane
.set_signal($pos, crate::signal::Signal::empty($pos, $dir))
.unwrap();
};
}

@ -6,7 +6,7 @@
*/
use super::*;
use enum_dispatch::enum_dispatch;
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
mod full;
pub use full::*;
@ -177,10 +177,10 @@ pub trait Tile: std::clone::Clone + std::fmt::Debug + Serialize + for<'d> Deseri
pub mod prelude {
pub use crate::prelude::*;
pub use crate::tile::{FullTile, AnyTile};
pub use crate::signal::Signal;
pub use crate::utils::State;
pub use crate::text::*;
pub use crate::tile::{AnyTile, FullTile};
pub use crate::utils::State;
pub use serde::{Serialize, Deserialize};
pub use serde::{Deserialize, Serialize};
}

@ -1,4 +1,4 @@
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
/// Represents one or many undirected orientation(s), since we are in a 2D grid,
/// this may either be [Horizontal](Orientation::Horizontal), [Vertical](Orientation::Vertical) or both ([Any](Orientation::Any))
@ -53,12 +53,13 @@ impl Orientation {
/// Returns true iff `dir ∈ self`
#[inline]
pub fn contains(&self, dir: Direction) -> bool {
matches!((self, dir),
matches!(
(self, dir),
(Orientation::Vertical, Direction::Up)
| (Orientation::Vertical, Direction::Down)
| (Orientation::Horizontal, Direction::Left)
| (Orientation::Horizontal, Direction::Right)
| (Orientation::Any, _)
| (Orientation::Vertical, Direction::Down)
| (Orientation::Horizontal, Direction::Left)
| (Orientation::Horizontal, Direction::Right)
| (Orientation::Any, _)
)
}
}

@ -1,6 +1,6 @@
use super::*;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
use veccell::{VecRef, VecRefMut};
#[derive(Debug, Serialize, Deserialize)]
@ -38,7 +38,11 @@ impl World {
for pane in self.panes.values() {
let x2 = x - pane.position().0;
let y2 = y - pane.position().1;
if x2 >= 0 && x2 < pane.width().get() as i32 && y2 >= 0 && y2 < pane.height().get() as i32 {
if x2 >= 0
&& x2 < pane.width().get() as i32
&& y2 >= 0
&& y2 < pane.height().get() as i32
{
let x2 = x2 as usize;
let y2 = y2 as usize;
if let Some(tile) = pane.get((x2, y2)) {
@ -53,7 +57,11 @@ impl World {
for pane in self.panes.values() {
let x2 = x - pane.position().0;
let y2 = y - pane.position().1;
if x2 >= 0 && x2 < pane.width().get() as i32 && y2 >= 0 && y2 < pane.height().get() as i32 {
if x2 >= 0
&& x2 < pane.width().get() as i32
&& y2 >= 0
&& y2 < pane.height().get() as i32
{
let x2 = x2 as usize;
let y2 = y2 as usize;
if let Some(tile) = pane.get((x2, y2)) {
@ -68,7 +76,11 @@ impl World {
for pane in self.panes.values_mut() {
let x2 = x - pane.position().0;
let y2 = y - pane.position().1;
if x2 >= 0 && x2 < pane.width().get() as i32 && y2 >= 0 && y2 < pane.height().get() as i32 {
if x2 >= 0
&& x2 < pane.width().get() as i32
&& y2 >= 0
&& y2 < pane.height().get() as i32
{
let x2 = x2 as usize;
let y2 = y2 as usize;
if let Some(tile) = pane.get_mut((x2, y2)) {
@ -79,11 +91,18 @@ impl World {
None
}
pub fn get_mut_with_pos(&mut self, (x, y): (i32, i32)) -> Option<(&mut FullTile, usize, usize)> {
pub fn get_mut_with_pos(
&mut self,
(x, y): (i32, i32),
) -> Option<(&mut FullTile, usize, usize)> {
for pane in self.panes.values_mut() {
let x2 = x - pane.position().0;
let y2 = y - pane.position().1;
if x2 >= 0 && x2 < pane.width().get() as i32 && y2 >= 0 && y2 < pane.height().get() as i32 {
if x2 >= 0
&& x2 < pane.width().get() as i32
&& y2 >= 0
&& y2 < pane.height().get() as i32
{
let x2 = x2 as usize;
let y2 = y2 as usize;
if let Some(tile) = pane.get_mut((x2, y2)) {
@ -104,8 +123,7 @@ impl World {
pub fn in_pane(&self, x: i32, y: i32) -> bool {
for pane in self.panes.values() {
if
x >= pane.position().0
if x >= pane.position().0
&& y >= pane.position().1
&& x < pane.position().0 + pane.width().get() as i32
&& y < pane.position().1 + pane.height().get() as i32

@ -5,9 +5,10 @@ macro_rules! load_test {
let raw = std::fs::read_to_string(&path).unwrap_or_else(|err| {
panic!("Couldn't load {}: {}", &path, err);
});
let world: stackline::prelude::World = serde_json::from_str(&raw).expect("Couldn't parse World");
let world: stackline::prelude::World =
serde_json::from_str(&raw).expect("Couldn't parse World");
world
}}
}};
}
#[macro_export]
@ -20,7 +21,7 @@ macro_rules! run {
for _step in 0..$steps {
$world.step();
}
}
};
}
#[macro_export]
@ -30,7 +31,13 @@ macro_rules! assert_signal {
.get(($x, $y))
.expect(&format!("Couldn't get tile at {}:{}", $x, $y));
let signal = guard.signal();
assert!(signal.is_some(), "Expected signal at {}:{}!\n{}", $x, $y, $world);
assert!(
signal.is_some(),
"Expected signal at {}:{}!\n{}",
$x,
$y,
$world
);
signal
}};
@ -47,6 +54,12 @@ macro_rules! assert_no_signal {
.get(($x, $y))
.expect(&format!("Couldn't get tile at {}:{}", $x, $y));
let signal = guard.signal();
assert!(signal.is_none(), "Expected no signal at {}:{}!\n{}", $x, $y, $world);
assert!(
signal.is_none(),
"Expected no signal at {}:{}!\n{}",
$x,
$y,
$world
);
}};
}

Loading…
Cancel
Save