Skip to content

Commit f6e3e66

Browse files
authored
Merge pull request #1021 from petertseng/fmt
GitHub Actions: Enforce exercises' formatting; format exercises
2 parents 44570c9 + 6b7e0e1 commit f6e3e66

File tree

27 files changed

+294
-200
lines changed

27 files changed

+294
-200
lines changed

.github/workflows/tests.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,33 @@ jobs:
109109
run: sh ./_test/check-exercise-crate.sh
110110
continue-on-error: ${{ matrix.rust == 'beta' && matrix.deny_warnings == '1' }}
111111

112+
rustformat:
113+
name: Check Rust Formatting
114+
runs-on: ubuntu-latest
115+
116+
steps:
117+
- name: Checkout code
118+
uses: actions/checkout@v2
119+
120+
- name: Setup toolchain
121+
uses: actions-rs/toolchain@v1
122+
with:
123+
toolchain: stable
124+
default: true
125+
126+
- name: Rust Format Version
127+
run: rustfmt --version
128+
129+
- name: Format
130+
run: sh bin/format_exercises
131+
132+
- name: Diff
133+
run: |
134+
if ! git diff --color --exit-code; then
135+
echo "Please run cargo fmt, or see the diff above:"
136+
exit 1
137+
fi
138+
112139
clippy:
113140
name: Clippy
114141
runs-on: ubuntu-latest

bin/format_exercises

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,17 @@
1-
#!/bin/bash
1+
#!/bin/sh
22
# Format existing exercises using rustfmt
33

44
RUST_TRACK_REPO_PATH=$(cd "$(dirname "$0")/.." && pwd)
55

66
EXERCISES_PATH="${RUST_TRACK_REPO_PATH}/exercises/"
77

8-
# Some exercises use custom formatting for illustration purpose and should not be formatted.
9-
IGNORED_EXERCISES=(
10-
"diamond"
11-
"minesweeper"
12-
"rectangles"
13-
)
14-
15-
# Iterate over every exercise directory and if it is not ignored - format it
168
for exercise_dir in ${EXERCISES_PATH}/*; do
17-
exercise_name=${exercise_dir##*/}
18-
19-
if [[ " ${IGNORED_EXERCISES[*]} " == *"$exercise_name"* ]]; then
20-
echo "$exercise_name - Ignored"
21-
else
22-
(
23-
echo "$exercise_name - Formatting"
24-
25-
cd "$exercise_dir"
26-
27-
cargo fmt
28-
29-
[ -f example.rs ] && rustfmt example.rs
30-
)
31-
fi
32-
9+
exercise_name=$(basename $exercise_dir)
10+
(
11+
cd "$exercise_dir"
12+
cargo fmt
13+
if [ -f example.rs ]; then
14+
rustfmt example.rs
15+
fi
16+
)
3317
done

exercises/acronym/tests/acronym.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ fn punctuation() {
3030
#[test]
3131
#[ignore]
3232
fn all_caps_word() {
33-
assert_eq!(acronym::abbreviate("GNU Image Manipulation Program"), "GIMP");
33+
assert_eq!(
34+
acronym::abbreviate("GNU Image Manipulation Program"),
35+
"GIMP"
36+
);
3437
}
3538

3639
#[test]
@@ -52,7 +55,9 @@ fn punctuation_without_whitespace() {
5255
#[ignore]
5356
fn very_long_abbreviation() {
5457
assert_eq!(
55-
acronym::abbreviate("Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"),
58+
acronym::abbreviate(
59+
"Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me"
60+
),
5661
"ROTFLSHTMDCOALM"
5762
);
5863
}
@@ -69,17 +74,11 @@ fn consecutive_delimiters() {
6974
#[test]
7075
#[ignore]
7176
fn apostrophes() {
72-
assert_eq!(
73-
acronym::abbreviate("Halley's Comet"),
74-
"HC"
75-
);
77+
assert_eq!(acronym::abbreviate("Halley's Comet"), "HC");
7678
}
7779

7880
#[test]
7981
#[ignore]
8082
fn underscore_emphasis() {
81-
assert_eq!(
82-
acronym::abbreviate("The Road _Not_ Taken"),
83-
"TRNT"
84-
);
83+
assert_eq!(acronym::abbreviate("The Road _Not_ Taken"), "TRNT");
8584
}

exercises/all-your-base/example.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn convert<P: AsRef<[Digit]>>(
5555
if let Some(&invalid) = digits.as_ref().iter().find(|&num| *num >= from_base) {
5656
return Err(Error::InvalidDigit(invalid));
5757
}
58-
58+
5959
// convert all digits into a single large number
6060
let mut immediate: Digit = digits
6161
.as_ref()
@@ -75,6 +75,6 @@ pub fn convert<P: AsRef<[Digit]>>(
7575
res.reverse();
7676
if res.is_empty() {
7777
res.push(0);
78-
}
78+
}
7979
Ok(res)
8080
}

exercises/bob/tests/bob.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,196 +8,183 @@ fn test_stating_something() {
88
process_response_case("Tom-ay-to, tom-aaaah-to.", "Whatever.");
99
}
1010

11-
1211
#[test]
1312
#[ignore]
1413
/// ending with whitespace
1514
fn test_ending_with_whitespace() {
1615
process_response_case("Okay if like my spacebar quite a bit? ", "Sure.");
1716
}
1817

19-
2018
#[test]
2119
#[ignore]
2220
/// shouting numbers
2321
fn test_shouting_numbers() {
2422
process_response_case("1, 2, 3 GO!", "Whoa, chill out!");
2523
}
2624

27-
2825
#[test]
2926
#[ignore]
3027
/// other whitespace
3128
fn test_other_whitespace() {
3229
process_response_case("\r\r ", "Fine. Be that way!");
3330
}
3431

35-
3632
#[test]
3733
#[ignore]
3834
/// shouting with special characters
3935
fn test_shouting_with_special_characters() {
40-
process_response_case("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!", "Whoa, chill out!");
36+
process_response_case(
37+
"ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!",
38+
"Whoa, chill out!",
39+
);
4140
}
4241

43-
4442
#[test]
4543
#[ignore]
4644
/// talking forcefully
4745
fn test_talking_forcefully() {
4846
process_response_case("Hi there!", "Whatever.");
4947
}
5048

51-
5249
#[test]
5350
#[ignore]
5451
/// prattling on
5552
fn test_prattling_on() {
5653
process_response_case("Wait! Hang on. Are you going to be OK?", "Sure.");
5754
}
5855

59-
6056
#[test]
6157
#[ignore]
6258
/// forceful question
6359
fn test_forceful_question() {
64-
process_response_case("WHAT'S GOING ON?", "Calm down, I know what I'm doing!");
60+
process_response_case("WHAT'S GOING ON?", "Calm down, I know what I'm doing!");
6561
}
6662

67-
6863
#[test]
6964
#[ignore]
7065
/// shouting with no exclamation mark
7166
fn test_shouting_with_no_exclamation_mark() {
7267
process_response_case("I HATE THE DENTIST", "Whoa, chill out!");
7368
}
7469

75-
7670
#[test]
7771
#[ignore]
7872
/// asking gibberish
7973
fn test_asking_gibberish() {
8074
process_response_case("fffbbcbeab?", "Sure.");
8175
}
8276

83-
8477
#[test]
8578
#[ignore]
8679
/// question with no letters
8780
fn test_question_with_no_letters() {
8881
process_response_case("4?", "Sure.");
8982
}
9083

91-
9284
#[test]
9385
#[ignore]
9486
/// no letters
9587
fn test_no_letters() {
9688
process_response_case("1, 2, 3", "Whatever.");
9789
}
9890

99-
10091
#[test]
10192
#[ignore]
10293
/// statement containing question mark
10394
fn test_statement_containing_question_mark() {
10495
process_response_case("Ending with ? means a question.", "Whatever.");
10596
}
10697

107-
10898
//NEW
10999
#[test]
110100
#[ignore]
111101
/// multiple line question
112102
fn test_multiple_line_question() {
113-
process_response_case("\rDoes this cryogenic chamber make me look fat?\rNo.", "Whatever.");
103+
process_response_case(
104+
"\rDoes this cryogenic chamber make me look fat?\rNo.",
105+
"Whatever.",
106+
);
114107
}
115108

116-
117109
#[test]
118110
#[ignore]
119111
/// non-question ending with whitespace
120112
fn test_nonquestion_ending_with_whitespace() {
121-
process_response_case("This is a statement ending with whitespace ", "Whatever.");
113+
process_response_case(
114+
"This is a statement ending with whitespace ",
115+
"Whatever.",
116+
);
122117
}
123118

124-
125119
#[test]
126120
#[ignore]
127121
/// shouting
128122
fn test_shouting() {
129123
process_response_case("WATCH OUT!", "Whoa, chill out!");
130124
}
131125

132-
133126
#[test]
134127
#[ignore]
135128
/// non-letters with question
136129
fn test_nonletters_with_question() {
137130
process_response_case(":) ?", "Sure.");
138131
}
139132

140-
141133
#[test]
142134
#[ignore]
143135
/// shouting gibberish
144136
fn test_shouting_gibberish() {
145137
process_response_case("FCECDFCAAB", "Whoa, chill out!");
146138
}
147139

148-
149140
#[test]
150141
#[ignore]
151142
/// asking a question
152143
fn test_asking_a_question() {
153144
process_response_case("Does this cryogenic chamber make me look fat?", "Sure.");
154145
}
155146

156-
157147
#[test]
158148
#[ignore]
159149
/// asking a numeric question
160150
fn test_asking_a_numeric_question() {
161151
process_response_case("You are, what, like 15?", "Sure.");
162152
}
163153

164-
165154
#[test]
166155
#[ignore]
167156
/// silence
168157
fn test_silence() {
169158
process_response_case("", "Fine. Be that way!");
170159
}
171160

172-
173161
#[test]
174162
#[ignore]
175163
/// starting with whitespace
176164
fn test_starting_with_whitespace() {
177165
process_response_case(" hmmmmmmm...", "Whatever.");
178166
}
179167

180-
181168
#[test]
182169
#[ignore]
183170
/// using acronyms in regular speech
184171
fn test_using_acronyms_in_regular_speech() {
185-
process_response_case("It's OK if you don't want to go work for NASA.", "Whatever.");
172+
process_response_case(
173+
"It's OK if you don't want to go work for NASA.",
174+
"Whatever.",
175+
);
186176
}
187177

188-
189178
#[test]
190179
#[ignore]
191180
/// alternate silence
192181
fn test_alternate_silence() {
193182
process_response_case(" ", "Fine. Be that way!");
194183
}
195184

196-
197185
#[test]
198186
#[ignore]
199187
/// prolonged silence
200188
fn test_prolonged_silence() {
201189
process_response_case(" ", "Fine. Be that way!");
202190
}
203-

exercises/bowling/tests/bowling.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ fn cannot_roll_after_bonus_roll_for_strike() {
432432
assert_eq!(game.roll(2), Err(Error::GameComplete));
433433
}
434434

435-
436435
#[test]
437436
#[ignore]
438437
fn last_two_strikes_followed_by_only_last_bonus_with_non_strike_points() {

exercises/decimal/example.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Decimal {
7676
lower_precision.decimal_index += precision_difference;
7777
}
7878
match one.decimal_index.cmp(&two.decimal_index) {
79-
std::cmp::Ordering::Equal => {},
79+
std::cmp::Ordering::Equal => {}
8080
std::cmp::Ordering::Less => expand(&mut one, &two),
8181
std::cmp::Ordering::Greater => expand(&mut two, &one),
8282
}
@@ -118,7 +118,13 @@ macro_rules! auto_impl_decimal_ops {
118118

119119
auto_impl_decimal_ops!(Add, add, |s, o| s + o, |s, _| s);
120120
auto_impl_decimal_ops!(Sub, sub, |s, o| s - o, |s, _| s);
121-
auto_impl_decimal_ops!(#[allow(clippy::suspicious_arithmetic_impl)] Mul, mul, |s, o| s * o, |s, o| s + o);
121+
auto_impl_decimal_ops!(
122+
#[allow(clippy::suspicious_arithmetic_impl)]
123+
Mul,
124+
mul,
125+
|s, o| s * o,
126+
|s, o| s + o
127+
);
122128

123129
macro_rules! auto_impl_decimal_cow {
124130
($trait:ident, $func_name:ident, $digits_operation:expr, $return_type:ty) => {

0 commit comments

Comments
 (0)