Skip to content

Commit

Permalink
Running rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
sunjay committed Aug 15, 2018
1 parent 9e502f6 commit 7df3b4c
Show file tree
Hide file tree
Showing 44 changed files with 961 additions and 588 deletions.
20 changes: 12 additions & 8 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
comment_width = 120
control_brace_style = "ClosingNextLine"
error_on_line_overflow = false
error_on_unformatted = false
match_block_trailing_comma = true
max_width = 120
reorder_imports = false
struct_lit_single_line = true
# Line limits are important because most sane code probably shouldn't go on
# forever on only a single line. The problem is that line limits usually end up
# impacting code that should really be exempt from them. For example, functions
# taking long string literals and #[should_panic(expected = ...)] attributes
# tend to get formatted weirdly. The value of this configuration is set to allow
# most lines containing string literals and attributes to stay the way they were
# originally formatted by the programmer. We chose this value by trying out
# different limits and seeing at which point we started accidentally making
# rustfmt do bad things. For example, at around 162, rustfmt is able to put a
# very complex function signature on one line. This is not desirable, so we
# tuned the limit to be less than that.
max_width = 142
10 changes: 5 additions & 5 deletions examples/arrowkeys.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extern crate turtle;

use turtle::Turtle;
use turtle::event::Key::{Left, Right};
use turtle::Event::KeyPressed;
use turtle::Turtle;

fn main() {
let mut turtle = Turtle::new();
Expand All @@ -25,18 +25,18 @@ fn main() {
turtle.left(4.5);
}
turtle.set_speed(4);
},
}
Right => {
turtle.set_speed(8);
for _ in 0..20 {
turtle.forward(1.0);
turtle.right(4.5);
}
turtle.set_speed(4);
},
_ => {},
}
_ => {}
},
_ => {},
_ => {}
}
}
}
Expand Down
13 changes: 4 additions & 9 deletions examples/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ impl BitVec {
// Ceiling division of bits / BITS so that we get >= bits capacity
// From: https://stackoverflow.com/a/2745086/551904
let capacity = 1 + ((bits - 1) / BITS);
Self {
bytes: vec![0; capacity],
}
}
else {
Self { bytes: vec![0; capacity] }
} else {
Default::default()
}
}
Expand Down Expand Up @@ -82,17 +79,15 @@ impl BitVec {
//TODO: Check this somehow
let bit_index = if cfg!(target_endian = "big") {
bit_index
}
else {
} else {
BITS - 1 - bit_index
};
if value {
// Set the bit to 1 by performing a bitwise OR with each bit in the byte.
// `1 << bit_index` will make sure the bit_index bit is 1.
// 1 OR anything will always result in 1, so the bit will be set to 1.
*byte |= 1 << bit_index;
}
else {
} else {
// Set the bit to 0 by performing a bitwise AND with each bit
// `!(1 << bit_index)` sets the bit_index bit to 0 and the other bits to 1.
// 1 AND x will always leave x as is.
Expand Down
4 changes: 2 additions & 2 deletions examples/coloredridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate turtle;

use std::f64::consts::E;

use turtle::{Turtle, Color, random};
use turtle::{random, Color, Turtle};

fn main() {
let mut turtle = Turtle::new();
Expand All @@ -25,7 +25,7 @@ fn main() {
let x = i as f64 * step;
// y = e^(-x^2) translated and scaled by the width and amplitude
// 200e^(-(1/200(x - 400))^2)
let y = amplitude * E.powf(-(1.0/(width / 4.0) * (x - width/2.0)).powi(2));
let y = amplitude * E.powf(-(1.0 / (width / 4.0) * (x - width / 2.0)).powi(2));

turtle.set_pen_color(random::<Color>().opaque());
turtle.set_pen_size(y * height_factor);
Expand Down
10 changes: 2 additions & 8 deletions examples/dragon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@

extern crate turtle;

use turtle::Turtle;
use turtle::Color;
use turtle::Turtle;

fn main() {
let mut turtle = Turtle::new();
Expand All @@ -63,13 +63,7 @@ fn main() {
/// `num_folds`: The number of times to fold the 'strip of paper'.
/// `color_start`/`color_end`: The color at the start/end of this subsection
/// of the curve as a number 0-255.
fn dragon(
turtle: &mut Turtle,
fold_direction: f64,
num_folds: usize,
color_start: f64,
color_end: f64,
) {
fn dragon(turtle: &mut Turtle, fold_direction: f64, num_folds: usize, color_start: f64, color_end: f64) {
let color_mid = (color_start + color_end) * 0.5;
if num_folds == 0 {
// mapping a color number 0-255 to an rgb gradient.
Expand Down
16 changes: 13 additions & 3 deletions examples/draw_turtle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
//! lengths. The more sophisticated the figure is, the more loops we need to make it.
extern crate turtle;

use turtle::{Turtle, Color, color};
use turtle::{color, Color, Turtle};

const SIZE: f64 = 1.0;
const SHELL_COLOR: Color = Color {red: 62.0, green: 114.0, blue: 29.0, alpha: 1.0};
const BODY_COLOR: Color = Color {red: 119.0, green: 178.0, blue: 85.0, alpha: 1.0};
const SHELL_COLOR: Color = Color {
red: 62.0,
green: 114.0,
blue: 29.0,
alpha: 1.0,
};
const BODY_COLOR: Color = Color {
red: 119.0,
green: 178.0,
blue: 85.0,
alpha: 1.0,
};
const EYE_COLOR: Color = color::BLACK;

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/empty_star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ fn main() {
turtle.forward(100.0);
turtle.left(angle * 2.0);
turtle.forward(100.0);
turtle.right(180.0 - angle);
turtle.right(180.0 - angle);
}
}
4 changes: 2 additions & 2 deletions examples/followmouse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate turtle;

use turtle::Turtle;
use turtle::Event::MouseMove;
use turtle::Turtle;

fn main() {
let mut turtle = Turtle::new();
Expand All @@ -20,7 +20,7 @@ fn main() {
turtle.turn_towards(target);
turtle.set_speed(8);

while let Some(MouseMove {x, y}) = turtle.drawing_mut().poll_event() {
while let Some(MouseMove { x, y }) = turtle.drawing_mut().poll_event() {
target = [x, y];
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
let red: Color = "red".into();
let white: Color = "white".into();

for i in 0..100+1 {
for i in 0..100 + 1 {
turtle.set_pen_color(red.mix(white, i as f64 / 100.0));
turtle.forward(5.0);
}
Expand Down
62 changes: 26 additions & 36 deletions examples/logo_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

extern crate turtle;

use turtle::Turtle;
use std::env;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use turtle::Turtle;

fn main() {
let mut turtle = Turtle::new();
Expand All @@ -17,8 +17,7 @@ fn main() {
println!("Interactive LOGO Mode.\nType the commands in stdin.\nPress '^C' (cmd/ctrl+C) to exit");
let stdin = io::stdin();
interpret(&mut turtle, stdin)
}
else {
} else {
let path = &args[1];
let file = File::open(path).expect("Could not open provided program");
interpret(&mut turtle, file)
Expand All @@ -29,8 +28,7 @@ fn interpret<R: io::Read>(turtle: &mut Turtle, input: R) {
let mut reader = BufReader::new(input);
loop {
let mut buffer = String::new();
let read_bytes = reader.read_line(&mut buffer)
.expect("Unable read input");
let read_bytes = reader.read_line(&mut buffer).expect("Unable read input");
if read_bytes == 0 {
// Reached EOF, break loop
break;
Expand All @@ -44,67 +42,59 @@ fn handle_command<'a, A: Iterator<Item = &'a str>>(turtle: &mut Turtle, mut args
while let Some(command) = args.next() {
match command {
"fd" | "forward" => {
let distance = parse_distance(args.next()
.expect("Expected a distance value after fd/forward command"));
let distance = parse_distance(args.next().expect("Expected a distance value after fd/forward command"));
turtle.forward(distance);
},
}
"bk" | "back" => {
let distance = parse_distance(args.next()
.expect("Expect a distance value after bk/back command"));
let distance = parse_distance(args.next().expect("Expect a distance value after bk/back command"));
turtle.backward(distance);
},
}
"lt" | "left" => {
let distance = parse_distance(args.next()
.expect("Expect a distance value after lt/left command"));
let distance = parse_distance(args.next().expect("Expect a distance value after lt/left command"));
turtle.left(distance);
},
}
"rt" | "right" => {
let distance = parse_distance(args.next()
.expect("Expect a distance value after rt/right command"));
let distance = parse_distance(args.next().expect("Expect a distance value after rt/right command"));
turtle.right(distance);
},
}
"home" => {
turtle.home();
},
}
"setx" => {
let x_pos = parse_distance(args.next()
.expect("No expression found"));
let x_pos = parse_distance(args.next().expect("No expression found"));
turtle.set_x(x_pos);
},
}
"sety" => {
let y_pos = parse_distance(args.next()
.expect("No expression found"));
let y_pos = parse_distance(args.next().expect("No expression found"));
turtle.set_y(y_pos);
},
}
"setheading" | "seth" => {
let expr = parse_distance(args.next()
.expect("No expression found"));
let expr = parse_distance(args.next().expect("No expression found"));
turtle.set_heading(expr);
},
}
"showturtle" | "st" => {
turtle.show();
},
}
"hideturtle" | "ht" => {
turtle.hide();
},
}
"clean" => {
turtle.clear();
},
}
"clearscreen" | "cs" => {
turtle.clear();
turtle.home();
},
}
"pendown" | "pd" => {
turtle.pen_down();
},
}
"penup" | "pu" => {
turtle.pen_up();
},
}
"setpensize" | "setwidth" | "setpw" => {
let value = parse_distance(args.next()
.expect("No value found"));
let value = parse_distance(args.next().expect("No value found"));
turtle.set_pen_size(value);
},
}
_ => unimplemented!("Use of invalid or unsupported LOGO command"),
}
}
Expand Down
10 changes: 5 additions & 5 deletions examples/maze/cell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use turtle::rand::{distributions::{Distribution, Standard}, Rng};
use turtle::rand::{
distributions::{Distribution, Standard},
Rng,
};

use wall::Wall;

Expand All @@ -12,10 +15,7 @@ pub struct Cell {

impl Cell {
pub fn is_all_closed(&self) -> bool {
self.north.is_closed()
&& self.east.is_closed()
&& self.south.is_closed()
&& self.west.is_closed()
self.north.is_closed() && self.east.is_closed() && self.south.is_closed() && self.west.is_closed()
}
}

Expand Down
Loading

0 comments on commit 7df3b4c

Please sign in to comment.